ratekeeper v0.2.1 Ratekeeper View Source
Ratekeeper is a library to schedule rate-limited actions. It supports complex rate limits and estimates time left to reset limits.
Installation
Add ratekeeper
as dependency in mix.exs
def deps do
[{:ratekeeper, "~> 0.2.0"}]
end
Usage
Limits can be set in config:
config :ratekeeper, :limits, %{"myapi.org" => [{1000, 5}, {60000, 100}]}
or at runtime:
Ratekeeper.add_limit "myapi.org", 1000, 5
Ratekeeper.add_limit "myapi.org", 60000, 100
This sets limits to 5 requests per 1 second and 100 requests per minute.
Appoint a request to rate limited api:
case Ratekeeper.register("myapi.org", 10_000) do
nil -> raise "Rate limits exceeded, request not allowed in next 10 seconds"
delay ->
:timer.sleep(delay)
MyApi.do_request()
end
Link to this section Summary
Functions
Adds limit rule
Returns a specification to start this module under a supervisor
Deletes limit rule
Returns all limits
Returns limits for id
Registers next request to the rate limited api in specified time
Resets all hits registered for current intervals
Starts Ratekeeper server
Returns time in milliseconds to wait for the next allowed request
Link to this section Functions
Adds limit rule.
Returns a specification to start this module under a supervisor.
See Supervisor
.
Deletes limit rule.
Returns all limits
Returns limits for id
Registers next request to the rate limited api in specified time.
Returns delay to wait before the next allowed request or nil
if no request allowed in max_allowed_time
Resets all hits registered for current intervals.
Starts Ratekeeper server.
args[:limits]
can be provided to set limits in format %{bucket_name: [{interval, limit}]}
Returns time in milliseconds to wait for the next allowed request.