SetuClient.RateLimiter (Setu Client v1.0.0)

Copy Markdown View Source

Client-side token-bucket rate limiter implemented as an OTP GenServer.

Each unique {client_id, environment} pair gets an independent bucket. Limits come from SetuClient.Config fields :rate_limit_rps and :rate_limit_burst.

Summary

Functions

Acquires one token for cfg, blocking if the bucket is empty.

Returns a specification to start this module under a supervisor.

Types

bucket()

@type bucket() :: %{tokens: float(), last_refill: integer()}

bucket_key()

@type bucket_key() :: {String.t(), atom()}

state()

@type state() :: %{buckets: %{optional(bucket_key()) => bucket()}}

Functions

acquire(cfg)

@spec acquire(SetuClient.Config.t()) :: :ok | {:error, :timeout}

Acquires one token for cfg, blocking if the bucket is empty.

Returns :ok when a token is granted, {:error, :timeout} after cfg.timeout ms.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.