-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_framework.go
More file actions
254 lines (229 loc) · 6.87 KB
/
Copy pathqueue_framework.go
File metadata and controls
254 lines (229 loc) · 6.87 KB
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
package mqpf
import (
"fmt"
cl "github.com/akimimi/config-loader"
"github.com/aliyun/aliyun-mns-go-sdk"
"github.com/gogap/logs"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
const defaultStopQueueSeconds = 90
// QueueFramework is the interface that should be satisfied for any modified queue framework.
type QueueFramework interface {
RegisterBreakQueueOsSingal(sigs ...os.Signal)
GetConfig() cl.QueueConfig
GetStatistic() *Statistic
SetQueue(q ali_mns.AliMNSQueue)
HasValidQueue() bool
SetEventHandler(h QueueEventHandlerInterface)
HasEventHandler() bool
Launch()
Stop()
WaitProcessingSeconds(pop bool) int
}
type queueFramework struct {
queue ali_mns.AliMNSQueue
config cl.QueueConfig
handler QueueEventHandlerInterface
breakByUser bool
stat Statistic
perfLog performanceLog
stopQueueSeconds int
waitProcessingSeconds int
previousWaitProcessingSeconds int
}
// NewQueueFramework creates and returns a queue framework which consists with a provided event handler,
// and configures the queue framework by provided QueueConfig.
func NewQueueFramework(q ali_mns.AliMNSQueue, c cl.QueueConfig, h QueueEventHandlerInterface) *queueFramework {
qf := queueFramework{config: c, handler: &DefaultEventHandler{}, stopQueueSeconds: defaultStopQueueSeconds}
qf.SetQueue(q)
qf.SetEventHandler(h)
return &qf
}
func (qf *queueFramework) GetConfig() cl.QueueConfig {
return qf.config
}
func (qf *queueFramework) GetStatistic() *Statistic {
return &qf.stat
}
func (qf *queueFramework) SetQueue(q ali_mns.AliMNSQueue) {
if q != nil {
qf.queue = q
}
}
func (qf *queueFramework) HasValidQueue() bool {
return qf.queue != nil
}
func (qf *queueFramework) SetEventHandler(h QueueEventHandlerInterface) {
if h != nil {
qf.handler = h
}
}
func (qf *queueFramework) HasEventHandler() bool {
return qf.handler != nil
}
func (qf *queueFramework) Launch() {
qf.handler.BeforeLaunch(qf)
if !qf.HasValidQueue() || !qf.HasEventHandler() {
return
}
wg := sync.WaitGroup{} // wait for OnMessageReceived returns
for {
if qf.breakByUser {
break
}
qf.stat.Loop()
qf.handler.OnWaitingMessage(qf)
endChan, respChan := make(chan int), make(chan ali_mns.BatchMessageReceiveResponse)
errChan := make(chan error)
handling := qf.stat.Fetch("msgreceived") - qf.stat.Fetch("success") - qf.stat.Fetch("error")
if handling < uint64(qf.config.MaxProcessingMessage) { // can handle message
go func() {
select {
case resp := <-respChan:
if qf.config.Verbose {
logs.Debug("Batch Received", len(resp.Messages), "Messages")
}
for mid := range resp.Messages {
message := resp.Messages[mid]
wg.Add(1)
go qf.OnMessageReceived(&message, &wg)
}
qf.ResetWaitProcessingSeconds()
case err := <-errChan:
qf.stat.QueueError()
qf.handler.OnError(err, nil, nil, nil, qf)
qf.ResetWaitProcessingSeconds()
}
endChan <- 1
}()
qf.queue.BatchReceiveMessage(respChan, errChan,
int32(qf.config.RecvMessageBatchSize), int64(qf.config.PollingWaitSeconds))
} else { // too many messages are being processed, wait for a few seconds
go func() {
select {
case <-time.After(time.Duration(qf.WaitProcessingSeconds(true)) * time.Second):
qf.handler.OnRecoverProcessing(qf)
}
endChan <- 1
}()
if qf.WaitProcessingSeconds(false) > qf.config.OverloadBreakSeconds {
break // break the for loop to avoid non-stop waiting
} else {
qf.stat.Wait()
qf.handler.OnWaitingProcessing(qf)
}
}
<-endChan
}
// wait for every OnMessageReceived returns in no longer than qf.stopQueueSeconds
waitGroupFinished := make(chan bool)
go func() {
wg.Wait()
waitGroupFinished <- true
}()
select {
case <-waitGroupFinished:
case <-time.After(time.Duration(qf.stopQueueSeconds) * time.Second):
}
qf.breakByUser = false
qf.handler.AfterLaunch(qf)
}
func (qf *queueFramework) Stop() {
qf.breakByUser = true
}
func (qf *queueFramework) WaitProcessingSeconds(pop bool) int {
nw, np := 0, 0
if qf.waitProcessingSeconds == 0 {
nw, np = 1, 1
} else {
nw, np = qf.waitProcessingSeconds+qf.previousWaitProcessingSeconds, qf.waitProcessingSeconds
}
if pop {
qf.waitProcessingSeconds = nw
qf.previousWaitProcessingSeconds = np
}
return nw
}
func (qf *queueFramework) ResetWaitProcessingSeconds() {
qf.waitProcessingSeconds, qf.previousWaitProcessingSeconds = 0, 0
}
func (qf *queueFramework) OnMessageReceived(resp *ali_mns.MessageReceiveResponse, wg *sync.WaitGroup) {
defer wg.Done()
qf.stat.MessageReceived()
qf.changeVisibility(resp, func(vret *ali_mns.MessageVisibilityChangeResponse) {
bodyBytes, err := qf.handler.ParseMessageBody(resp)
if err == nil {
finishChan := make(chan error)
if qf.GetConfig().ConsumeTimeout > 0 { // limit ConsumeMessageDuration
go func() {
e := qf.handler.ConsumeMessage(bodyBytes, resp)
finishChan <- e
}()
select {
case e := <-finishChan:
err = e
case <-time.After(time.Duration(qf.GetConfig().ConsumeTimeout) * time.Second):
err = fmt.Errorf("timeout for ConsumeMessage in %d seconds", qf.GetConfig().ConsumeTimeout)
}
} else {
err = qf.handler.ConsumeMessage(bodyBytes, resp)
}
if err == nil {
if err = qf.queue.DeleteMessage(vret.ReceiptHandle); err == nil {
qf.stat.HandleSuccess()
}
}
} else {
qf.handler.OnParseMessageBodyFailed(err, resp)
}
if err != nil {
qf.stat.HandleError()
qf.handler.OnError(err, qf.queue, resp, vret, qf)
}
})
}
func (qf *queueFramework) changeVisibility(resp *ali_mns.MessageReceiveResponse,
onSuccess func(vret *ali_mns.MessageVisibilityChangeResponse)) {
qf.handler.BeforeChangeVisibility(qf.queue, resp)
if vret, e := qf.queue.ChangeMessageVisibility(resp.ReceiptHandle, int64(qf.config.VisibilityTimeout)); e == nil {
qf.handler.AfterChangeVisibility(qf.queue, resp, &vret)
onSuccess(&vret)
} else {
qf.stat.HandleError()
qf.handler.OnChangeVisibilityFailed(qf.queue, resp, &vret)
qf.handler.OnError(e, qf.queue, resp, &vret, qf)
}
}
func (qf *queueFramework) RegisterBreakQueueOsSingal(sigs ...os.Signal) {
signalChan := make(chan os.Signal, 1)
if sigs == nil {
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGSTOP, syscall.SIGHUP)
} else {
signal.Notify(signalChan, sigs...)
}
go qf.listenOsSignal(signalChan)
}
func (qf *queueFramework) listenOsSignal(signalChan chan os.Signal) {
for {
select {
case s := <-signalChan:
switch s {
case syscall.SIGINT:
fallthrough
case syscall.SIGTERM:
fallthrough
case syscall.SIGSTOP:
logs.Info("Signal ", s.String(), " received, stopping queue daemon.....")
qf.Stop()
case syscall.SIGHUP:
logs.Info(fmt.Sprintf("User Signal Received (%v)", s))
logs.Info(qf.stat.String())
logs.Info(qf.stat.Performance())
}
}
}
}