Skip to content

Commit

Permalink
Merge pull request #12 from danl5/readme
Browse files Browse the repository at this point in the history
update Readme
  • Loading branch information
danl5 authored Apr 22, 2024
2 parents 8104279 + 73ba1cf commit 42720fc
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 4 deletions.
65 changes: 63 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,63 @@
# goelect
A library for the election process with no third-party dependencies.
<h1 align="center" style="border-bottom: none">
<a><img alt="goelect" src="/docs/goelect-logo.svg"></a>
</h1>

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
* No third-party dependencies; it does not require third-party services such as ZooKeeper or Etcd.
* Supports ***Novote*** role which does not participate in the election.


## 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
e, err := goelect.NewElect(&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,
},
}, slog.Default())
```
Start the Elect:
```go
err = e.Run()
```
This is everything we need to do :)
163 changes: 163 additions & 0 deletions docs/goelect-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions elect.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ type ElectConfig struct {
ConnectTimeout uint
// List of peers in the network
Peers []Node
// Node information
// Node represents the information of this node
Node Node
// State callbacks
CallBacks *StateCallBacks
// Timeout for callbacks
// Timeout for callbacks, in seconds
CallBackTimeout int
}

Expand Down

0 comments on commit 42720fc

Please sign in to comment.