Syntropy.RateLimiter
(syntropy v0.2.0)
Copy Markdown
Dependency-free token-bucket rate limiter for the external API surface.
Buckets live in an ETS table owned by this GenServer and are keyed by
{identity, class} where identity is a hashed bearer token or the peer IP.
Refill is computed lazily from elapsed time — no sweep timers. Buckets are
sized to the per-minute limit (read_per_minute, write_per_minute), so a
full-burst client can spend a minute of budget at once and then refills
continuously.
Summary
Functions
Takes one token from the bucket for identity/class.
Returns a specification to start this module under a supervisor.
Empties all buckets. Test support.
Types
Functions
@spec allow(String.t(), class()) :: :ok | {:error, pos_integer()}
Takes one token from the bucket for identity/class.
Returns :ok or {:error, retry_after_seconds} when the bucket is empty.
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec enabled?() :: boolean()
@spec limit_per_minute(class()) :: pos_integer()
@spec reset() :: :ok
Empties all buckets. Test support.
@spec start_link(term()) :: GenServer.on_start()