Auth.RateLimit

Auth

Fixed-window rate limiting on a cache Repo (ETS or Valkey).

Call hit inside Repo.transaction / Repo.on before expensive work (login, password reset, OAuth callback). Keys are caller-chosen (for example "login:" ++ ip).

Types

Outcome

type Outcome
= Allow Int
| Limited Int

Result of one hit: allowed with the new count, or limited.

Examples

case Auth.RateLimit.hit { key = "login:" ++ ip, limit = 20, windowSeconds = 60 } of
    Allow n ->
        Ok n

    Limited n ->
        Err n

Values

hit

hit : { key : String , limit : Int , windowSeconds : Int } -> Transaction Outcome

Increment key and enforce limit within windowSeconds.

On the first increment in a window the key's TTL is set to windowSeconds.

Examples

Repo.transaction
    (Auth.RateLimit.hit { key = "login:" ++ ip, limit = 20, windowSeconds = 60 })