Use atomic pub/sub for events

This commit is contained in:
Michael Crosby 2018-09-18 22:08:30 -04:00
parent 72db99191c
commit cfe9715911
2 changed files with 55 additions and 11 deletions

View file

@ -2,6 +2,7 @@ package element
import ( import (
"errors" "errors"
"sync"
"time" "time"
"github.com/hashicorp/memberlist" "github.com/hashicorp/memberlist"
@ -19,6 +20,8 @@ var (
// Agent represents the node agent // Agent represents the node agent
type Agent struct { type Agent struct {
*subscribers
config *Config config *Config
members *memberlist.Memberlist members *memberlist.Memberlist
peerUpdateChan chan bool peerUpdateChan chan bool
@ -35,6 +38,7 @@ func NewAgent(info *Peer, cfg *Config) (*Agent, error) {
nodeEventCh = make(chan *NodeEvent, 64) nodeEventCh = make(chan *NodeEvent, 64)
) )
a := &Agent{ a := &Agent{
subscribers: newSubscribers(),
config: cfg, config: cfg,
peerUpdateChan: updateCh, peerUpdateChan: updateCh,
nodeEventChan: nodeEventCh, nodeEventChan: nodeEventCh,
@ -62,14 +66,42 @@ func (a *Agent) SyncInterval() time.Duration {
return a.memberConfig.PushPullInterval return a.memberConfig.PushPullInterval
} }
func newSubscribers() *subscribers {
return &subscribers{
subs: make(map[chan *NodeEvent]struct{}),
}
}
type subscribers struct {
mu sync.Mutex
subs map[chan *NodeEvent]struct{}
}
// Subscribe subscribes to the node event channel // Subscribe subscribes to the node event channel
func (a *Agent) Subscribe(ch chan *NodeEvent) { func (s *subscribers) Subscribe() chan *NodeEvent {
go func() { ch := make(chan *NodeEvent, 64)
for { s.mu.Lock()
s.subs[ch] = struct{}{}
s.mu.Unlock()
return ch
}
// Unsubscribe removes the channel from node events
func (s *subscribers) Unsubscribe(ch chan *NodeEvent) {
s.mu.Lock()
delete(s.subs, ch)
s.mu.Unlock()
}
func (s *subscribers) send(e *NodeEvent) {
s.mu.Lock()
for ch := range s.subs {
// non-blocking send
select { select {
case evt := <-a.nodeEventChan: case ch <- e:
ch <- evt default:
} }
} }
}() s.mu.Unlock()
} }

View file

@ -19,21 +19,33 @@ const (
// NodeEvent stores the event type and node information // NodeEvent stores the event type and node information
type NodeEvent struct { type NodeEvent struct {
// EventType is the type of event fired // EventType is the type of event fired
EventType NodeEventType Type NodeEventType
// Node is the internal cluster node // Node is the internal cluster node
Node *memberlist.Node Node *memberlist.Node
} }
// NotifyJoin notifies when a node joins the cluster // NotifyJoin notifies when a node joins the cluster
func (a *Agent) NotifyJoin(n *memberlist.Node) { func (a *Agent) NotifyJoin(n *memberlist.Node) {
a.send(&NodeEvent{
Type: NodeJoin,
Node: n,
})
} }
// NotifyLeave notifies when a node leaves the cluster // NotifyLeave notifies when a node leaves the cluster
func (a *Agent) NotifyLeave(n *memberlist.Node) { func (a *Agent) NotifyLeave(n *memberlist.Node) {
delete(a.state.Peers, n.Name) delete(a.state.Peers, n.Name)
a.peerUpdateChan <- true a.peerUpdateChan <- true
a.send(&NodeEvent{
Type: NodeLeave,
Node: n,
})
} }
// NotifyUpdate notifies when a node is updated in the cluster // NotifyUpdate notifies when a node is updated in the cluster
func (a *Agent) NotifyUpdate(n *memberlist.Node) { func (a *Agent) NotifyUpdate(n *memberlist.Node) {
a.send(&NodeEvent{
Type: NodeUpdate,
Node: n,
})
} }