Node selection strategies for distributed RPC calls.
Supports both static node lists and dynamic node discovery through MFA (Module, Function, Arguments).
Selection Strategies
:random- Randomly picks a node on each call.:round_robin- Circular distribution, tracked per-process.:hash- Consistent hashing on function arguments.Same args always route to the same node.
Sticky Nodes
When sticky_node: true, a process sticks to the first node it selects
for all subsequent calls (stored in the process dictionary).
Node Configuration
- Static list:
nodes: [:node1@host, :node2@host] - Dynamic MFA:
nodes: {MyModule, :get_nodes, []}
Examples
selector = NodeSelector.new([:n1@host, :n2@host], :my_id, :random)
selector = NodeSelector.new(
{ClusterHelper, :get_nodes, [:backend]},
:backend_selector,
:round_robin,
true
)
node = NodeSelector.select_node(selector, ["user_123"])
#=> :n1@host
Summary
Functions
Clears the sticky node for the current process.
Loads a NodeSelector from application config.
Creates and validates a new NodeSelector.
Raises EasyRpc.Error if any option is invalid.
Resets the round-robin counter for the current process.
Selects a node using the configured strategy.
Updates the selector's :id field.
Types
@type node_list() :: [node()]
@type selector_id() :: term()
@type strategy() :: :random | :round_robin | :hash
@type t() :: %EasyRpc.NodeSelector{ id: selector_id(), nodes_or_mfa: nodes_config(), sticky_node: boolean(), strategy: strategy() }
Functions
@spec clear_sticky_node(t()) :: :ok
Clears the sticky node for the current process.
Loads a NodeSelector from application config.
Expected format:
config :my_app, :remote_nodes,
nodes: [:node1@host, :node2@host],
select_mode: :round_robin,
sticky_node: falseRaises EasyRpc.Error if config is missing or invalid.
@spec new(nodes_config(), selector_id(), strategy(), boolean()) :: t()
Creates and validates a new NodeSelector.
Raises EasyRpc.Error if any option is invalid.
@spec reset_round_robin(t()) :: :ok
Resets the round-robin counter for the current process.
Selects a node using the configured strategy.
- Respects sticky-node state stored in the process dictionary.
datais only used by the:hashstrategy.
Raises EasyRpc.Error when no nodes are available.
@spec update_id(t(), selector_id()) :: t()
Updates the selector's :id field.