Req Token bucket
Mix.install([
{:req, "~> 0.4.9"},
{:nimble_parsec, "~> 1.4"},
# {:dummy_graph_server, path: "C:\\github\\chgeuer\\_resiliency\\ex_simulated_graph_server"}
{:dummy_graph_server, github: "chgeuer/ex_simulated_graph_server"}
])
Section
Trying to inject a plugin into Req, that waits with making a request until a token is available, and updates the token bucket after the request with the remaining tokens.
defmodule InfiniteLoop do
def loop(req) do
%Req.Response{status: 200, body: body} = req |> Req.get!()
IO.puts(body)
loop(req)
end
end
When this Livebook loads, there’s a Bandit server listening on port 4000, which has a rate limiter. By clicking “Re-evaluate” on the cell below, you can issue multiple requests…
Req.get!(url: "http://127.0.0.1:4000/")
However, this cell issues requests in an endless loop (and uses a client-side rate limiter, which respects the server’s limits).
Req.new(url: "http://127.0.0.1:4000/")
|> ReqTokenBarrier.attach_now_token_barrier()
|> InfiniteLoop.loop()