-
Notifications
You must be signed in to change notification settings - Fork 1
/
future.go
67 lines (58 loc) · 1.03 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
package raft
import (
"github.com/dzdx/raft/raftpb"
"context"
"fmt"
)
type RespWithError struct {
Resp interface{}
Err error
}
type future struct {
respChan chan RespWithError
}
func (future *future) init() {
future.respChan = make(chan RespWithError, 1)
}
func (future *future) Respond(resp interface{}, err error) {
select {
case future.respChan <- RespWithError{
Resp: resp,
Err: err,
}:
default:
}
}
func (future *future) Response() <-chan RespWithError {
return future.respChan
}
type ApplyFuture struct {
future
Entry *raftpb.LogEntry
ctx context.Context
}
type DataFuture struct {
future
Data []byte
}
type IndexFuture struct {
future
Index uint64
}
type ConfChangeFuture struct {
future
action *raftpb.ConfChange
}
func (f *ConfChangeFuture) String() string {
var t string
if f.action.Type == raftpb.ConfChange_RemoveNode {
t = "remove"
} else {
t = "add"
}
var r string
if f.action.Role == raftpb.NodeRole_Voter {
r = "voter"
}
return fmt.Sprintf("req: %s %s node %s", t, r, f.action.ServerID)
}