ExMCP.Transport.Beam.LoadBalancer (ex_mcp v0.9.2)
View SourceLoad balancer for distributing client connections across MCP service instances.
Provides multiple load balancing strategies optimized for BEAM transport:
- Round-robin: Cycles through available services
- Least-connections: Routes to service with fewest active connections
- Weighted: Distributes based on service weights
- Health-aware: Excludes unhealthy services from selection
Example Usage
# Start load balancer with round-robin strategy
{:ok, balancer} = LoadBalancer.start_link(%{
cluster: cluster_pid,
strategy: :round_robin,
health_aware: true
})
# Get a service instance
{:ok, service} = LoadBalancer.get_service(balancer, "calculator")
# Update service connection count for least-connections strategy
LoadBalancer.update_connections(balancer, service_id, 5)
Summary
Functions
Returns a specification to start this module under a supervisor.
Gets a service instance using the configured load balancing strategy.
Gets load balancer statistics.
Records that a connection to a service was established.
Records that a connection to a service was closed.
Starts a load balancer with the given configuration.
Stops the load balancer.
Updates connection count for a service (used by least-connections strategy).
Updates the load balancing strategy.
Types
@type config() :: %{ cluster: GenServer.server(), strategy: strategy(), health_aware: boolean(), connection_tracking: boolean(), exclude_circuit_broken: boolean() }
@type strategy() :: :round_robin | :least_connections | :weighted | :random
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec get_service(GenServer.server(), String.t(), map()) :: {:ok, map()} | {:error, term()}
Gets a service instance using the configured load balancing strategy.
@spec get_stats(GenServer.server()) :: {:ok, map()}
Gets load balancer statistics.
@spec record_connection(GenServer.server(), String.t()) :: :ok
Records that a connection to a service was established.
@spec record_disconnection(GenServer.server(), String.t()) :: :ok
Records that a connection to a service was closed.
@spec start_link(config()) :: GenServer.on_start()
Starts a load balancer with the given configuration.
@spec stop(GenServer.server()) :: :ok
Stops the load balancer.
@spec update_connections(GenServer.server(), String.t(), non_neg_integer()) :: :ok
Updates connection count for a service (used by least-connections strategy).
@spec update_strategy(GenServer.server(), strategy()) :: :ok
Updates the load balancing strategy.