https://github.com/pteich/clusterleader
⚖️Cluster Leader Election using Consul distributed locks
https://github.com/pteich/clusterleader
cluster clustering consul leader-election
Last synced: 7 months ago
JSON representation
⚖️Cluster Leader Election using Consul distributed locks
- Host: GitHub
- URL: https://github.com/pteich/clusterleader
- Owner: pteich
- License: mit
- Created: 2019-01-31T08:33:31.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-10-22T21:14:21.000Z (almost 2 years ago)
- Last Synced: 2025-01-15T15:19:43.779Z (9 months ago)
- Topics: cluster, clustering, consul, leader-election
- Language: Go
- Homepage:
- Size: 27.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Cluster leader election using Consul distributed locks
```go
package mainimport (
"log"
"time""github.com/hashicorp/consul/api"
"github.com/pteich/clusterleader"
)func main() {
config := api.DefaultConfig()
config.Address = "127.0.0.1:8500"
client, err := api.NewClient(config)
clusterLeader, err := clusterleader.NewClusterleader(client, "testkey", "localhost", 15*time.Second)
if err != nil {
log.Fatal(err)
}go func() {
for err := range clusterLeader.Errors() {
log.Print(err)
}
}()// as long as this loop runs we try to become leader
// an isElected event is send on every state change
// best would be to run this loop in a go routine and
// react to whatever we are leader or not
for isElected := range clusterLeader.Election() {
if isElected {
// we are leader
} else {
// not leader
}
}}
```