An OTP application for leader election.
The nbully application starts a supervisor that supervises a worker process. This process runs in every node and consensuates node leadership with its homologues in other nodes.
In your rebar.config
file, add the dependency:
{deps, [
{nbully, {git, "git@github.com:nomasystems/nbully.git", {branch, "main"}}}
]}.
Then, once the application is started, you can consult nbully:leader/0
to know which node is the current leader. This value is the same for all nodes connected through net_kernel
and might only change when nodes go up or down.
You can also use nbully:subscribe/0
to receive live updates when the leader changes in the form of a message {nbully_leader_update, Node}
. For example:
ok = nbully:subscribe(),
InitialLeader = nbully:leader(),
% [...]
NewLeader =
receive
{nbully_leader_update, Node} -> Node
after 1000 -> InitialLeader
end,
LeaderIsMe = node() =:= NewLeader,
% [...]
ok = nbully:unsubscribe().
Any doubt or suggestion? Please, check out our issue tracker.
Pull requests are welcome. Please read the contributing guidelines to know more about contribution.