LemonAi. CallDispatcher
(lemon_ai v0.1.0)
View Source
Central dispatcher for routing AI provider requests through rate limiting and circuit breaking infrastructure.
Overview
This module acts as a coordination point that:
- Checks the circuit breaker state
- Acquires a rate limit permit
- Enforces per-provider concurrency limits
- Returns appropriate errors when conditions aren't met
Usage
# Dispatch a request (typically wrapping an actual provider call)
case LemonAi.CallDispatcher.dispatch(:anthropic, fn -> make_api_call() end) do
{:ok, result} -> handle_result(result)
{:error, :rate_limited} -> retry_later()
{:error, :circuit_open} -> use_fallback()
{:error, :max_concurrency} -> queue_request()
{:error, reason} -> handle_error(reason)
endConfiguration
Concurrency caps are configured per-provider via set_concurrency_cap/2.
Default cap is 10 concurrent requests per provider.
Summary
Functions
Returns a specification to start this module under a supervisor.
Dispatch a request through rate limiting and circuit breaking.
Get the number of active requests for a provider.
Get the current concurrency cap for a provider.
Get dispatcher state for debugging/monitoring.
Set the concurrency cap for a provider.
Start the call dispatcher.
Types
@type provider() :: atom()
@type state() :: %{ concurrency_caps: %{required(provider()) => pos_integer()}, active_requests: %{required(provider()) => non_neg_integer()}, monitors: %{required(reference()) => provider()} }
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
Dispatch a request through rate limiting and circuit breaking.
Returns the callback result, or an error if the request was blocked by rate limiting, circuit breaker, or concurrency limits.
Examples
LemonAi.CallDispatcher.dispatch(:anthropic, fn ->
LemonAi.Providers.Anthropic.call(params)
end)
@spec get_active_requests(provider()) :: non_neg_integer()
Get the number of active requests for a provider.
@spec get_concurrency_cap(provider()) :: pos_integer()
Get the current concurrency cap for a provider.
@spec get_state() :: map()
Get dispatcher state for debugging/monitoring.
@spec set_concurrency_cap(provider(), pos_integer()) :: :ok
Set the concurrency cap for a provider.
@spec start_link(keyword()) :: GenServer.on_start()
Start the call dispatcher.