RpcLoadBalancer.LoadBalancer (rpc_load_balancer v0.1.0)

Copy Markdown View Source

Distributed load balancer based on :pg.

Nodes register themselves when started so callers can pick an available node using a selection algorithm.

Summary

Functions

Selects a node and executes an RPC call through the load balancer.

Selects a node and executes an RPC cast through the load balancer.

Returns a specification to start this module under a supervisor.

Gets the nodes that are registered for the load balancer.

Releases a node after an RPC call completes.

Selects a node from the available nodes for the load balancer.

Selects multiple nodes from the available nodes for the load balancer.

Types

name()

@type name() :: atom() | module()

node_match_list()

@type node_match_list() :: [String.t() | Regex.t()] | :all

option()

@type option() ::
  {:node_match_list, node_match_list()}
  | {:selection_algorithm, module()}
  | {:algorithm_opts, keyword()}

opts()

@type opts() :: [GenServer.option() | option()]

Functions

call(load_balancer_name, module, fun, args, opts \\ [])

@spec call(name(), module(), atom(), [term()], keyword()) :: ErrorMessage.t_res(any())

Selects a node and executes an RPC call through the load balancer.

cast(load_balancer_name, module, fun, args, opts \\ [])

@spec cast(name(), module(), atom(), [term()], keyword()) ::
  :ok | {:error, ErrorMessage.t()}

Selects a node and executes an RPC cast through the load balancer.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_members(load_balancer_name)

@spec get_members(name()) :: ErrorMessage.t_res([node()])

Gets the nodes that are registered for the load balancer.

pg_group_name()

@spec pg_group_name() :: atom()

See RpcLoadBalancer.LoadBalancer.Pg.pg_group_name/0.

release_node(load_balancer_name, node)

@spec release_node(name(), node()) :: :ok

Releases a node after an RPC call completes.

Used by connection-tracking algorithms (e.g., Least Connections) to decrement their active connection counters.

select_node(load_balancer_name, opts \\ [])

@spec select_node(
  name(),
  keyword()
) :: ErrorMessage.t_res(node())

Selects a node from the available nodes for the load balancer.

select_nodes(load_balancer_name, count, opts \\ [])

@spec select_nodes(name(), pos_integer(), keyword()) :: ErrorMessage.t_res([node()])

Selects multiple nodes from the available nodes for the load balancer.

Useful for replication strategies where a key needs to be routed to a primary and one or more replica nodes. Returns up to count distinct nodes.

Algorithms that implement the choose_nodes/4 callback (e.g., HashRing) provide consistent multi-node selection. Other algorithms fall back to returning count randomly shuffled nodes.

start_link(opts \\ [])

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