-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (56 loc) · 1.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"flag"
"fmt"
"github.com/xpanvictor/raft/commons"
"github.com/xpanvictor/raft/configs"
raft "github.com/xpanvictor/raft/node"
"log"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
var (
done bool
rw = sync.Mutex{}
)
var (
nodesUrlFile = flag.String("nodes_url_file", "nodes.txt", "The file containing all nodes url")
newClient = flag.Bool("new_client", false, "To just add another client to a running system")
)
func main() {
flag.Parse()
log.Printf("Starting server")
ch := make(chan os.Signal)
// will create nodes
// creates the nodesUrlFile
file, err := os.OpenFile(*nodesUrlFile, os.O_RDWR, 0644)
if err != nil {
log.Fatalf("Unable to create nodes url file %v", err)
}
defer file.Close()
// Use the seed nodes to initiate the system
seedNodes := commons.ArrStrFromFile(file)
for i, addr := range seedNodes {
// avoid 0 for initial node
raft.NewNode(commons.NodeID(i+1), configs.ELECTION_TIMEOUT, addr, seedNodes, ch)
}
signal.Notify(ch, syscall.SIGINT)
signal.Notify(ch, syscall.SIGKILL)
caughtSignal := <-ch
log.Printf("Closing raft with %v", caughtSignal)
}
func periodic() {
// infinite loop that waits till done
for {
time.Sleep(1 * time.Second)
fmt.Println("Heartbeat")
rw.Lock()
if done {
return
}
rw.Unlock()
}
}