https://github.com/danl5/goelect
Goelect is an open-source, self-contained Golang library designed to facilitate leader election within a distributed system.
https://github.com/danl5/goelect
elect election golang non-depen self-contained
Last synced: 3 months ago
JSON representation
Goelect is an open-source, self-contained Golang library designed to facilitate leader election within a distributed system.
- Host: GitHub
- URL: https://github.com/danl5/goelect
- Owner: danl5
- License: apache-2.0
- Created: 2024-01-13T14:05:21.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-13T05:33:34.000Z (almost 2 years ago)
- Last Synced: 2024-07-13T06:38:27.194Z (almost 2 years ago)
- Topics: elect, election, golang, non-depen, self-contained
- Language: Go
- Homepage:
- Size: 117 KB
- Stars: 217
- Watchers: 3
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://goreportcard.com/report/github.com/danl5/goelect) 
Goelect is an open-source Go (Golang) library for leader election. It is heavily influenced by the election component of the Raft implementation. For more details, you can refer to [Raft Wiki](https://en.wikipedia.org/wiki/Raft_(algorithm)).
## Features
* **Independent Operation**: No third-party services are required. You don't need to set up or rely on external systems like ZooKeeper or etcd.
* **Simplified Integration**: Easy to integrate into your existing Golang projects with minimal configuration.
* **Supports Novote role**:The no-vote node does not participate in the election.
* **Highly Available**: Built to be fault-tolerant, suitable for systems that require high availability.
## How to use
### Config
```go
// ElectConfig is a struct that represents the configuration for an election.
type ElectConfig struct {
// Timeout for heartbeat messages, in milliseconds
HeartBeatInterval uint
// Timeout for election messages, in milliseconds
ElectTimeout uint
// Timeout for connecting to peers, in seconds
ConnectTimeout uint
// List of peers in the network
Peers []Node
// Node represents the information of this node
Node Node
// State callbacks
CallBacks *StateCallBacks
// Timeout for callbacks, in seconds
CallBackTimeout int
}
```
### Example
`examples/onenode/node.go` is a great example of using this goelect package.
Create an Elect instance:
```go
// use the built-in RPC as the transport layer.
rpcTransport, err := rpc.NewRPC(logger)
if err != nil {
return nil, err
}
e, err := goelect.NewElect(
rpcTransport,
// rpc transport config
&rpc.Config{},
&goelect.ElectConfig{
ElectTimeout: 200,
HeartBeatInterval: 150,
ConnectTimeout: 10,
Peers: peerNodes,
// state transition callbacks
CallBacks: &goelect.StateCallBacks{
EnterLeader: enterLeader,
LeaveLeader: leaveLeader,
EnterFollower: enterFollower,
LeaveFollower: leaveFollower,
EnterCandidate: enterCandidate,
LeaveCandidate: leaveCandidate,
},
// self node
Node: goelect.Node{
Address: *nodeAddress,
ID: *nodeAddress,
},
}, logger)
```
Start the Elect:
```go
err = e.Run()
```
This is everything we need to do :)