-
Notifications
You must be signed in to change notification settings - Fork 64
/
cluster.go
65 lines (61 loc) · 1.32 KB
/
cluster.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
63
64
65
package armor
import (
"log"
"net"
"strconv"
"github.com/hashicorp/logutils"
"github.com/hashicorp/serf/serf"
)
// Events
const (
EventPluginLoad = "1"
EventPluginUpdate = "2"
)
func (a *Armor) StartCluster() {
conf := serf.DefaultConfig()
filter := &logutils.LevelFilter{
Levels: []logutils.LogLevel{"DEBUG", "INFO", "WARN", "ERROR"},
MinLevel: logutils.LogLevel("WARN"),
Writer: a.Logger.Output(),
}
conf.MemberlistConfig.Logger = log.New(filter, "", log.LstdFlags)
conf.LogOutput = filter
conf.NodeName = a.Name
host, port, err := net.SplitHostPort(a.Cluster.Address)
if err != nil {
a.Logger.Fatal(err)
}
if host == "" {
host = "127.0.0.1"
}
p, err := strconv.Atoi(port)
if err != nil {
a.Logger.Fatal(err)
}
conf.MemberlistConfig.BindAddr = host
conf.MemberlistConfig.BindPort = p
a.Cluster.Serf, err = serf.Create(conf)
if err != nil {
a.Logger.Fatal(err)
}
a.Cluster.Join(a.Cluster.Peers, true)
ch := make(chan serf.Event, 64)
conf.EventCh = ch
for {
select {
case e := <-ch:
switch t := e.(type) {
case serf.UserEvent:
switch t.Name {
case EventPluginLoad, EventPluginUpdate:
id := string(t.Payload)
p, err := a.Store.FindPlugin(id)
if err != nil {
a.Logger.Error(err)
}
a.LoadPlugin(p, t.Name == EventPluginUpdate)
}
}
}
}
}