-
Notifications
You must be signed in to change notification settings - Fork 15
/
routine.go
418 lines (362 loc) · 7.21 KB
/
routine.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package dingo
//
// 'routines' helps to manage go-routines
// depending on the same input/output channels
//
import (
"errors"
"fmt"
"math/rand"
"reflect"
"sync"
"time"
)
// Homogeneous Routines
//
//
type Routines struct {
quits []chan int
qLock sync.Mutex
wg sync.WaitGroup
events chan *Event
}
func NewRoutines() *Routines {
return &Routines{
quits: make([]chan int, 0, 10),
events: make(chan *Event, 10),
}
}
func (rs *Routines) New() <-chan int {
rs.wg.Add(1)
rs.qLock.Lock()
defer rs.qLock.Unlock()
rs.quits = append(rs.quits, make(chan int, 1))
return rs.quits[len(rs.quits)-1]
}
func (rs *Routines) Wait() *sync.WaitGroup {
return &rs.wg
}
func (rs *Routines) Events() chan *Event {
return rs.events
}
/*Close is used to stop/release all allocated routines,
this function should be safe from multiple calls.
*/
func (rs *Routines) Close() {
rs.qLock.Lock()
defer rs.qLock.Unlock()
for _, v := range rs.quits {
v <- 1
close(v)
}
rs.quits = make([]chan int, 0, 10)
rs.wg.Wait()
close(rs.events)
rs.events = make(chan *Event, 10)
}
// Heterogeneous Routines
//
// similar to 'Routines', but can be closed
// one by one.
type _control struct {
quit, done chan int
}
type HetroRoutines struct {
ctrls map[int]*_control
ctrlsLock sync.Mutex
events chan *Event
}
func NewHetroRoutines() *HetroRoutines {
return &HetroRoutines{
ctrls: make(map[int]*_control),
events: make(chan *Event, 10),
}
}
func (rs *HetroRoutines) New(want int) (quit <-chan int, done chan<- int, idx int) {
rs.ctrlsLock.Lock()
defer rs.ctrlsLock.Unlock()
// get an index
idx = want
for {
_, ok := rs.ctrls[idx]
if !ok {
break
}
idx = rand.Int()
}
rs.ctrls[idx] = &_control{
quit: make(chan int, 1),
done: make(chan int, 1),
}
quit = rs.ctrls[idx].quit
done = rs.ctrls[idx].done
return
}
func (rs *HetroRoutines) Stop(idx int) (err error) {
var c *_control
err = func() (err error) {
rs.ctrlsLock.Lock()
defer rs.ctrlsLock.Unlock()
var ok bool
if c, ok = rs.ctrls[idx]; !ok {
err = fmt.Errorf("Index not found: %v", idx)
return
}
delete(rs.ctrls, idx)
return
}()
if c != nil {
c.quit <- 1
close(c.quit)
_, _ = <-c.done
}
return
}
func (rs *HetroRoutines) Events() chan *Event {
return rs.events
}
func (rs *HetroRoutines) Close() {
rs.ctrlsLock.Lock()
defer rs.ctrlsLock.Unlock()
// sending quit signal
for _, v := range rs.ctrls {
v.quit <- 1
close(v.quit)
}
// awaiting done signal
for _, v := range rs.ctrls {
_, _ = <-v.done
}
rs.ctrls = make(map[int]*_control)
}
// Chained Routines
//
// routines act like a linked list, each node have prev/next channel to connect with each other
type nodeHandler interface {
HandleInput(v interface{})
HandleLink(v interface{}) bool
Done()
}
type chainRoutines struct {
head, tail chan interface{}
outterEvents chan<- *Event
remain func(interface{})
lock sync.Mutex
headWait, nodeWait sync.WaitGroup
}
func newChainRoutines(remain func(interface{}), events chan<- *Event) (v *chainRoutines) {
v = &chainRoutines{
head: make(chan interface{}, 10), // TODO: config
remain: remain,
outterEvents: events,
}
v.tail = v.head
v.headWait.Add(1)
go v.headRoutine(&v.headWait)
return
}
func (rt *chainRoutines) Send(v interface{}) {
rt.head <- v
}
func (rt *chainRoutines) Add(input interface{}, handler nodeHandler) error {
rt.lock.Lock()
defer rt.lock.Unlock()
if rt.head == nil || rt.tail == nil {
return errors.New("chain-routines closed")
}
// prepare prev/next link for new node
var (
prev <-chan interface{}
next chan<- interface{}
)
if rt.head == rt.tail {
rt.head = make(chan interface{}, 10) // TODO: config
prev, next = rt.head, rt.tail
} else {
next = rt.head
rt.head = make(chan interface{}, 10)
prev = rt.head
}
rt.nodeWait.Add(1)
go rt.nodeRoutine(&rt.nodeWait, input, prev, next, handler)
return nil
}
func (rt *chainRoutines) Close() {
rt.lock.Lock()
defer rt.lock.Unlock()
if rt.head == nil || rt.tail == nil {
return
}
tmp := rt.head
rt.head = make(chan interface{}, 100) // TODO: config
// trigger a serious of close signal
close(tmp)
// wait until node routines done their clean up
rt.nodeWait.Wait()
// wait until head routine done
rt.headWait.Wait()
close(rt.head) // close the temporary channel
rt.head = nil
rt.tail = nil
return
}
func (rt *chainRoutines) headRoutine(wait *sync.WaitGroup) {
var (
k int
v interface{}
ok bool
rest = make([]interface{}, 0, 100)
)
defer wait.Done()
// lots of data race conition in this function,
// possible conditions 'should' already be considered and taken care.
//
// if anything goes wrong, beware of a performance downgrade when adding lock.
for {
select {
case <-time.After(1 * time.Millisecond):
if rt.tail == rt.head {
break
}
k = 0
sent:
for _, v = range rest {
select {
case rt.head <- v:
k++
default:
// channel buffer is full
break sent
}
}
rest = rest[k:]
case v, ok = <-rt.tail:
if !ok {
goto cleanTail
}
rest = append(rest, v)
}
}
cleanTail:
for {
// consume from head to 'rest'
select {
case v, ok = <-rt.tail:
if !ok {
break cleanTail
}
rest = append(rest, v)
default:
break cleanTail
}
}
cleanHead:
for {
// consume from head to 'rest'
select {
case v, ok = <-rt.head:
if !ok {
break cleanHead
}
rest = append(rest, v)
default:
break cleanHead
}
}
// dump everythin remaining in 'rest'
for _, v = range rest {
rt.outterEvents <- NewEventFromError(ObjT.ChainRoutine, fmt.Errorf("remaining link event:%v", v))
rt.remain(v)
}
}
func (rt *chainRoutines) nodeRoutine(
wait *sync.WaitGroup,
input interface{},
prev <-chan interface{},
next chan<- interface{},
handler nodeHandler,
) {
defer func() {
handler.Done()
wait.Done()
}()
var (
value reflect.Value
chosen int
ok bool
v interface{}
conds []reflect.SelectCase
)
// compose select-cases
conds = []reflect.SelectCase{
reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(prev),
}, reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(input),
},
}
for {
chosen, value, ok = reflect.Select(conds)
switch chosen {
case 0:
// prev
if !ok {
goto clean
}
v = value.Interface()
if !handler.HandleLink(v) {
next <- v
}
case 1:
// input
if !ok {
goto cleanLink
}
handler.HandleInput(value.Interface())
}
}
cleanLink:
// stay alive to forward link packets, until link channel closed
for {
select {
case v, ok = <-prev:
if !ok {
goto clean
}
if !handler.HandleLink(v) {
next <- v
}
}
}
clean:
// trigger closing signal for next node
close(next)
// keep consuming remaining inputs
conds = []reflect.SelectCase{
reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(input),
},
reflect.SelectCase{
Dir: reflect.SelectDefault,
},
}
finished:
for {
chosen, value, ok = reflect.Select(conds)
switch chosen {
case 0:
if !ok {
break finished
}
handler.HandleInput(value.Interface())
case 1:
break finished
}
}
}
func init() {
rand.Seed(time.Now().UnixNano())
}