-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise.go
189 lines (154 loc) · 3.24 KB
/
promise.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package goutube
type RecordRequest struct {
Data []byte
}
type RecordResponse struct {
LastOff uint64
Response interface{}
}
type FSMRecordResponse struct {
StoreKey interface{}
StoreValue interface{}
Response interface{}
}
type Promise interface {
Error() error
Response() interface{}
}
type promiseError struct {
err error
errCh chan error
responded bool
ShutdownCh chan struct{}
}
func (p *promiseError) init() {
p.errCh = make(chan error, 1)
}
func (p *promiseError) Error() error {
if p.err != nil {
return p.err
}
if p.errCh == nil {
panic("waiting for response on nil channel")
}
select {
case p.err = <-p.errCh:
case <-p.ShutdownCh:
p.err = ErrArcShutdown
}
return p.err
}
func (p *promiseError) respondError(err error) {
if p.responded {
return
}
if p.errCh != nil {
p.errCh <- err
close(p.errCh)
}
p.responded = true
}
type RecordPromise struct {
promiseError
req *RecordRequest
resp *RecordResponse
}
func (c *RecordPromise) init() {
c.promiseError.init()
}
func (c *RecordPromise) Request() *RecordRequest {
return c.req
}
func (c *RecordPromise) Response() interface{} {
return c.resp
}
func (c *RecordPromise) respond(resp interface{}) *RecordPromise {
c.resp.Response = resp
return c
}
// RPCResponse captures both a response and a potential error.
type RPCResponse struct {
Response interface{}
Error error
}
// RPC has a command, and provides a response mechanism.
type RPC struct {
Command interface{}
RespChan chan<- RPCResponse
}
// Respond is used to respondError with a response, error or both
func (r *RPC) Respond(resp interface{}, err error) {
r.RespChan <- RPCResponse{resp, err}
}
type ReplicateCommandPromise struct {
req *RecordRequest
expected interface{}
resp *RecordResponse
}
func NewReplicateCommandPromise(req *RecordRequest, expected interface{}) *ReplicateCommandPromise {
return &ReplicateCommandPromise{
req: req,
expected: expected,
}
}
type shutdownPromise struct {
arc *Arc
}
func (s *shutdownPromise) Error() error {
if s.arc == nil {
return nil
}
s.arc.waitShutdown()
return nil
}
func (s *shutdownPromise) Response() interface{} {
return nil
}
type RecordEntriesRequest struct {
Entries []*RecordRequest
}
type RecordEntriesResponse struct {
LastOff uint64
Response interface{}
}
type RecordEntriesPromise struct {
promiseError
req *RecordEntriesRequest
resp *RecordEntriesResponse
}
func (c *RecordEntriesPromise) init() {
c.promiseError.init()
}
func (c *RecordEntriesPromise) Request() *RecordEntriesRequest {
return c.req
}
func (c *RecordEntriesPromise) Response() interface{} {
return c.resp
}
func (c *RecordEntriesPromise) respond(resp interface{}) *RecordEntriesPromise {
c.resp.Response = resp
return c
}
type GetServersRequest struct {
}
type GetServersResponse struct {
Response interface{}
}
type GetServersPromise struct {
promiseError
req *GetServersRequest
resp *GetServersResponse
}
func (c *GetServersPromise) init() {
c.promiseError.init()
}
func (c *GetServersPromise) Request() *GetServersRequest {
return c.req
}
func (c *GetServersPromise) Response() interface{} {
return c.resp
}
func (c *GetServersPromise) respond(resp interface{}) *GetServersPromise {
c.resp.Response = resp
return c
}