forked from fl00r/go-iproto
-
Notifications
You must be signed in to change notification settings - Fork 2
/
context.go
283 lines (250 loc) · 5.21 KB
/
context.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
package iproto
import (
"log"
"runtime"
"sync"
"sync/atomic"
)
var _ = log.Print
const (
cxReqBuf = 16
cxReqBufMax = 128
)
type contextBookmark struct {
Bookmark
c *Context
}
func (cm *contextBookmark) Respond(res *Response) {
if c := cm.c; c != nil {
c.RemoveCanceler(cm)
}
}
func (cm *contextBookmark) Cancel() {
if r := cm.Request; r != nil {
r.Cancel()
}
}
type Canceler interface {
Cancel()
}
type CxState uint32
const (
CxOK = CxState(0)
CxCanceled = CxState(1)
CxTimeout = CxState(2)
)
type Context struct {
// RetCode stores return code if case when original request were canceled or expired
State CxState
parent *Context
m sync.Mutex
childCond sync.Cond
cancels [2]Canceler
cancelsn int
cancelsm map[Canceler]struct{}
reqId uint32
owngen bool
gen *RGenerator
cancelBuf []contextBookmark
}
func (c *Context) RemoveCanceler(cn Canceler) {
c.m.Lock()
var i int
for i = 0; i < len(c.cancels); i++ {
if cn == c.cancels[i] {
c.cancels[i] = nil
c.cancelsn--
break
}
}
if i == len(c.cancels) {
delete(c.cancelsm, cn)
}
if c.cancelsn == 0 && len(c.cancelsm) == 0 {
c.childCond.Signal()
}
c.m.Unlock()
}
func (c *Context) AddCanceler(cn Canceler) {
var ok bool
c.m.Lock()
c.childCond.L = &c.m
ok = c.State == 0
if ok {
var i int
for i = 0; i < len(c.cancels); i++ {
if c.cancels[i] == nil {
c.cancels[i] = cn
c.cancelsn++
break
}
}
if i == len(c.cancels) {
if c.cancelsm == nil {
c.cancelsm = make(map[Canceler]struct{})
}
c.cancelsm[cn] = struct{}{}
}
}
c.m.Unlock()
if !ok {
cn.Cancel()
}
}
func (c *Context) cancelAll() {
for len(c.cancelsm) > 0 || c.cancelsn > 0 {
c.m.Lock()
cancels := make([]Canceler, 0, len(c.cancels)+c.cancelsn)
for i := 0; i < len(c.cancels); i++ {
if c.cancels[i] != nil {
cancels = append(cancels, c.cancels[i])
}
}
for cancel := range c.cancelsm {
cancels = append(cancels, cancel)
}
c.m.Unlock()
for _, cancel := range cancels {
cancel.Cancel()
}
runtime.Gosched()
}
}
func (c *Context) Cancel() {
if atomic.CompareAndSwapUint32((*uint32)(&c.State), 0, uint32(CxCanceled)) {
c.cancelAll()
}
}
func (c *Context) Expire() {
if atomic.CompareAndSwapUint32((*uint32)(&c.State), 0, uint32(CxTimeout)) {
c.cancelAll()
}
}
func (c *Context) NewRequest(msg RequestType, body interface{}) (r *Request, res <-chan *Response) {
c.reqId++
if c.gen == nil {
c.gen = GetGenerator()
c.owngen = true
}
r = c.gen.Request(c.reqId, msg, body)
ch := make(Chan, 1)
res, r.Responder = ch, ch
rc := RetCode(atomic.LoadUint32((*uint32)(&c.State)))
if rc != 0 {
r.Cancel()
} else {
if len(c.cancelBuf) == 0 {
c.cancelBuf = make([]contextBookmark, cxReqBuf)
}
m := &c.cancelBuf[0]
m.c = c
c.cancelBuf = c.cancelBuf[1:]
r.ChainBookmark(m)
c.AddCanceler(m)
}
return
}
func (c *Context) NewMulti() (multi *MultiRequest) {
if c.gen == nil {
c.gen = GetGenerator()
c.owngen = true
}
multi = &MultiRequest{cx: c, gen: c.gen}
rc := RetCode(atomic.LoadUint32((*uint32)(&c.State)))
if rc != 0 {
multi.Cancel()
} else {
c.AddCanceler(multi)
}
return multi
}
func (c *Context) SendMsgBody(serv Service, msg RequestType, body interface{}) (req *Request, res <-chan *Response) {
req, res = c.NewRequest(msg, body)
serv.Send(req)
return
}
func (c *Context) CallMsgBody(serv Service, msg RequestType, body interface{}) *Response {
var req *Request
req, res := c.NewRequest(msg, body)
serv.Send(req)
return <-res
}
func (c *Context) Send(serv Service, r RequestData) (req *Request, res <-chan *Response) {
req, res = c.NewRequest(r.IMsg(), r)
serv.Send(req)
return req, res
}
func (c *Context) Call(serv Service, r RequestData) *Response {
req, res := c.NewRequest(r.IMsg(), r)
serv.Send(req)
return <-res
}
func (c *Context) Alive() bool {
return c.State == 0
}
func (c *Context) Timeout() bool {
return c.State == CxTimeout
}
func (c *Context) Child() (child *Context, ok bool) {
child = &Context{parent: c}
rc := CxState(atomic.LoadUint32((*uint32)(&c.State)))
if rc == 0 {
c.AddCanceler(child)
} else {
child.Cancel()
}
return
}
func (c *Context) Go(f func(cx *Context)) (child *Context) {
var ok bool
if child, ok = c.Child(); ok {
go child.go_(f)
}
return
}
func (child *Context) go_(f func(cx *Context)) {
defer child.Done()
f(child)
}
func (c *Context) GoInt(f func(*Context, interface{}), i interface{}) (child *Context) {
var ok bool
if child, ok = c.Child(); ok {
go child.goInt(f, i)
}
return
}
func (child *Context) goInt(f func(*Context, interface{}), i interface{}) {
defer child.Done()
f(child, i)
}
func (c *Context) WaitAll() {
c.m.Lock()
for c.cancelsn != 0 || len(c.cancelsm) > 0 {
c.childCond.Wait()
}
c.m.Unlock()
}
func (c *Context) GoAsync(f func(cx *Context)) (child *Context) {
var ok bool
if child, ok = c.Child(); ok {
go f(child)
}
return
}
func (c *Context) GoIntAsync(f func(*Context, interface{}), i interface{}) (child *Context) {
var ok bool
if child, ok = c.Child(); ok {
go f(child, i)
}
return
}
func (c *Context) Done() {
if parent := c.parent; parent != nil {
c.parent = nil
parent.RemoveCanceler(c)
}
if c.owngen {
c.owngen = false
c.gen.Release()
}
}