-
Notifications
You must be signed in to change notification settings - Fork 0
/
membership_hooks.go
33 lines (27 loc) · 1023 Bytes
/
membership_hooks.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
package go_serfly
import (
"github.com/far4599/go-serfly/types"
"github.com/hashicorp/serf/serf"
)
// AddMessageListener adds a callback to handle a query message sent by Broadcast method
func (m *Membership) AddMessageListener(msgType string, cb func(*Membership, *serf.Query)) {
m.messageListeners[msgType] = append(m.messageListeners[msgType], cb)
}
// AddMemberEventListener adds a callback to handle member's raft status change
func (m *Membership) AddMemberEventListener(eventType types.MemberEventType, cb func(*Membership, *serf.Member)) {
m.memberEventListeners[eventType] = append(m.memberEventListeners[eventType], cb)
}
func (m *Membership) onMessageHook(msgType string, query *serf.Query) {
for _, fn := range m.messageListeners[msgType] {
fn(m, query)
}
}
func (m *Membership) onMemberEventHook(eventType types.MemberEventType, member *serf.Member) {
if member == nil {
_m := m.serf.LocalMember()
member = &_m
}
for _, fn := range m.memberEventListeners[eventType] {
fn(m, member)
}
}