https://github.com/git-hulk/go-elect
https://github.com/git-hulk/go-elect
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/git-hulk/go-elect
- Owner: git-hulk
- License: apache-2.0
- Created: 2023-12-01T12:52:15.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-03-10T09:26:49.000Z (over 2 years ago)
- Last Synced: 2025-02-10T14:12:25.434Z (over 1 year ago)
- Language: Go
- Size: 56.6 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## go-elect
go-elect is a library for making the election process easier. It provides a simple interface for managing your distributed exclusive job execution,
and it supports multiple backends for storing the election state:
- [x] [Redis](https://redis.io/)
- [ ] [Etcd](https://etcd.io/)
- [ ] [Zookeeper](https://zookeeper.apache.org/)
- [ ] [MySQL](https://www.mysql.com/)
- [ ] [PostgreSQL](https://www.postgresql.org/)
And more to come...
## How to use
### Redis
```go
package main
import (
"github.com/redis/go-redis/v9"
"github.com/git-hulk/go-elect/elector/engine/store"
"github.com/git-hulk/go-elect/elector"
)
type CountRunner struct {
count atomic.Int32
}
func (r *CountRunner) Run(_ context.Context) error {
r.count.Inc()
time.Sleep(100 * time.Millisecond)
return nil
}
func main() {
ctx := context.Background()
redisClient := redis.NewClient(&redis.Options{Addr: "localhost:6379"})
redisStore := store.NewRedisStore(redisClient)
elector, err := elector.New(redisStore, "test-elector1-key", 3 * time.Second, &CountRunner{})
if err := elector.Run(ctx); err != nil {
// handle error
}
elector.Wait()
// use elector.Release() to release the election
}
```
## License