SimpleRateLimiter (simple_rate_limiter v0.2.0)
A GenServer module that implements token bucket rate limiting.
To start the rate limiter, specify the maximum number of tokens and the refill interval in milliseconds. The token bucket is initially filled to the maximum.
To check if an action can proceed, call SimpleRateLimiter.can_proceed?/1.
It will return :ok if there are tokens available, and decrement the
token count by 1. If there are no tokens available, it will return an
error tuple with the amount of time remaining until the next token is
available.
Link to this section Summary
Functions
Checks if an action can proceed by trying to consume a token.
Returns a specification to start this module under a supervisor.
Starts the rate limiter.
Waits until an action can proceed, then calls the given function.
Link to this section Functions
can_proceed?(server \\ __MODULE__)
Checks if an action can proceed by trying to consume a token.
## Example
iex> SimpleRateLimiter.can_proceed?(pid)
:ok
child_spec(init_arg)
Returns a specification to start this module under a supervisor.
See Supervisor.
start_link(opts \\ [])
Starts the rate limiter.
## Options
:interval: The refill interval in milliseconds (default1_000).:max: The maximum number of tokens (default10).
## Example
iex> {:ok, pid} = SimpleRateLimiter.start_link(interval: 1_000, max: 10)
wait_and_proceed(server \\ __MODULE__, action_fun)
Waits until an action can proceed, then calls the given function.
## Example
iex> SimpleRateLimiter.wait_and_proceed(pid, fn -> IO.puts("Hello!") end)
Hello!