NoNoncense.MachineId.ConflictGuard (NoNoncense v2.2.0)

Copy Markdown View Source

Checks machine IDs with newly connected Erlang nodes and calls :on_conflict when this guard is the newer of two guards with the same ID.

When to use

The guard is most useful with static strategies such as NoNoncense.MachineId.Strategy.HostIdentifiers and NoNoncense.MachineId.Strategy.EnvironmentVariable, which do not coordinate IDs externally.

Requires distributed Erlang

ConflictGuard only works when nodes are connected to each other via distributed Erlang (i.e. started with --name / --sname and a shared cookie). Without it, the conflict checks never run. Guards on peer nodes must use the same registered name. Checks occur on connection, not continuously or when a machine ID changes.

Usage

NoNoncense.MachineId starts ConflictGuard by default. A conflict marks the local lease as lost, disabling factories and triggering the configured :on_lease_lost callback:

{NoNoncense.MachineId,
 strategy: NoNoncense.MachineId.Strategy.HostIdentifiers,
 strategy_opts: [node_list: [:"myapp@10.0.0.1", :"myapp@10.0.0.2"]],
 on_lease_lost: fn reason -> Logger.critical(inspect(reason)) end,
 instances: [[base_key: System.fetch_env!("BASE_KEY")]]}

Pass enable_conflict_guard?: false to disable it.

To run it on its own, supply :machine_id as an integer or zero-arity function. The :on_conflict callback defaults to halting the node via :erlang.halt(111):

{NoNoncense.MachineId.ConflictGuard,
 machine_id: fn -> NoNoncense.MachineId.Strategy.HostIdentifiers.id!(node_list: nodes) end,
 on_conflict: fn -> System.stop(1) end}

Guard start times use the system clock; equal timestamps cause both guards to act.

Summary

Functions

Returns a specification to start this module under a supervisor.

Starts the conflict guard.

Types

opt()

@type opt() ::
  {:name, GenServer.name()}
  | {:on_conflict, (-> any())}
  | {:machine_id, integer() | (-> integer())}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

start_link(opts \\ [])

@spec start_link([opt()]) :: GenServer.on_start()

Starts the conflict guard.