-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (49 loc) · 1.17 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
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/davidmontoyago/go-grpc-gossiping-cluster/cluster"
)
func main() {
var errors []chan error
node1 := cluster.NewNode("node1", "127.0.0.1", 9000, 7900, "")
errors = append(errors, node1.Start())
// give first node a break
time.Sleep(1 * time.Second)
node2 := cluster.NewNode("node2", "127.0.0.1", 9001, 7901, "localhost:7900")
errors = append(errors, node2.Start())
node3 := cluster.NewNode("node3", "127.0.0.1", 9002, 7902, "localhost:7901")
errors = append(errors, node3.Start())
// agregate nodes errors into a single channel
agg := make(chan error)
for _, errChan := range errors {
go func(c chan error) {
for msg := range c {
agg <- msg
}
}(errChan)
}
shutdown := make(chan os.Signal)
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-shutdown
log.Println("shutting down...")
node1.Shutdown()
node2.Shutdown()
node3.Shutdown()
log.Println("all nodes shutdown... exiting now.")
os.Exit(0)
}()
select {
case err := <-agg:
fmt.Println(err)
node1.Shutdown()
node2.Shutdown()
node3.Shutdown()
os.Exit(1)
}
}