forked from hashicorp/raft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuture.go
108 lines (90 loc) · 1.92 KB
/
future.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package raft
import (
"net"
"time"
)
// Future is used to represent an action that may occur in the future
type Future interface {
Error() error
}
// ApplyFuture is used for Apply() and can returns the FSM response
type ApplyFuture interface {
Future
Response() interface{}
}
// errorFuture is used to return a static error
type errorFuture struct {
err error
}
func (e errorFuture) Error() error {
return e.err
}
func (e errorFuture) Response() interface{} {
return nil
}
// deferError can be embedded to allow a future
// to provide an error in the future
type deferError struct {
err error
errCh chan error
}
func (d *deferError) init() {
d.errCh = make(chan error, 1)
}
func (d *deferError) Error() error {
if d.err != nil {
return d.err
}
if d.errCh == nil {
panic("waiting from response on nil channel")
}
d.err = <-d.errCh
return d.err
}
func (d *deferError) respond(err error) {
if d.errCh == nil {
return
}
d.errCh <- err
close(d.errCh)
}
// logFuture is used to apply a log entry and waits until
// the log is considered committed
type logFuture struct {
deferError
log Log
policy quorumPolicy
response interface{}
}
func (l *logFuture) Response() interface{} {
return l.response
}
type shutdownFuture struct {
raft *Raft
}
func (s *shutdownFuture) Error() error {
for s.raft.getRoutines() > 0 {
time.Sleep(5 * time.Millisecond)
}
return nil
}
// snapshotFuture is used for waiting on a snapshot to complete
type snapshotFuture struct {
deferError
}
// reqSnapshotFuture is used for requesting a snapshot start.
// It is only used internally
type reqSnapshotFuture struct {
deferError
// snapshot details provided by the FSM runner before responding
index uint64
term uint64
peers []net.Addr
snapshot FSMSnapshot
}
// restoreFuture is used for requesting an FSM to perform a
// snapshot restore. Used internally only.
type restoreFuture struct {
deferError
ID string
}