Kemal::BasicAuth::RateLimiter
Simple in-memory sliding-window rate limiter for failed authentication attempts. Tracks failures per key (typically the remote address) and treats a key as "limited" once the configured threshold is reached within the window.
Thread-safe via an internal Mutex. Memory grows with the number of
distinct keys; purge_expired may be called periodically to drop
entries whose attempts have all aged out.
limiter = Kemal::BasicAuth::RateLimiter.new(max_attempts: 5, window: 1.minute)
Constants
DEFAULT_MAX_ATTEMPTS = 5
DEFAULT_WINDOW = 1.minute
Constructors
new(max_attempts : Int32 = DEFAULT_MAX_ATTEMPTS, window : Time::Span = DEFAULT_WINDOW, clock : -> Time = -> do
Time.utc
end)
SourceInstance methods
Returns true if the key has reached or exceeded max_attempts within
the active window.
max_attempts
SourceRecords a failed attempt for the given key and returns the number of attempts within the active window after this one was recorded.
Clears any recorded failures for the key (e.g. after a successful login).
window
Source