https://github.com/upsight/dinghy
Dinghy implements leader election using the raft protocol
https://github.com/upsight/dinghy
go golang leader-election raft
Last synced: 2 months ago
JSON representation
Dinghy implements leader election using the raft protocol
- Host: GitHub
- URL: https://github.com/upsight/dinghy
- Owner: upsight
- License: mit
- Created: 2017-02-11T21:24:18.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-09T21:11:27.000Z (over 7 years ago)
- Last Synced: 2024-06-19T02:08:31.631Z (over 1 year ago)
- Topics: go, golang, leader-election, raft
- Language: Go
- Homepage:
- Size: 204 KB
- Stars: 13
- Watchers: 7
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# dinghy [](http://godoc.org/github.com/upsight/dinghy) [](https://travis-ci.org/upsight/dinghy)
Dinghy implements leader election using part of the raft protocol. It might
be useful if you have several workers but only want one of them at a time
doing things.```
package mainimport (
"flag"
"fmt"
"log"
"net/http"
"os"
"strings""github.com/upsight/dinghy"
)func main() {
addr := flag.String("addr", "localhost:8899", "The address to listen on.")
nodesList := flag.String("nodes", "localhost:8898,localhost:8897", "Comma separated list of host:port")
flag.Parse()nodes := strings.Split(*nodesList, ",")
nodes = append(nodes, *addr)onLeader := func() error {
fmt.Println("leader")
return nil
}
onFollower := func() error {
fmt.Println("me follower")
return nil
}din, err := dinghy.New(
*addr,
nodes,
onLeader,
onFollower,
&dinghy.LogLogger{Logger: log.New(os.Stderr, "logger: ", log.Lshortfile)},
dinghy.DefaultElectionTickRange,
dinghy.DefaultHeartbeatTickRange,
)
if err != nil {
log.Fatal(err)
}
for _, route := range din.Routes() {
http.HandleFunc(route.Path, route.Handler)
}
go func() {
if err := din.Start(); err != nil {
log.Fatal(err)
}
}()
log.Fatal(http.ListenAndServe(*addr, nil))
}
```