forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 58
/
future.go
268 lines (229 loc) · 5.57 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
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
package tarantool
import (
"io"
"sync"
"time"
)
// Future is a handle for asynchronous request.
type Future struct {
requestId uint32
req Request
next *Future
timeout time.Duration
mutex sync.Mutex
pushes []Response
resp Response
err error
ready chan struct{}
done chan struct{}
}
func (fut *Future) wait() {
if fut.done == nil {
return
}
<-fut.done
}
func (fut *Future) isDone() bool {
if fut.done == nil {
return true
}
select {
case <-fut.done:
return true
default:
return false
}
}
type asyncResponseIterator struct {
fut *Future
timeout time.Duration
resp Response
err error
curPos int
done bool
}
func (it *asyncResponseIterator) Next() bool {
if it.done || it.err != nil {
it.resp = nil
return false
}
var last = false
var exit = false
for !exit {
// We try to read at least once.
it.fut.mutex.Lock()
it.resp = it.nextResponse()
it.err = it.fut.err
last = it.resp == it.fut.resp
it.fut.mutex.Unlock()
if it.timeout == 0 || it.resp != nil || it.err != nil {
break
}
select {
case <-it.fut.ready:
case <-time.After(it.timeout):
exit = true
}
}
if it.resp == nil {
return false
}
if last {
it.done = true
} else {
it.err = nil
it.curPos += 1
}
return true
}
func (it *asyncResponseIterator) Value() Response {
return it.resp
}
func (it *asyncResponseIterator) IsPush() bool {
return !it.done
}
func (it *asyncResponseIterator) Err() error {
return it.err
}
func (it *asyncResponseIterator) WithTimeout(timeout time.Duration) TimeoutResponseIterator {
it.timeout = timeout
return it
}
func (it *asyncResponseIterator) nextResponse() (resp Response) {
fut := it.fut
pushesLen := len(fut.pushes)
if it.curPos < pushesLen {
resp = fut.pushes[it.curPos]
} else if it.curPos == pushesLen {
resp = fut.resp
}
return resp
}
// PushResponse is used for push requests for the Future.
type PushResponse struct {
baseResponse
}
func createPushResponse(header Header, body io.Reader) (Response, error) {
resp, err := createBaseResponse(header, body)
if err != nil {
return nil, err
}
return &PushResponse{resp}, nil
}
// NewFuture creates a new empty Future for a given Request.
func NewFuture(req Request) (fut *Future) {
fut = &Future{}
fut.ready = make(chan struct{}, 1000000000)
fut.done = make(chan struct{})
fut.pushes = make([]Response, 0)
fut.req = req
return fut
}
// AppendPush appends the push response to the future.
// Note: it works only before SetResponse() or SetError()
//
// Deprecated: the method will be removed in the next major version,
// use Connector.NewWatcher() instead of box.session.push().
func (fut *Future) AppendPush(header Header, body io.Reader) error {
fut.mutex.Lock()
defer fut.mutex.Unlock()
if fut.isDone() {
return nil
}
resp, err := createPushResponse(header, body)
if err != nil {
return err
}
fut.pushes = append(fut.pushes, resp)
fut.ready <- struct{}{}
return nil
}
// SetResponse sets a response for the future and finishes the future.
func (fut *Future) SetResponse(header Header, body io.Reader) error {
fut.mutex.Lock()
defer fut.mutex.Unlock()
if fut.isDone() {
return nil
}
resp, err := fut.req.Response(header, body)
if err != nil {
return err
}
fut.resp = resp
close(fut.ready)
close(fut.done)
return nil
}
// SetError sets an error for the future and finishes the future.
func (fut *Future) SetError(err error) {
fut.mutex.Lock()
defer fut.mutex.Unlock()
if fut.isDone() {
return
}
fut.err = err
close(fut.ready)
close(fut.done)
}
// GetResponse waits for Future to be filled and returns Response and error.
//
// Note: Response could be equal to nil if ClientError is returned in error.
//
// "error" could be Error, if it is error returned by Tarantool,
// or ClientError, if something bad happens in a client process.
func (fut *Future) GetResponse() (Response, error) {
fut.wait()
return fut.resp, fut.err
}
// Get waits for Future to be filled and returns the data of the Response and error.
//
// The data will be []interface{}, so if you want more performance, use GetTyped method.
//
// "error" could be Error, if it is error returned by Tarantool,
// or ClientError, if something bad happens in a client process.
func (fut *Future) Get() ([]interface{}, error) {
fut.wait()
if fut.err != nil {
return nil, fut.err
}
return fut.resp.Decode()
}
// GetTyped waits for Future and calls msgpack.Decoder.Decode(result) if no error happens.
// It is could be much faster than Get() function.
//
// Note: Tarantool usually returns array of tuples (except for Eval and Call17 actions).
func (fut *Future) GetTyped(result interface{}) error {
fut.wait()
if fut.err != nil {
return fut.err
}
return fut.resp.DecodeTyped(result)
}
// GetIterator returns an iterator for iterating through push messages
// and a response. Push messages and the response will contain deserialized
// result in Data field as for the Get() function.
//
// # See also
//
// - box.session.push():
// https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_session/push/
//
// Deprecated: the method will be removed in the next major version,
// use Connector.NewWatcher() instead of box.session.push().
func (fut *Future) GetIterator() (it TimeoutResponseIterator) {
futit := &asyncResponseIterator{
fut: fut,
}
return futit
}
var closedChan = make(chan struct{})
func init() {
close(closedChan)
}
// WaitChan returns channel which becomes closed when response arrived or error occurred.
func (fut *Future) WaitChan() <-chan struct{} {
if fut.done == nil {
return closedChan
}
return fut.done
}