class

Vug::RateLimiter

Inherits Reference < Object

Per-host rate limiter using a sliding window algorithm. Tracks request timestamps per host and enforces a max requests per minute limit. Uses sorted arrays with binary search for O(log n) operations.

Constants

CLEANUP_INTERVAL = 1000

Run cleanup after this many allow? calls to prevent unbounded growth

DEFAULT_MAX_PER_MINUTE = 60

Default max requests per host per minute

WINDOW = 1.minute

Sliding window duration for rate limiting

Constructors

new(max_per_minute : Int32 = DEFAULT_MAX_PER_MINUTE)
Source

Instance methods

acquire(host : String) : Bool

Acquire a rate limit slot for the given host. Returns true if under the limit (slot acquired), false if rate limited. This is a stateful operation — it records the request timestamp.

Source
cleanup

Remove expired entries for hosts that haven't been queried recently. Call periodically (e.g., from a background task) to prevent unbounded growth.

Source
clear(host : String) : Nil

Clear rate limit state for a specific host.

Source
clear

Clear rate limit state for all hosts.

Source
max_per_minute
Source
remaining(host : String) : Int32

Get remaining requests for a host in the current window.

Source