RateLimit
in
Backend for rate limiting and brute-force protection.
IMPORTANT: implementations must be fail-open: on a Redis/DB outage they return
allowed = true, so a storage outage does not bring down the auth flow (and lifts the
limit meanwhile). No method may throw.
Backends:
- RedisRateLimit: sliding-window log over a Redis sorted set.
- DbRateLimit: fixed window through an insert with on-duplicate-key update.
Table of Contents
Methods
- check() : RateLimitResult
- Reads the current count without incrementing and returns whether the next hit would pass.
- hit() : RateLimitResult
- Records a hit and returns whether it pushed the client over the limit; the count grows.
- purgeOlderThan() : int
- Deletes expired records; called by cron for the DB backend, a no-op for Redis (TTL is managed by `expire`).
Methods
check()
Reads the current count without incrementing and returns whether the next hit would pass.
public
check(string $key, int $limit, int $windowSeconds) : RateLimitResult
Use when the limit applies selectively (e.g. login counts only failed attempts: peek before, hit on failure).
Parameters
- $key : string
- $limit : int
- $windowSeconds : int
Return values
RateLimitResulthit()
Records a hit and returns whether it pushed the client over the limit; the count grows.
public
hit(string $key, int $limit, int $windowSeconds) : RateLimitResult
Parameters
- $key : string
-
namespaced key (e.g. 'login:
', 'refresh: ') - $limit : int
-
maximum allowed hits in the window
- $windowSeconds : int
-
window length in seconds
Return values
RateLimitResultpurgeOlderThan()
Deletes expired records; called by cron for the DB backend, a no-op for Redis (TTL is managed by `expire`).
public
purgeOlderThan(int $maxAgeSeconds) : int
Parameters
- $maxAgeSeconds : int
Return values
int —number of deleted rows (-1 on error, 0 for a no-op)