https://github.com/octu0/bully-election
memberlist based Bully Leader Election
https://github.com/octu0/bully-election
bully-algorithm clustering leader-election membership-management
Last synced: 4 months ago
JSON representation
memberlist based Bully Leader Election
- Host: GitHub
- URL: https://github.com/octu0/bully-election
- Owner: octu0
- License: mit
- Created: 2024-10-19T10:59:50.000Z (8 months ago)
- Default Branch: master
- Last Pushed: 2024-10-29T05:09:58.000Z (8 months ago)
- Last Synced: 2024-11-08T09:12:39.822Z (8 months ago)
- Topics: bully-algorithm, clustering, leader-election, membership-management
- Language: Go
- Homepage: https://pkg.go.dev/github.com/octu0/bully-election
- Size: 85.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `bully-election`
[](https://github.com/octu0/bully-election/blob/master/LICENSE)
[](https://pkg.go.dev/github.com/octu0/bully-election)
[](https://goreportcard.com/report/github.com/octu0/bully-election)
[](https://github.com/octu0/bully-election/releases)Hashicorp's [memberlist](https://github.com/hashicorp/memberlist) based [Bully Leader Election](https://en.wikipedia.org/wiki/Bully_algorithm).
Features:
- Simple API
- Voter / Nonvoter node state management(monitoring)
- TransferLeadership## Installation
```bash
go get github.com/octu0/bully-election
```## Example
```go
package mainimport (
"context"
"log"
"time""github.com/hashicorp/memberlist"
"github.com/octu0/bully-election"
)func main() {
ctx := context.Background()
conf := memberlist.DefaultLANConfig()
conf.Name = "node1"
conf.BindPort = 7947
conf.AdvertiseAddr = "10.0.0.123"
conf.AdvertisePort = conf.BindPortb, err := bullyelection.CreateVoter(ctx, conf,
WithElectionTimeout(1*time.Second),
WithObserveFunc(func(b *bullyelection.Bully, evt bullyelection.NodeEvent, id, addr string) {
log.Printf("[%s] event: %s node=%s(%s)", b.ID(), evt.String(), id, addr)
if evt == bullyelection.ElectionEvent {
for _, n := range b.Members() {
log.Printf("%s is_leader=%v", n.ID(), n.IsLeader())
}
}
}),
WithOnErrorFunc(func(err error) {
log.Printf("error=%+v", err)
}),
)
err := b.Join("10.0.0.1")
b.IsLeader()
b.UpdateMetadata([]byte("hello world"))nn, _ := bullyelection.CreateNonVoter(ctx, conf2)
err := nn.Join("10.0.0.1")
for _, m := range nn.Members() {
_ = m.UserMetadata()
}b.Leave()
}
```# License
MIT, see LICENSE file for details.