Coffrify.Runtime.RateLimit behaviour (Coffrify v0.9.0)

View Source

Client-side rate limiting. Use to stay below plan caps without hitting 429s.

Two adapters are bundled:

Both are GenServer-based. Pass the PID (or registered name) as :rate_limiter to Coffrify.new/1. The client calls acquire/1 before each request.

Summary

Types

t()

Any term implementing the limiter behaviour.

Callbacks

Wait until a token is available, then consume it.

Snapshot for observability.

Try to consume a token without waiting.

Functions

Acquire dispatching to the right adapter. Accepts either a PID/name (assumed TokenBucket) or a {module, pid} tuple. The module must implement the Coffrify.Runtime.RateLimit behaviour.

Types

t()

@type t() :: GenServer.server()

Any term implementing the limiter behaviour.

Callbacks

acquire(t)

@callback acquire(t()) :: :ok

Wait until a token is available, then consume it.

snapshot(t)

@callback snapshot(t()) :: map()

Snapshot for observability.

try_acquire(t)

@callback try_acquire(t()) :: boolean()

Try to consume a token without waiting.

Functions

acquire(server)

@spec acquire(any()) :: :ok

Acquire dispatching to the right adapter. Accepts either a PID/name (assumed TokenBucket) or a {module, pid} tuple. The module must implement the Coffrify.Runtime.RateLimit behaviour.