https://github.com/linka-cloud/leaderelection
https://github.com/linka-cloud/leaderelection
cloud-computing distributed-systems git gossip kubernetes leader-election memberlist s3
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/linka-cloud/leaderelection
- Owner: linka-cloud
- License: apache-2.0
- Created: 2023-07-25T15:40:39.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-08-08T14:39:40.000Z (almost 2 years ago)
- Last Synced: 2024-12-30T07:20:21.066Z (5 months ago)
- Topics: cloud-computing, distributed-systems, git, gossip, kubernetes, leader-election, memberlist, s3
- Language: Go
- Homepage:
- Size: 161 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# leaderelection
[](https://golang.org/)
[](https://pkg.go.dev/go.linka.cloud/leaderelection)**Project status: *alpha***
*The API, spec, status and other user facing objects are subject to change.*
*We do not support backward-compatibility for the alpha releases.*## Overview
This module is extracted from the [k8s.io/client-go](https://github.com/kubernetes/client-go) package.
It provides a simple interface for implementing leader election in a distributed system using different backends:
- [kubernetes](k8s): using [Lease](https://kubernetes.io/docs/concepts/architecture/leases/)
(Kubernetes API)[](https://pkg.go.dev/go.linka.cloud/leaderelection/k8s)
- [gossip](gossip): using distributed in memory key-value store (based on [hashicorp/memberlist](https://github.com/hashicorp/memberlist))
[](https://pkg.go.dev/go.linka.cloud/leaderelection/gossip)
- [s3](s3): using a simple json file stored in a s3 bucket
[](https://pkg.go.dev/go.linka.cloud/leaderelection/s3)
- [git](git): using a simple json file stored in a git repository
[](https://pkg.go.dev/go.linka.cloud/leaderelection/git)## Usage
```go
package mainimport (
"context"
"time"
"k8s.io/apimachinery/pkg/util/rand"
le "go.linka.cloud/leaderelection"
"go.linka.cloud/leaderelection/[backend]"
)const (
lockName = "test"
)func main() {
ctx, cancel := context.WithCancel(le.SetupSignalHandler())
defer cancel()
l, err := [backend].New(ctx, lockName, rand.String(8), ...)
if err != nil {
logrus.Fatal(err)
}
config := le.Config{
Lock: l,
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
RetryPeriod: 2 * time.Second,
ReleaseOnCancel: true,
Name: lockName,
Callbacks: le.Callbacks{
OnStartedLeading: func(ctx context.Context) {
logrus.Info("started leading")
},
OnStoppedLeading: func() {
logrus.Info("stopped leading")
},
OnNewLeader: func(identity string) {
logrus.Infof("new leader: %s", identity)
},
},
}
e, err := le.New(config)
if err != nil {
logrus.Fatal(err)
}
e.Run(ctx)
}
```