Repository navigation
Expand file tree
/
Copy pathworker.go
More file actions
299 lines (268 loc) · 6.49 KB
/
Copy pathworker.go
File metadata and controls
299 lines (268 loc) · 6.49 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
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
package queue
import (
"sync/atomic"
"time"
)
type WorkerStatus uint32
const (
WorkerStatusIdle WorkerStatus = iota
WorkerStatusActive
WorkerStatusSleep
)
func (s WorkerStatus) String() string {
switch s {
case WorkerStatusIdle:
return "idle"
case WorkerStatusActive:
return "active"
case WorkerStatusSleep:
return "sleep"
}
return "unknown"
}
type signal uint32
const (
sigInit signal = iota
sigSleep
sigWakeup
sigStop
sigForceStop
)
// Internal worker implementation.
type worker struct {
// Index of worker in the pool.
// For logging purposes.
idx uint32
// Status of the worker.
status WorkerStatus
// Channel to control sleep and stop states.
// This channel delivers two signals:
// * wakeup for slept workers
// * force close for active workers
ctl chan struct{}
// Channel to notify worker finishes his life.
eol chan struct{}
// Liveness flag: 1 while the worker goroutine is running (or about to start), 0 otherwise.
running int32
// Last signal timestamp.
lastTS int64
// Worker instance.
proc Worker
// Config of the queue.
config *Config
}
// Make new idle worker.
func makeWorker(idx uint32, config *Config) *worker {
w := &worker{
idx: idx,
status: WorkerStatusIdle,
ctl: make(chan struct{}, 1),
eol: make(chan struct{}, 1),
proc: config.Worker,
config: config,
}
return w
}
// Send signal to worker.
func (w *worker) signal(sig signal) {
atomic.StoreInt64(&w.lastTS, time.Now().UnixNano())
switch sig {
case sigInit:
w.init()
case sigSleep:
w.sleep()
case sigWakeup:
w.wakeup()
case sigStop, sigForceStop:
w.stop(sig == sigForceStop)
}
}
// Waits to income item to process or control signal.
func (w *worker) await(queue *Queue) {
defer func() {
// Notify life is ended.
select {
case w.eol <- struct{}{}:
default:
}
// The goroutine is no longer running. Only now it is safe to restart the worker: the end-of-life
// signal is already in place, so the next life's init() will drain it.
atomic.StoreInt32(&w.running, 0)
}()
for {
switch w.getStatus() {
case WorkerStatusSleep:
// Wait config.SleepInterval.
<-w.ctl
case WorkerStatusActive:
// Read itm from the stream.
itm, ok := queue.engine.dequeue()
if !ok {
// Stream is closed. Immediately stop and exit.
w.stop(true)
return
}
// Check deadline.
if itm.deadline > 0 {
now := queue.clk().Now().UnixNano()
if now-itm.deadline >= 0 {
if queue.CheckBit(flagLeaky) && w.c().DeadlineToDLQ {
_ = w.c().DLQ.Enqueue(itm.payload)
}
w.mw().QueueDeadline()
continue
}
}
var intr bool
// Check delayed execution.
if itm.delay > 0 {
now := queue.clk().Now().UnixNano()
if delta := time.Duration(itm.delay - now); delta > 0 {
// Processing time has not yet arrived. So wait till delay ends.
select {
case <-time.After(delta):
break
case <-w.ctl:
// Waiting interrupted due to force close signal.
intr = true
// Calculate real wait time.
delta = time.Duration(queue.clk().Now().UnixNano() - now)
break
}
w.mw().WorkerWait(w.idx, delta)
}
}
if intr {
// Return item back to the queue due to interrupt signal.
_ = queue.renqueue(&itm)
return
}
// Forward itm to dequeuer.
now := w.config.Clock.Now()
err := w.proc.Do(itm.payload)
w.mw().QueueExec(w.config.Clock.Now().Sub(now))
if err != nil {
// Processing failed.
if itm.retries < w.c().MaxRetries {
// Try to retry processing if possible.
delay := w.c().Backoff.Next(w.c().RetryInterval, int(itm.retries))
if delay > 0 {
// Apply jitter logic to precalculated delay.
delay = w.c().Jitter.Apply(delay)
select {
case <-time.After(delay):
// Wait for interval calculated by Backoff + Jitter.
break
case <-w.ctl:
intr = true
}
}
if !intr {
w.mw().QueuePull() // Item came from queue for short time.
w.mw().QueueRetry(delay)
itm.retries++
itm.delay = 0 // Clear item timestamp for 2nd, 3rd, ... attempts.
_ = queue.renqueue(&itm)
}
} else if queue.CheckBit(flagLeaky) && w.c().FailToDLQ {
_ = w.c().DLQ.Enqueue(itm.payload)
w.mw().QueueLeak(LeakDirectionFront.String())
continue
}
}
w.mw().QueuePull()
case WorkerStatusIdle:
// Exit on idle status.
return
}
}
}
// Start idle worker.
func (w *worker) init() {
if w.l() != nil {
w.l().Printf("worker #%d init\n", w.idx)
}
// Drain previous value from EOF.
select {
case <-w.eol:
default:
}
// Mark the worker as alive before it becomes visible as Idle-restartable.
atomic.StoreInt32(&w.running, 1)
w.setStatus(WorkerStatusActive)
w.mw().WorkerInit(w.idx)
}
// Put worker to the sleep.
func (w *worker) sleep() {
if w.l() != nil {
w.l().Printf("worker #%d sleep\n", w.idx)
}
w.setStatus(WorkerStatusSleep)
w.mw().WorkerSleep(w.idx)
}
// Wakeup sleeping worker.
func (w *worker) wakeup() {
if w.l() != nil {
w.l().Printf("worker #%d wakeup\n", w.idx)
}
w.setStatus(WorkerStatusActive)
w.mw().WorkerWakeup(w.idx)
w.notifyCtl()
}
// Stop (or force stop) worker.
func (w *worker) stop(force bool) {
if w.l() != nil {
msg := "worker #%d stop\n"
if force {
msg = "worker #%d force stop\n"
}
w.l().Printf(msg, w.idx)
}
w.mw().WorkerStop(w.idx, force, w.getStatus().String())
w.setStatus(WorkerStatusIdle)
w.notifyCtl()
}
// Check if worker's life has ended.
func (w *worker) done() <-chan struct{} {
return w.eol
}
// destroy closes the worker control channels.
func (w *worker) destroy() {
close(w.ctl)
close(w.eol)
}
// Check if ctl channel is empty and send signal (wakeup or force close).
func (w *worker) notifyCtl() {
// Clear ctl channel for previously undelivered signal.
select {
case <-w.ctl:
default:
}
// Send non-blocking stop signal to ctl channel.
select {
case w.ctl <- struct{}{}:
default:
}
}
// Set worker status.
func (w *worker) setStatus(status WorkerStatus) {
atomic.StoreUint32((*uint32)(&w.status), uint32(status))
}
// Get worker status.
func (w *worker) getStatus() WorkerStatus {
return WorkerStatus(atomic.LoadUint32((*uint32)(&w.status)))
}
// Check if worker slept enough time.
func (w *worker) sleptEnough() bool {
dur := time.Duration(w.c().Clock.Now().UnixNano() - atomic.LoadInt64(&w.lastTS))
return dur >= w.c().SleepInterval
}
func (w *worker) c() *Config {
return w.config
}
func (w *worker) mw() MetricsWriter {
return w.config.MetricsWriter
}
func (w *worker) l() Logger {
return w.config.Logger
}