-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregator.go
195 lines (166 loc) · 3.58 KB
/
aggregator.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
package aggregator
import (
"time"
"fmt"
"sync"
"sync/atomic"
)
const DefaultTickTime = time.Millisecond * 500
//aggregation to deal
type aggregator struct{
wait *sync.WaitGroup
handler func([] interface{})error
errorHandler func(msg string)
aggregatorData []interface{}
processCount int //process count
lock *sync.Mutex
workCount int
dataChan chan interface{}
stopped chan struct{}
isStop int32
tickFrequency time.Duration
}
//create object
func NewAggregator(workCount, chanSize, processCount int )*aggregator{
return &aggregator{
wait:&sync.WaitGroup{},
workCount:workCount,
processCount:processCount,
dataChan: make(chan interface{}, chanSize),
stopped: make(chan struct{}),
isStop: 0,
aggregatorData:make([]interface{},0),
lock: &sync.Mutex{},
tickFrequency:DefaultTickTime,
}
}
//set handler
func (a *aggregator)SetHandler(handler func([] interface{})error){
a.handler = handler
}
//set error handler
func (a *aggregator)SetErrorHandler(errorHandler func(msg string)){
a.errorHandler = errorHandler
}
func (a *aggregator)SetTickFrequency(duration time.Duration){
a.tickFrequency = duration
}
//receive data
func (a *aggregator)Receive(data interface{}){
//not close
if a.IsStopped(){
return
}
a.dataChan<-data
}
//start pool
func (a *aggregator) Start(){
for i:=0; i < a.workCount; i++ {
go func(){
a.wait.Add(1)
worker(a)
}()
}
}
//do some worker
func worker(a *aggregator){
defer a.wait.Done()
defer func(){
if err := recover(); err != nil {
if a.errorHandler != nil {
a.errorHandler(fmt.Sprintf("aggregaterData has not deal %d", len(a.aggregatorData)))
}
}
}()
ticker := time.NewTicker(a.tickFrequency)
var toBeProcess []interface{}
loop:
for{
select {
case <-a.stopped:
a.addData(toBeProcess)
a.StopHandle()
break loop
case data,ok := <- a.dataChan:
if a.IsStopped() || !ok{
a.addData(toBeProcess)
a.StopHandle()
break loop
}else{
toBeProcess = append(toBeProcess, data)
if len(toBeProcess) >= a.processCount {
err := a.handler(toBeProcess)
if err == nil {
toBeProcess = make([]interface{}, 0)
}else {
if a.errorHandler != nil {
}
a.errorHandler(fmt.Sprintf("aggregate handler msg failed %+v",toBeProcess))
}
}
}
case <-ticker.C:
if a.IsStopped(){
a.addData(toBeProcess)
break loop
}
}
}
}
//add data
func (a *aggregator) addData(toBeProcess []interface{}){
if len(toBeProcess) > 0{
a.lock.Lock()
a.aggregatorData = append(a.aggregatorData, toBeProcess...)
a.lock.Unlock()
}
}
//stop
func (a *aggregator)StopHandle(){
atomic.StoreInt32(&(a.isStop),1)
}
//check is stopped
func (a *aggregator)IsStopped()bool{
return atomic.LoadInt32(&(a.isStop)) == 1
}
//stop
func (a *aggregator) Stop(){
a.stopped <- struct{}{}
close(a.dataChan)
a.wait.Wait()
//range data chan
for v := range a.dataChan{
a.aggregatorData = append(a.aggregatorData, v)
}
a.handleLeft()
}
//handle left data
func (a *aggregator) handleLeft(){
leftLen := len(a.aggregatorData)
if leftLen == 0 {
return
}
//smaller then processCount
if leftLen < a.processCount{
a.handler(a.aggregatorData)
a.aggregatorData = nil
return
}
//bigger then processCount
gap := len(a.aggregatorData)/a.processCount + 1
for i:=1;i < gap; i++{
start := (i-1)*a.processCount
end := i*a.processCount
toBeProcess := a.aggregatorData[start:end]
toBeProcessLen := len(toBeProcess)
if toBeProcessLen <= 0{
break
}
a.handler(toBeProcess)
//smaller than a.processCount
if toBeProcessLen < a.processCount {
break
}
}
a.aggregatorData = nil
}