-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.go
75 lines (64 loc) · 1.32 KB
/
event.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
package bullyelection
import (
"context"
"github.com/pkg/errors"
)
type NodeEvent uint8
const (
JoinEvent NodeEvent = iota + 1
LeaveEvent
TransferLeadershipEvent
ElectionEvent
)
func (evt NodeEvent) String() string {
switch evt {
case JoinEvent:
return "join"
case LeaveEvent:
return "leave"
case TransferLeadershipEvent:
return "transfer_leadership"
case ElectionEvent:
return "election"
}
return "unknown event"
}
func (b *Bully) readNodeEventLoop(ctx context.Context, ch chan *nodeEventMsg) {
defer b.wg.Done()
for {
select {
case <-ctx.Done():
return
case msg := <-ch:
select {
case b.electionQueue <- msg:
// ok
default:
b.opt.onErrorFunc(errors.Wrapf(ErrBullyBusy, "maybe hangup election, drop: %v", msg))
}
}
}
}
func (b *Bully) electionRunLoop(ctx context.Context) {
defer b.wg.Done()
for {
select {
case <-ctx.Done():
return
case msg := <-b.electionQueue:
switch msg.evt {
case JoinEvent, LeaveEvent, TransferLeadershipEvent:
if msg.isVoter {
if err := b.startElection(ctx, msg.evt, msg.id); err != nil {
b.opt.onErrorFunc(errors.Wrapf(err, "election failure"))
continue
}
}
b.opt.observeFunc(b, msg.evt, msg.id, msg.addr)
case ElectionEvent:
// emit only
b.opt.observeFunc(b, msg.evt, msg.id, msg.addr)
}
}
}
}