-
Notifications
You must be signed in to change notification settings - Fork 3
/
events.go
80 lines (68 loc) · 2.01 KB
/
events.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
package raft
// Event types represented in Raft
const (
UnknownEvent EventType = iota
ErrorEvent
CommitEvent
DropEvent
MessageEvent
VoteRequestEvent
VoteReplyEvent
AppendRequestEvent
AppendReplyEvent
CommitRequestEvent
AggregatedCommitRequestEvent
CommitReplyEvent
TimeoutEvent
HeartbeatTimeout
ElectionTimeout
)
// Names of event types
var eventTypeStrings = [...]string{
"unknown", "error", "entryCommitted", "entryDropped", "messageReceived",
"voteRequested", "voteReplied", "appendRequested", "appendReplied",
"commitRequested", "aggregatedCommitRequests", "commitReplied",
"timeout", "heartbeatTimeout", "electionTimeout",
}
//===========================================================================
// Event Types
//===========================================================================
// EventType is an enumeration of the kind of events that can occur.
type EventType uint16
// String returns the name of event types
func (t EventType) String() string {
if int(t) < len(eventTypeStrings) {
return eventTypeStrings[t]
}
return eventTypeStrings[0]
}
// Callback is a function that can receive events.
type Callback func(Event) error
//===========================================================================
// Event Definition and Methods
//===========================================================================
// Event represents actions that occur during consensus. Listeners can
// register callbacks with event handlers for specific event types.
type Event interface {
Type() EventType
Source() interface{}
Value() interface{}
}
// event is an internal implementation of the Event interface.
type event struct {
etype EventType
source interface{}
value interface{}
}
// Type returns the event type.
func (e *event) Type() EventType {
return e.etype
}
// Source returns the entity that dispatched the event.
func (e *event) Source() interface{} {
return e.source
}
// Value returns the current value associated with teh event.
func (e *event) Value() interface{} {
return e.value
}