View Source NoNoncense.MachineId (NoNoncense v0.0.5)
Determine unique machine IDs for nodes, which can be used to guarantee uniqueness in a distributed system.
To determine the unique node ID, you must provide a list of possible node identifiers. The possible identifiers are IP addresses, OTP node identifiers, hostnames and fully-qualified domain names. You should only provide possible identifiers that can't conflict with one another. So if you don't explicitly set OTP node names you should not add the default :"nonode@nohost"
to your identifier list.
Use the same node list everywhere
Your node_list
must be the same for every node or the generated machine IDs will not be unique.
You can configure the options in your application environment and just pass them into id/1
.
Summary
Functions
Get a list of all identifiers of the current node. You can use one or more of these values to populate your node list.
Determine the current node's machine ID.
Types
@type id_opts() :: [ machine_id: non_neg_integer() | nil, node_list: host_identifiers(), max_nodes: pos_integer() ]
Functions
@spec host_identifiers() :: host_identifiers()
Get a list of all identifiers of the current node. You can use one or more of these values to populate your node list.
Examples / doctests
iex> MachineId.host_identifiers()
[:nonode@nohost, "host.mydomain.com", "10.11.12.13", "myhost", "fe80::1234::abcd"]
@spec id!(id_opts()) :: non_neg_integer()
Determine the current node's machine ID.
Examples / doctests
# provide a list of possible node identifiers
iex> node_list = ["1.1.1.1", "127.0.0.1", "8.8.8.8", "0.0.0.0"]
iex> MachineId.id!(node_list: node_list)
2
# a statically configured ID will override the node list
iex> node_list = ["1.1.1.1", "127.0.0.1", "8.8.8.8", "0.0.0.0"]
iex> MachineId.id!(machine_id: 1, node_list: node_list)
1
# the node ID must be within the provided range (default 0-1023)
iex> node_list = ["1.1.1.1", "127.0.0.1", "8.8.8.8", "0.0.0.0"]
iex> MachineId.id!(max_nodes: 2, node_list: node_list)
** (RuntimeError) Node ID 2 out of range 0-1
# raises when the machine ID could not be determined from the node list
iex> node_list = ["1.1.1.1"]
iex> MachineId.id!(node_list: node_list)
** (RuntimeError) machine ID could not be determined