This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
observe.go
69 lines (59 loc) · 1.82 KB
/
observe.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
package canoe
import (
"sync/atomic"
)
// Observation is sent out to each observer.
// An obeservation can have many different types.
// It is currently used to detect the successful addition of a node to
// a cluster during the cluster join or bootstrap phase
type Observation interface{}
// FilterFn is a function used to filter what events an Observer gets piped
type FilterFn func(o Observation) bool
var nextObserverID uint64
// Observer is a struct responsible for monitoring raft's internal operations if one
// wants to perform unpredicted operations
type Observer struct {
channel chan Observation
filter FilterFn
id uint64
}
// NewObserver gets an observer. Note, if you aren't actively consuming the observer,
// the observations will get lost
func NewObserver(channel chan Observation, filter FilterFn) *Observer {
return &Observer{
channel: channel,
filter: filter,
id: atomic.AddUint64(&nextObserverID, 1),
}
}
func (rn *Node) observe(data Observation) {
rn.observersLock.RLock()
defer rn.observersLock.RUnlock()
for _, observer := range rn.observers {
if observer.filter != nil && !observer.filter(interface{}(data).(Observation)) {
continue
}
if observer.channel == nil {
continue
}
// make sure we don't block if consumer isn't consuming fast enough
select {
case observer.channel <- data:
continue
default:
continue
}
}
}
// RegisterObserver registers and begins to send observations down an Observer
func (rn *Node) RegisterObserver(o *Observer) {
rn.observersLock.Lock()
defer rn.observersLock.Unlock()
rn.observers[o.id] = o
}
// UnregisterObserver is called when one no longer needs to look for a particular raft event occuring
func (rn *Node) UnregisterObserver(o *Observer) {
rn.observersLock.Lock()
defer rn.observersLock.Unlock()
delete(rn.observers, o.id)
}