forked from fl00r/go-iproto
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wait_group.go
238 lines (211 loc) · 4.09 KB
/
wait_group.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
package iproto
import (
"log"
"sort"
"sync"
"sync/atomic"
"time"
)
var _ = log.Print
const (
mrInFly = 0
mrFailed = mrCancel | mrExpire
)
const (
mrChan = (1 << iota)
mrWait
mrCancel
mrExpire
mrFailPerformed
)
const (
mrBufSize = 16
)
type MultiResponse []*Response
var _ sort.Interface = MultiResponse(nil)
func (mr MultiResponse) Less(i, j int) bool {
return mr[i].Id < mr[j].Id
}
func (mr MultiResponse) Swap(i, j int) {
mr[i], mr[j] = mr[j], mr[i]
}
func (mr MultiResponse) Len() int {
return len(mr)
}
func (mr MultiResponse) Sort() MultiResponse {
sort.Sort(mr)
return mr
}
type MultiRequest struct {
cx *Context
gen *RGenerator
m sync.Mutex
c, r uint32
requests []*Request
responses MultiResponse
ch chan *Response
w sync.Cond
timer Timer
timerSet bool
owngen bool
kind uint32
bodyn uint32
}
func (w *MultiRequest) Timer() *Timer {
return &w.timer
}
func (w *MultiRequest) TimeoutFrom(d Service) {
w.SetTimeout(d.DefaultTimeout())
}
func (w *MultiRequest) SetTimeout(timeout time.Duration) {
if timeout > 0 && !w.timerSet {
w.timerSet = true
w.timer.After(timeout, w)
}
}
func (w *MultiRequest) Request(msg RequestType, body interface{}) *Request {
if w.gen == nil {
w.gen = GetGenerator()
w.owngen = true
}
req := w.gen.Request(uint32(len(w.requests)), msg, body)
req.Responder = w
req.timerSet = w.timerSet
if len(w.requests) == cap(w.requests) {
w.m.Lock()
if cap(w.requests) == 0 {
w.requests = make([]*Request, 1, cxReqBuf)
w.requests[0] = req
} else {
w.requests = append(w.requests, req)
}
w.m.Unlock()
} else {
w.requests = append(w.requests, req)
}
atomic.StoreUint32(&w.r, uint32(len(w.requests)))
if atomic.LoadUint32(&w.kind)&mrFailed != 0 {
w.performFail(req)
}
return req
}
func (w *MultiRequest) SendMsgBody(serv Service, msg RequestType, body interface{}) *Request {
req := w.Request(msg, body)
serv.Send(req)
return req
}
func (w *MultiRequest) Send(serv Service, r RequestData) *Request {
req := w.Request(r.IMsg(), r)
serv.Send(req)
return req
}
func (w *MultiRequest) Each() <-chan *Response {
if w.kind&mrFailed != 0 && w.c != w.r {
w.performFailAll()
}
w.m.Lock()
if w.owngen {
w.gen.Release()
}
w.setKind(mrChan)
w.ch = make(chan *Response, w.r)
for _, resp := range w.responses {
w.requests[resp.Id] = nil
w.ch <- resp
}
w.responses = nil
if w.c == w.r {
close(w.ch)
}
w.m.Unlock()
return w.ch
}
func (w *MultiRequest) Results() MultiResponse {
if w.kind&mrFailed != 0 && w.c != w.r {
w.performFailAll()
}
w.m.Lock()
if w.owngen {
w.gen.Release()
}
w.setKind(mrWait)
w.w.L = &w.m
if cap(w.responses) < len(w.requests) {
tmp := make([]*Response, len(w.responses), len(w.requests))
copy(tmp, w.responses)
w.responses = tmp
}
if atomic.LoadUint32(&w.c) < w.r {
w.w.Wait()
}
res := w.responses
w.responses = nil
if w.cx != nil {
w.cx.RemoveCanceler(w)
}
w.m.Unlock()
return res
}
func (w *MultiRequest) Respond(r *Response) {
if w.ch == nil {
w.m.Lock()
if w.ch == nil {
w.responses = append(w.responses, r)
w.requests[r.Id] = nil
if w.c++; w.c == w.r && w.kind&mrWait != 0 {
w.timer.Stop()
w.w.Signal()
}
w.m.Unlock()
return
}
w.m.Unlock()
}
w.ch <- r
if v := atomic.AddUint32(&w.c, 1); v == w.r {
w.timer.Stop()
close(w.ch)
if w.cx != nil {
w.cx.RemoveCanceler(w)
}
}
}
func (w *MultiRequest) Cancel() {
w.setKind(mrCancel)
if w.c == w.r {
return
}
w.performFailAll()
}
func (w *MultiRequest) Expire() {
w.setKind(mrExpire)
if w.c == w.r {
return
}
w.performFailAll()
}
func (w *MultiRequest) performFailAll() {
w.m.Lock()
r := int(atomic.LoadUint32(&w.r))
allReqs := w.requests
w.m.Unlock()
for i := 0; i < r; i++ {
if allReqs[i] != nil {
w.performFail(allReqs[i])
}
}
}
func (w *MultiRequest) performFail(r *Request) {
switch {
case w.kind&mrCancel != 0:
r.Cancel()
case w.kind&mrExpire != 0:
r.Expire()
}
}
func (w *MultiRequest) setKind(k uint32) {
kind := w.kind
for !atomic.CompareAndSwapUint32(&w.kind, kind, kind|k) {
kind = w.kind
}
}