-
Notifications
You must be signed in to change notification settings - Fork 42
/
request.go
349 lines (279 loc) · 5.92 KB
/
request.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// +build linux
package iouring
import (
"errors"
"sync"
"sync/atomic"
"syscall"
iouring_syscall "github.com/iceber/iouring-go/syscall"
)
type ResultResolver func(req Request)
type RequestCallback func(result Result) error
type Request interface {
Result
Cancel() (Request, error)
Done() <-chan struct{}
GetRes() (int, error)
// Can Only be used in ResultResolver
SetResult(r0, r1 interface{}, err error) error
}
type Result interface {
Fd() int
Opcode() uint8
GetRequestBuffer() (b0, b1 []byte)
GetRequestBuffers() [][]byte
GetRequestInfo() interface{}
FreeRequestBuffer()
Err() error
ReturnValue0() interface{}
ReturnValue1() interface{}
ReturnFd() (int, error)
ReturnInt() (int, error)
Callback() error
}
var _ Request = &request{}
type request struct {
iour *IOURing
id uint64
opcode uint8
res int32
once sync.Once
resolving bool
resolver ResultResolver
callback RequestCallback
fd int
b0 []byte
b1 []byte
bs [][]byte
err error
r0 interface{}
r1 interface{}
requestInfo interface{}
set *requestSet
done chan struct{}
}
func (req *request) resolve() {
if req.resolver == nil {
return
}
select {
case <-req.done:
default:
return
}
req.once.Do(func() {
req.resolving = true
req.resolver(req)
req.resolving = false
req.resolver = nil
})
}
func (req *request) complate(cqe *iouring_syscall.CompletionQueueEvent) {
req.res = cqe.Result
req.iour = nil
close(req.done)
if req.set != nil {
req.set.complateOne()
req.set = nil
}
}
func (req *request) isDone() bool {
select {
case <-req.done:
return true
default:
}
return false
}
func (req *request) Callback() error {
if !req.isDone() {
return ErrRequestCompleted
}
if req.callback == nil {
return ErrNoRequestCallback
}
return req.callback(req)
}
// Cancel request if request is not completed
func (req *request) Cancel() (Request, error) {
if req.isDone() {
return nil, ErrRequestCompleted
}
return req.iour.submitCancel(req.id)
}
func (req *request) Done() <-chan struct{} {
return req.done
}
func (req *request) Opcode() uint8 {
return req.opcode
}
func (req *request) Fd() int {
return req.fd
}
func (req *request) GetRequestBuffer() (b0, b1 []byte) {
return req.b0, req.b1
}
func (req *request) GetRequestBuffers() [][]byte {
return req.bs
}
func (req *request) GetRequestInfo() interface{} {
return req.requestInfo
}
func (req *request) Err() error {
req.resolve()
return req.err
}
func (req *request) ReturnValue0() interface{} {
req.resolve()
return req.r0
}
func (req *request) ReturnValue1() interface{} {
req.resolve()
return req.r1
}
func (req *request) ReturnFd() (int, error) {
return req.ReturnInt()
}
func (req *request) ReturnInt() (int, error) {
req.resolve()
if req.err != nil {
return -1, req.err
}
fd, ok := req.r0.(int)
if !ok {
return -1, errors.New("req value is not int")
}
return fd, nil
}
func (req *request) FreeRequestBuffer() {
req.b0 = nil
req.b1 = nil
req.bs = nil
}
func (req *request) GetRes() (int, error) {
if !req.isDone() {
return 0, ErrRequestNotCompleted
}
return int(req.res), nil
}
func (req *request) SetResult(r0, r1 interface{}, err error) error {
if !req.isDone() {
return ErrRequestNotCompleted
}
if !req.resolving {
return errors.New("request is not resolving")
}
req.r0, req.r1, req.err = r0, r1, err
return nil
}
type RequestSet interface {
Len() int
Done() <-chan struct{}
Requests() []Request
ErrResults() []Result
}
var _ RequestSet = &requestSet{}
type requestSet struct {
requests []Request
complates int32
done chan struct{}
}
func newRequestSet(userData []*UserData) *requestSet {
set := &requestSet{
requests: make([]Request, len(userData)),
done: make(chan struct{}),
}
for i, data := range userData {
set.requests[i] = data.request
data.request.set = set
}
return set
}
func (set *requestSet) complateOne() {
if atomic.AddInt32(&set.complates, 1) == int32(len(set.requests)) {
close(set.done)
}
}
func (set *requestSet) Len() int {
return len(set.requests)
}
func (set *requestSet) Done() <-chan struct{} {
return set.done
}
func (set *requestSet) Requests() []Request {
return set.requests
}
func (set *requestSet) ErrResults() (results []Result) {
for _, req := range set.requests {
if req.Err() != nil {
results = append(results, req)
}
}
return
}
func errResolver(req Request) {
result := req.(*request)
if result.res < 0 {
result.err = syscall.Errno(-result.res)
if result.err == syscall.ECANCELED {
// request is canceled
result.err = ErrRequestCanceled
}
}
}
func fdResolver(req Request) {
result := req.(*request)
if errResolver(result); result.err != nil {
return
}
result.r0 = int(result.res)
}
func timeoutResolver(req Request) {
result := req.(*request)
if errResolver(result); result.err != nil {
// if timeout got completed through expiration of the timer
// result.res is -ETIME and result.err is syscall.ETIME
if result.err == syscall.ETIME {
result.err = nil
result.r0 = TimeoutExpiration
}
return
}
// if timeout got completed through requests completing
// result.res is 0
if result.res == 0 {
result.r0 = CountCompletion
}
}
func removeTimeoutResolver(req Request) {
result := req.(*request)
if errResolver(result); result.err != nil {
switch result.err {
case syscall.EBUSY:
// timeout request was found bu expiration was already in progress
result.err = ErrRequestCompleted
case syscall.ENOENT:
// timeout request not found
result.err = ErrRequestNotFound
}
return
}
// timeout request is found and cacelled successfully
// result.res value is 0
}
func cancelResolver(req Request) {
result := req.(*request)
if errResolver(result); result.err != nil {
switch result.err {
case syscall.ENOENT:
result.err = ErrRequestNotFound
case syscall.EALREADY:
result.err = nil
result.r0 = RequestMaybeCanceled
}
return
}
if result.res == 0 {
result.r0 = RequestCanceledSuccessfully
}
}