SaltDns. RateLimiter
(Salt v1.0.0)
A small fixed-window rate limiter in ETS.
case RateLimiter.hit({:ip, ip}, 300, 60) do
{:allow, remaining} -> ...
{:deny, retry_after_seconds} -> ...
endhit/3 counts one request for key in the current window of window
seconds and allows it while the count stays within limit. Counters are
atomic (:ets.update_counter/4), so concurrent requests cannot slip past
the limit. Windows are aligned to the clock; an old window's counters are
swept every few seconds (5 s unless configured). Limits are per node and in memory: a restart
forgets them, and several nodes each allow limit.
Summary
Functions
Returns a specification to start this module under a supervisor.
Counts a hit for key. Returns {:allow, remaining} or
{:deny, retry_after} (seconds until the window ends, at least 1).
now is the time in seconds (defaults to the system time; tests pass it).
The number of hits counted for key in the current window, without counting one.
Forgets every counter (for tests).
Types
@type result() :: {:allow, non_neg_integer()} | {:deny, pos_integer()}
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec hit(term(), pos_integer(), pos_integer(), integer()) :: result()
Counts a hit for key. Returns {:allow, remaining} or
{:deny, retry_after} (seconds until the window ends, at least 1).
now is the time in seconds (defaults to the system time; tests pass it).
@spec peek(term(), pos_integer(), integer()) :: non_neg_integer()
The number of hits counted for key in the current window, without counting one.
@spec reset() :: :ok
Forgets every counter (for tests).