-
Notifications
You must be signed in to change notification settings - Fork 4
/
q-block-retry-multi-async.go
145 lines (135 loc) · 3.15 KB
/
q-block-retry-multi-async.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
package Figo
import (
"github.com/quexer/utee"
"sync"
"log"
"time"
)
type MultiAsyncBlockExecuteQ struct {
mq map[string]utee.MemQueue
mc map[string]chan bool
tc *utee.TimerCache
seq *SeqMem
trySecs int
tryTimes int
execute func(data interface{})
callback func(data interface{}, workFlag bool)
qlock sync.Mutex
clock sync.Mutex
perCap int
}
func NewMultiAsyncBlockExecuteQ(perCap, retrySec, tryTimes int, exec func(data interface{}), callback func(data interface{}, workFlag bool)) MultiAsyncBlockExecuteQ {
beq := MultiAsyncBlockExecuteQ{
tryTimes: tryTimes,
trySecs: tryTimes,
execute: exec,
callback: callback,
seq: NewSeqMem(),
qlock: sync.Mutex{},
clock: sync.Mutex{},
perCap: perCap,
}
beq.mq = make(map[string]utee.MemQueue)
beq.mc = make(map[string]chan bool)
beq.tc = utee.NewTimerCache(retrySec, beq.retry)
return beq
}
func (p *MultiAsyncBlockExecuteQ) retry(k, v interface{}) {
task := v.(*MultiBlockChannelItem)
task.timesIncr()
if task.tryTimes > p.tryTimes {
p.tc.Remove(task.k)
p.getC(task.prefix) <- false
return
}
if !task.doneFlag {
p.execute(task.data)
p.tc.Put(k, v)
}
}
func (p *MultiAsyncBlockExecuteQ) blockExec(v interface{}) {
task := v.(*MultiBlockChannelItem)
ch := p.getC(task.prefix)
clearDirtyHookData := func() {
for len(ch) > 0 {
log.Println("clearDirtyHook >>> @prefix:",task.prefix," @len:",len(ch))
<-ch
}
}
clearDirtyHookData()
p.tc.Put(task.k, task)
task.timesIncr()
p.execute(task.data)
workFlag:=<-ch
task.doneFlag = true
p.tc.Remove(task.k)
mergeMuliResult := func(){
for len(ch) > 0 {
if !workFlag {
workFlag=<-ch
}else{
<-ch
}
log.Println("merge >>> @prefix:",task.prefix," @len:",len(ch),"@workFlag:",workFlag)
}
}
time.Sleep(time.Nanosecond*time.Duration(17))
mergeMuliResult()
if p.callback != nil {
p.callback(task.data,workFlag)
}
}
func (p *MultiAsyncBlockExecuteQ) getQ(prefix string) utee.MemQueue {
p.qlock.Lock()
defer p.qlock.Unlock()
if q := p.mq[prefix]; q == nil {
p.mq[prefix] = utee.NewLeakMemQueue(p.perCap, 1, p.blockExec)
}
return p.mq[prefix]
}
func (p *MultiAsyncBlockExecuteQ) getC(prefix string) chan bool {
if c := p.mc[prefix]; c != nil {
return c
}
p.clock.Lock()
defer p.clock.Unlock()
if c := p.mc[prefix]; c != nil {
return c
} else {
const WAY4Hook = 2
p.mc[prefix] = make(chan bool, WAY4Hook)
}
return p.mc[prefix]
}
func (p *MultiAsyncBlockExecuteQ) Enq(prefix string, v interface{}) {
k := p.seq.Next()
item := &MultiBlockChannelItem{
k: k,
data: v,
done: make(chan bool, 1),
tryTimes: 0,
doneFlag: false,
prefix: prefix,
}
p.getQ(prefix).Enq(item)
}
func (p *MultiAsyncBlockExecuteQ) Deq(prefix string, filter func(item *MultiBlockChannelItem)bool) chan *MultiBlockChannelItem {
mq:=p.getQ(prefix)
if mq.Len()<=0 {
return nil
}
items:=mq.DeqN(mq.Len())
c:=make(chan *MultiBlockChannelItem,mq.Len()+1)
for _,item:=range items {
v:=item.(*MultiBlockChannelItem)
if filter(v) {
c<-v
}
}
return c
}
func (p *MultiAsyncBlockExecuteQ) Hook(prefix string, status bool) {
if status {
p.getC(prefix) <- status
}
}