RpcLoadBalancer.LoadBalancer.SelectionAlgorithm.WeightedRoundRobin (rpc_load_balancer v0.3.4)

Copy Markdown View Source

Weighted round robin node selection algorithm.

Accepts a weight map via algorithm_opts where keys are node names and values are positive integers representing relative capacity. Nodes with higher weights receive proportionally more traffic.

Storage:

  • :weights (set once at init/2) lives in :persistent_term keyed by {__MODULE__, lb_name, :weights}.
  • The expanded node list (the weight map projected into a flat list, one entry per weight unit) lives in WeightedRoundRobinCache (ETS). It's rebuilt on every on_node_change so storing it in PT would trigger continuous global GC in clusters with steady flapping; ETS writes are local and lock-free under write_concurrency: true.

Steady-state hot path is one ETS lookup, one atomic counter increment, and an Enum.at/2 into the cached expanded list.

Usage

RpcLoadBalancer.LoadBalancer.start_link(
  name: :my_balancer,
  selection_algorithm: RpcLoadBalancer.LoadBalancer.SelectionAlgorithm.WeightedRoundRobin,
  algorithm_opts: [weights: %{:"node1@host" => 3, :"node2@host" => 1}]
)

Nodes not present in the weight map receive a default weight of 1.