View Source Hammer behaviour (hammer v7.0.0-rc.3)
Hammer is a rate-limiting library for Elixir.
It provides a simple way for creating rate limiters, and comes with a built-in ETS backend.
defmodule MyApp.RateLimit do
use Hammer, backend: :ets
end
# Start the rate limiter, in case of ETS it will create the ETS table and schedule cleanups
MyApp.RateLimit.start_link(clean_period: :timer.minutes(10))
# Check the rate limit allowing 10 requests per second
MyApp.RateLimit.hit("some-key", _scale = :timer.seconds(1), _limit = 10)
Summary
Callbacks
Optional callback for getting the counter value for a key.
Checks if a key is allowed to perform an action, and increment the counter.
Optional callback to check if a key is allowed to perform an action, and increment the counter.
Same as inc/3
with increment
set to 1.
Optional callback for incrementing a counter value for a kit without performing limit check.
Optional callback for setting the counter value for a key.
Functions
Use the Hammer library in a module to create a rate limiter.
Types
@type count() :: pos_integer()
@type increment() :: non_neg_integer()
@type key() :: term()
@type limit() :: pos_integer()
@type scale() :: pos_integer()
Callbacks
Optional callback for getting the counter value for a key.
Returns the current counter value.
Checks if a key is allowed to perform an action, and increment the counter.
Same as hit/4
with increment
set to 1.
Optional callback to check if a key is allowed to perform an action, and increment the counter.
Returns {:allow, count}
if the action is allowed, or {:deny, timeout}
if the action is denied.
This is the only required callback.
Same as inc/3
with increment
set to 1.
Optional callback for incrementing a counter value for a kit without performing limit check.
Returns the new counter value.
Optional callback for setting the counter value for a key.
Returns the new counter value.
Functions
Use the Hammer library in a module to create a rate limiter.
defmodule MyApp.RateLimit do
use Hammer, backend: :ets
end