Wise.Internal.RateLimiter (Wise v1.0.0)

Copy Markdown View Source

Token-bucket rate limiter backed by a GenServer.

Provides sustained throughput control for outbound API requests. The default bucket allows 10 requests per second with a burst of 20.

Usage

{:ok, _pid} = Wise.Internal.RateLimiter.start_link(rate: 10, burst: 20)
:ok = Wise.Internal.RateLimiter.wait(pid)

Summary

Types

Rate limiter state

Functions

Returns a specification to start this module under a supervisor.

Starts a rate limiter process.

Blocks until a token is available. Returns :ok or {:error, :timeout}.

Types

state()

@type state() :: %{
  tokens: float(),
  max_tokens: float(),
  refill_rate: float(),
  last_refill: integer()
}

Rate limiter state

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

start_link(opts \\ [])

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

Starts a rate limiter process.

Options

  • :rate - sustained requests per second (default: 10)
  • :burst - maximum burst capacity (default: 20)
  • :name - GenServer registration name

wait(server \\ __MODULE__, timeout \\ 5000)

@spec wait(GenServer.server(), timeout()) :: :ok | {:error, :timeout}

Blocks until a token is available. Returns :ok or {:error, :timeout}.