https://github.com/tomtaylor/multibully
Go library for distributed leader election on a multicast network (like a LAN)
https://github.com/tomtaylor/multibully
bully bully-algorithm lan leader-election multicast
Last synced: 3 months ago
JSON representation
Go library for distributed leader election on a multicast network (like a LAN)
- Host: GitHub
- URL: https://github.com/tomtaylor/multibully
- Owner: tomtaylor
- Created: 2018-01-21T19:08:20.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-01-21T19:24:14.000Z (over 7 years ago)
- Last Synced: 2025-03-21T23:33:58.357Z (3 months ago)
- Topics: bully, bully-algorithm, lan, leader-election, multicast
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 5
- Watchers: 1
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MultiBully
MultiBully is a Go library for distributed leadership election on a UDP multicast enabled network, like a LAN. It uses the [Bully algorithm](https://en.wikipedia.org/wiki/Bully_algorithm), a relatively simply mechanism which may not be suitable for a large numbers of nodes.
I think there are some bugs and inefficiencies in my implementation of it. It always manages to converge on the correct leader, but it sometimes takes a few goes round. Please feel free to help!
## Usage
The Bully algorithm elects the node with the largest `pid` to be leader. The `pid` doesn't necessarily need to be the process ID in the operating system – you could choose for this to be a timestamp or a fixed integer for each node. In this implementation, the combination of `pid` and IP address must be unique, so you can run multiple instances on a single host.
While MultiBully uses multicast UDP for communication between nodes, it transmits the non-multicast IP of each node in the communication packet, so you can point the follower at the new leader.
## Example
```go
package mainimport (
"github.com/tomtaylor/multibully"
"log"
"os"
"net"
)func main() {
address := "224.0.0.0:9999"
iface := "en0"
stop := make(chan struct{})
pid := uint64(os.Getpid())p, err := multibully.NewParticipant(address, iface, pid, func(state int, ip *net.IP) {
switch state {
case multibully.Follower:
log.Println("* Became Follower of", ip)
case multibully.Leader:
log.Println("* Became Leader")
}
})if err != nil {
log.Fatal(err)
}go p.StartElection()
p.RunLoop(stop)
}
```## Thanks
Thanks to [`oaStuff`](https://github.com/oaStuff/leaderElection) for their sample code which helped me understand how to implement the Bully algorithm.