Skip to content

Auth rate limits

Fixed-window rate limiting.

rn_forge.fastapi.auth.rate_limit

Fixed-window rate limiting for the login and callback routes.

InMemoryRateLimiter(clock=time.time)

:class:RateLimiter held in process memory, for tests and development.

Parameters:

Name Type Description Default
clock Callable[[], float]

Returns the current time in seconds.

time

hit(scope, subject, rate)

Count one hit of subject in scope.

RateLimit(limit, window) dataclass

At most limit hits in each window.

Attributes:

Name Type Description
limit int

The hits one window allows.

window timedelta

The length of a window.

RateLimiter

Bases: Protocol

Counts hits per scope and subject in fixed windows.

Windows are aligned to the clock, so a client can use a limit twice around a window boundary. Every method is synchronous and thread-safe.

hit(scope, subject, rate)

Count one hit of subject in scope.

Returns:

Type Description
int | None

None while within rate, otherwise the seconds until the

int | None

window ends.

window_key(scope, subject, rate, now)

The counter key for a hit at now: a digest, so subject is never stored.

window_seconds(rate)

The length of rate's window in whole seconds, at least one.