/
eventDelegate.go
82 lines (71 loc) · 2.13 KB
/
eventDelegate.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package gossip
import (
"encoding/json"
"github.com/hashicorp/memberlist"
"go.uber.org/zap"
)
type (
// EventDelegate is a simpler delegate that is used only to receive
// notifications about members joining and leaving. The methods in this
// delegate may be called by multiple goroutines, but never concurrently.
// This allows you to reason about ordering.
EventDelegate struct {
debug bool
logger *zap.Logger
localNodeName string
joinCh chan uint16
leaveCh chan uint16
}
)
func newEventDelegate(debug bool, logger *zap.Logger, lnn string, joinCh, leaveCh chan uint16) *EventDelegate {
return &EventDelegate{
debug: debug,
logger: logger,
localNodeName: lnn,
joinCh: joinCh,
leaveCh: leaveCh,
}
}
// NotifyJoin is invoked when a node is detected to have joined.
// The Node argument must not be modified.
// - stop workers
// - assemble
// - elect leader
// - distribute load
// - start workers
func (d *EventDelegate) NotifyJoin(node *memberlist.Node) {
if d.localNodeName == node.Name {
return
}
if d.debug {
d.logger.Debug("--- gossip.EventDelegate.NotifyJoin()",
zap.String("localNode.Name", d.localNodeName),
zap.String("node.Name", node.Name))
}
var nodeMeta NodeMeta
if err := json.Unmarshal(node.Meta, &nodeMeta); err != nil {
d.logger.Fatal("gossip.NotifyJoin() json.Unmarshal()",
zap.String("localNode.Name", d.localNodeName),
zap.String("node.Name", node.Name),
zap.ByteString("node.Meta", node.Meta))
}
d.joinCh <- nodeMeta.NodeID
}
// NotifyLeave is invoked when a node is detected to have left.
// The Node argument must not be modified.
func (d *EventDelegate) NotifyLeave(node *memberlist.Node) {
if d.debug {
d.logger.Debug("event_delegate", zap.String("node_name", node.Name))
}
var nodeMeta NodeMeta
if err := json.Unmarshal(node.Meta, &nodeMeta); err != nil {
panic(err)
}
d.leaveCh <- nodeMeta.NodeID
}
// NotifyUpdate is invoked when a node is detected to have
// updated, usually involving the metadata. The Node argument
// must not be modified.
func (d *EventDelegate) NotifyUpdate(node *memberlist.Node) {
return
}