class Kemal::BasicAuth::RateLimiter

Overview

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)

Defined in:

kemal-basic-auth/rate_limiter.cr

Constant Summary

DEFAULT_MAX_ATTEMPTS = 5
DEFAULT_WINDOW = 1.minute

Constructors

Instance Method Summary

Constructor Detail

def self.new(max_attempts : Int32 = DEFAULT_MAX_ATTEMPTS, window : Time::Span = DEFAULT_WINDOW, clock : -> Time = -> do Time.utc end) #

[View source]

Instance Method Detail

def limited?(key : String) : Bool #

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


[View source]
def max_attempts : Int32 #

[View source]
def purge_expired : Nil #

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


[View source]
def 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.


[View source]
def reset(key : String) : Nil #

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


[View source]
def window : Time::Span #

[View source]