class

Kemal::BasicAuth::RateLimiter

Inherits Reference < Object

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)
Source

Instance methods

limited?(key : String) : Bool

Returns true if the key has reached or exceeded max_attempts within the active window.

Source
max_attempts
Source
purge_expired

Drops entries whose attempts have all aged out of the window.

Source
record_failure(key : String) : Int32

Records a failed attempt for the given key and returns the number of attempts within the active window after this one was recorded.

Source
reset(key : String) : Nil

Clears any recorded failures for the key (e.g. after a successful login).

Source
window
Source