Syntropy.RateLimiter (syntropy v0.3.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

class()

@type class() :: :read | :write

Functions

allow(identity, class)

@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.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

enabled?()

@spec enabled?() :: boolean()

limit_per_minute(atom)

@spec limit_per_minute(class()) :: pos_integer()

reset()

@spec reset() :: :ok

Empties all buckets. Test support.

start_link(opts)

@spec start_link(term()) :: GenServer.on_start()