forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathfuture.go
More file actions
206 lines (167 loc) · 4.1 KB
/
Copy pathfuture.go
File metadata and controls
206 lines (167 loc) · 4.1 KB
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
package tarantool
import (
"io"
"sync"
"time"
)
// Future is an interface that handle asynchronous request.
type Future interface {
Get() ([]any, error)
GetTyped(result any) error
GetResponse() (Response, error)
Release()
WaitChan() <-chan struct{}
}
var futurePool = sync.Pool{
New: func() any { return &future{} },
}
// future is inner implementation of Future interface.
type future struct {
requestId uint32
req Request
next *future
timeout time.Duration
mutex sync.Mutex
resp Response
err error
cond sync.Cond
finished bool
done chan struct{}
}
var _ = Future(&future{})
func (fut *future) wait() {
fut.mutex.Lock()
defer fut.mutex.Unlock()
for !fut.finished {
fut.cond.Wait()
}
}
func (fut *future) finish() {
fut.mutex.Lock()
defer fut.mutex.Unlock()
fut.finished = true
if fut.done != nil {
close(fut.done)
}
fut.cond.Broadcast()
}
// NewFutureWithErr returns Future with given error.
func NewFutureWithErr(req Request, err error) Future {
fut := newFuture(req)
fut.setError(err)
return fut
}
// NewFutureWithResponse returns Future with given Response.
func NewFutureWithResponse(req Request, header Header, body io.Reader) (Future, error) {
fut := newFuture(req)
if err := fut.setResponse(header, body); err != nil {
return nil, err
}
return fut, nil
}
// newFuture creates a new empty future for a given Request.
func newFuture(req Request) (fut *future) {
fut = futurePool.Get().(*future)
fut.done = nil
fut.finished = false
fut.cond.L = &fut.mutex
fut.req = req
return fut
}
func (fut *future) isFinished() bool {
fut.mutex.Lock()
defer fut.mutex.Unlock()
return fut.finished
}
// 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.finished {
return nil
}
resp, err := fut.req.Response(header, body)
if err != nil {
return err
}
fut.resp = resp
fut.finished = true
if fut.done != nil {
close(fut.done)
}
fut.cond.Broadcast()
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.finished {
return
}
fut.err = err
fut.finished = true
if fut.done != nil {
close(fut.done)
}
fut.cond.Broadcast()
}
// 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() ([]any, 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 Call actions).
func (fut *future) GetTyped(result any) error {
fut.wait()
if fut.err != nil {
return fut.err
}
return fut.resp.DecodeTyped(result)
}
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{} {
fut.mutex.Lock()
defer fut.mutex.Unlock()
if fut.finished {
return closedChan
}
if fut.done == nil {
fut.done = make(chan struct{})
}
return fut.done
}
// Release is freeing the Future resources.
// After this, using this Future becomes invalid.
func (fut *future) Release() {
if fut.resp != nil {
fut.resp.Release()
}
*fut = future{}
futurePool.Put(fut)
}