-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
answerqueue.go
298 lines (272 loc) · 7.21 KB
/
answerqueue.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package capnp
import (
"context"
"errors"
"sync"
"capnproto.org/go/capnp/v3/exc"
)
// AnswerQueue is a queue of method calls to make after an earlier
// method call finishes. The queue is unbounded; it is the caller's
// responsibility to manage/impose backpressure.
//
// An AnswerQueue can be in one of three states:
//
// 1. Queueing. Incoming method calls will be added to the queue.
// 2. Draining, entered by calling Fulfill or Reject. Queued method
// calls will be delivered in sequence, and new incoming method calls
// will block until the AnswerQueue enters the Drained state.
// 3. Drained, entered once all queued methods have been delivered.
// Incoming methods are passthrough.
type AnswerQueue struct {
method Method
draining chan struct{} // closed while exiting queueing state
mu sync.Mutex
q []qent // non-nil while queueing
bases []base // set when drain starts. len(bases) >= 1
}
// qent is a single entry in an AnswerQueue.
type qent struct {
ctx context.Context
basis int // index in bases
path []PipelineOp
Recv
}
// base is a message target derived from applying a qent.
type base struct {
ready chan struct{} // closed after recv is assigned
recv func(context.Context, []PipelineOp, Recv) PipelineCaller
}
// NewAnswerQueue creates a new answer queue.
func NewAnswerQueue(m Method) *AnswerQueue {
return &AnswerQueue{
method: m,
// N.B. since q == nil denotes the draining state,
// we do have to allocate something here, even though
// the queue is an empty slice.
q: make([]qent, 0),
draining: make(chan struct{}),
}
}
// Fulfill empties the queue, delivering the method calls on the given
// pointer. After fulfill returns, pipeline calls will be immediately
// delivered instead of being queued.
func (aq *AnswerQueue) Fulfill(ptr Ptr) {
// Enter draining state.
aq.mu.Lock()
q := aq.q
aq.q = nil
aq.bases = make([]base, len(q)+1)
ready := make(chan struct{}) // TODO(soon): use more fine-grained signals
defer close(ready)
for i := range aq.bases {
aq.bases[i].ready = ready
}
aq.bases[0].recv = ImmediateAnswer(aq.method, ptr).PipelineRecv
close(aq.draining)
aq.mu.Unlock()
// Drain queue.
for i := range q {
ent := &q[i]
recv := aq.bases[ent.basis].recv
recv(ent.ctx, ent.path, ent.Recv)
}
}
// Reject empties the queue, returning errors on all the method calls.
func (aq *AnswerQueue) Reject(e error) {
if e == nil {
panic("AnswerQueue.reject(nil)")
}
// Enter draining state.
aq.mu.Lock()
q := aq.q
aq.q = nil
aq.bases = make([]base, len(q)+1)
ready := make(chan struct{})
close(ready)
for i := range aq.bases {
b := &aq.bases[i]
b.ready = ready
b.recv = func(_ context.Context, _ []PipelineOp, r Recv) PipelineCaller {
r.Reject(e) // TODO(soon): attach pipelined method info
return nil
}
}
close(aq.draining)
aq.mu.Unlock()
// Drain queue by rejecting.
for i := range q {
q[i].Reject(e) // TODO(soon): attach pipelined method info
}
}
func (aq *AnswerQueue) PipelineRecv(ctx context.Context, transform []PipelineOp, r Recv) PipelineCaller {
return queueCaller{aq, 0}.PipelineRecv(ctx, transform, r)
}
func (aq *AnswerQueue) PipelineSend(ctx context.Context, transform []PipelineOp, r Send) (*Answer, ReleaseFunc) {
return queueCaller{aq, 0}.PipelineSend(ctx, transform, r)
}
// queueCaller is a client that enqueues calls to an AnswerQueue.
type queueCaller struct {
aq *AnswerQueue
basis int
}
func (qc queueCaller) PipelineRecv(ctx context.Context, transform []PipelineOp, r Recv) PipelineCaller {
qc.aq.mu.Lock()
if len(qc.aq.bases) > 0 {
// Draining/drained.
qc.aq.mu.Unlock()
b := &qc.aq.bases[qc.basis]
select {
case <-b.ready:
case <-ctx.Done():
r.Reject(ctx.Err())
return nil
}
return b.recv(ctx, transform, r)
}
// Enqueue.
qc.aq.q = append(qc.aq.q, qent{
ctx: ctx,
basis: qc.basis,
path: transform,
Recv: r,
})
basis := len(qc.aq.q) - 1
qc.aq.mu.Unlock()
return queueCaller{aq: qc.aq, basis: basis}
}
func (qc queueCaller) PipelineSend(ctx context.Context, transform []PipelineOp, s Send) (*Answer, ReleaseFunc) {
ret := new(StructReturner)
r := Recv{
Method: s.Method,
Returner: ret,
ReleaseArgs: func() {},
}
if s.PlaceArgs != nil {
var err error
_, seg := NewMultiSegmentMessage(nil)
r.Args, err = NewRootStruct(seg, s.ArgsSize)
if err != nil {
return ErrorAnswer(s.Method, err), func() {}
}
if err = s.PlaceArgs(r.Args); err != nil {
return ErrorAnswer(s.Method, err), func() {}
}
r.ReleaseArgs = r.Args.Message().Release
}
pcall := qc.PipelineRecv(ctx, transform, r)
return ret.Answer(s.Method, pcall)
}
// A StructReturner implements Returner by allocating an in-memory
// message. It is safe to use from multiple goroutines. The zero value
// is a Returner in its initial state.
type StructReturner struct {
mu sync.Mutex // guards all fields
p *Promise // assigned at most once
alloced bool
released bool
msg *Message // assigned at most once
returned bool // indicates whether the below fields are filled in
result Struct // assigned at most once
err error // assigned at most once
}
func (sr *StructReturner) AllocResults(sz ObjectSize) (Struct, error) {
defer sr.mu.Unlock()
sr.mu.Lock()
if sr.alloced {
return Struct{}, errors.New("StructReturner: multiple calls to AllocResults")
}
sr.alloced = true
_, seg := NewMultiSegmentMessage(nil)
s, err := NewRootStruct(seg, sz)
if err != nil {
return Struct{}, exc.WrapError("alloc results", err)
}
sr.result = s
sr.msg = s.Message()
return s, nil
}
func (sr *StructReturner) PrepareReturn(e error) {
sr.mu.Lock()
defer sr.mu.Unlock()
sr.err = e
}
func (sr *StructReturner) Return() {
sr.mu.Lock()
if sr.returned {
sr.mu.Unlock()
panic("StructReturner.Return called twice")
}
sr.returned = true
e := sr.err
if e == nil {
sr.mu.Unlock()
if sr.p != nil {
sr.p.Fulfill(sr.result.ToPtr())
}
} else {
sr.result = Struct{}
sr.mu.Unlock()
if sr.p != nil {
sr.p.Reject(e)
}
}
}
func (sr *StructReturner) ReleaseResults() {
sr.mu.Lock()
alloced := sr.alloced
returned := sr.returned
released := sr.released
err := sr.err
msg := sr.msg
sr.msg = nil
sr.released = true
sr.mu.Unlock()
if !returned {
panic("ReleaseResults() called before Return()")
}
if released {
panic("ReleaseResults() called twice")
}
if !alloced {
return
}
if err != nil && msg != nil {
msg.Release()
}
}
// Answer returns an Answer that will be resolved when Return is called.
// answer must only be called once per StructReturner.
func (sr *StructReturner) Answer(m Method, pcall PipelineCaller) (*Answer, ReleaseFunc) {
defer sr.mu.Unlock()
sr.mu.Lock()
if sr.p != nil {
panic("StructReturner.Answer called multiple times")
}
if sr.returned {
if sr.err != nil {
return ErrorAnswer(m, sr.err), func() {}
}
return ImmediateAnswer(m, sr.result.ToPtr()), func() {
sr.mu.Lock()
msg := sr.result.Message()
sr.result = Struct{}
sr.mu.Unlock()
if msg != nil {
msg.Release()
}
}
}
sr.p = NewPromise(m, pcall, nil)
ans := sr.p.Answer()
return ans, func() {
<-ans.Done()
sr.mu.Lock()
msg := sr.result.Message()
sr.result = Struct{}
sr.mu.Unlock()
sr.p.ReleaseClients()
if msg != nil {
msg.Release()
}
}
}