forked from vmihailenco/taskq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.go
950 lines (787 loc) · 18.8 KB
/
consumer.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
package taskq
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/go-redis/redis_rate/v9"
"github.com/bsm/redislock"
"github.com/vmihailenco/taskq/v3/internal"
)
const stopTimeout = 30 * time.Second
var ErrAsyncTask = errors.New("taskq: async task")
type Delayer interface {
Delay() time.Duration
}
type ConsumerStats struct {
NumWorker uint32
NumFetcher uint32
BufferSize uint32
Buffered uint32
InFlight uint32
Processed uint32
Retries uint32
Fails uint32
Timing time.Duration
}
//------------------------------------------------------------------------------
const (
stateInit = iota
stateStarted
stateStoppingFetchers
stateStoppingWorkers
)
// Consumer reserves messages from the queue, processes them,
// and then either releases or deletes messages from the queue.
type Consumer struct {
q Queue
opt *QueueOptions
buffer chan *Message // never closed
limiter *limiter
startStopMu sync.Mutex
state int32 // atomic
stopCh chan struct{}
cfgs *configRoulette
numWorker int32 // atomic
numFetcher int32 // atomic
fetchersWG sync.WaitGroup
workersWG sync.WaitGroup
consecutiveNumErr uint32
queueEmptyVote int32
inFlight uint32
processed uint32
fails uint32
retries uint32
timings sync.Map
hooks []ConsumerHook
}
// New creates new Consumer for the queue using provided processing options.
func NewConsumer(q Queue) *Consumer {
opt := q.Options()
c := &Consumer{
q: q,
opt: opt,
buffer: make(chan *Message, opt.BufferSize),
limiter: &limiter{
bucket: q.Name(),
limiter: opt.RateLimiter,
limit: opt.RateLimit,
},
}
return c
}
// Starts creates new Consumer and starts it.
func StartConsumer(ctx context.Context, q Queue) *Consumer {
c := NewConsumer(q)
if err := c.Start(ctx); err != nil {
panic(err)
}
return c
}
// AddHook adds a hook into message processing.
func (c *Consumer) AddHook(hook ConsumerHook) {
c.hooks = append(c.hooks, hook)
}
func (c *Consumer) Queue() Queue {
return c.q
}
func (c *Consumer) Options() *QueueOptions {
return c.opt
}
func (c *Consumer) Len() int {
return len(c.buffer)
}
// Stats returns processor stats.
func (c *Consumer) Stats() *ConsumerStats {
return &ConsumerStats{
NumWorker: uint32(atomic.LoadInt32(&c.numWorker)),
NumFetcher: uint32(atomic.LoadInt32(&c.numFetcher)),
BufferSize: uint32(cap(c.buffer)),
Buffered: uint32(len(c.buffer)),
InFlight: atomic.LoadUint32(&c.inFlight),
Processed: atomic.LoadUint32(&c.processed),
Retries: atomic.LoadUint32(&c.retries),
Fails: atomic.LoadUint32(&c.fails),
Timing: c.timing(),
}
}
func (c *Consumer) Add(msg *Message) error {
_ = c.limiter.Reserve(msg.Ctx, 1)
c.buffer <- msg
return nil
}
// Start starts consuming messages in the queue.
func (c *Consumer) Start(ctx context.Context) error {
if err := c.start(); err != nil {
return err
}
if c.opt.MinNumWorker < c.opt.MaxNumWorker {
c.cfgs = newConfigRoulette(c.opt)
cfg := c.cfgs.Select(nil, false)
c.replaceConfig(ctx, cfg)
c.fetchersWG.Add(1)
go func() {
defer c.fetchersWG.Done()
c.autotune(ctx, cfg)
}()
} else {
c.replaceConfig(ctx, &consumerConfig{
NumFetcher: 0, // fetcher is automatically started when needed
NumWorker: c.opt.MinNumWorker,
})
}
return nil
}
func (c *Consumer) start() error {
c.startStopMu.Lock()
defer c.startStopMu.Unlock()
switch atomic.LoadInt32(&c.state) {
case stateInit:
atomic.StoreInt32(&c.state, stateStarted)
c.stopCh = make(chan struct{})
case stateStarted:
return fmt.Errorf("taskq: Consumer is already started")
case stateStoppingFetchers, stateStoppingWorkers:
return fmt.Errorf("taskq: Consumer is stopping")
}
atomic.StoreInt32(&c.numFetcher, 0)
atomic.StoreInt32(&c.numWorker, 0)
return nil
}
// Stop is StopTimeout with 30 seconds timeout.
func (c *Consumer) Stop() error {
return c.StopTimeout(stopTimeout)
}
// StopTimeout waits workers for timeout duration to finish processing current
// messages and stops workers.
func (c *Consumer) StopTimeout(timeout time.Duration) error {
c.startStopMu.Lock()
defer c.startStopMu.Unlock()
switch atomic.LoadInt32(&c.state) {
case stateInit:
return fmt.Errorf("taskq: Consumer is not started")
case stateStarted:
atomic.StoreInt32(&c.state, stateStoppingFetchers)
close(c.stopCh)
case stateStoppingFetchers, stateStoppingWorkers:
return fmt.Errorf("taskq: Consumer is stopping")
}
// Stop all fetchers.
atomic.StoreInt32(&c.numFetcher, -1)
defer func() {
atomic.StoreInt32(&c.numWorker, -1)
atomic.StoreInt32(&c.state, stateInit)
}()
timer := time.NewTimer(timeout)
defer timer.Stop()
done := make(chan struct{}, 1)
go func() {
c.fetchersWG.Wait()
done <- struct{}{}
}()
var firstErr error
select {
case <-done:
case <-timer.C:
firstErr = fmt.Errorf("taskq: %s: fetchers are not stopped after %s", c, timeout)
}
if !atomic.CompareAndSwapInt32(&c.state, stateStoppingFetchers, stateStoppingWorkers) {
panic("not reached")
}
if firstErr != nil {
return firstErr
}
go func() {
c.workersWG.Wait()
done <- struct{}{}
}()
select {
case <-done:
case <-timer.C:
return fmt.Errorf("taskq: %s: workers are not stopped after %s", c, timeout)
}
return nil
}
func (c *Consumer) paused() time.Duration {
if c.opt.PauseErrorsThreshold == 0 ||
atomic.LoadUint32(&c.consecutiveNumErr) < uint32(c.opt.PauseErrorsThreshold) {
return 0
}
return time.Minute
}
func (c *Consumer) addWorker(ctx context.Context, id int32) bool {
c.startStopMu.Lock()
defer c.startStopMu.Unlock()
if atomic.CompareAndSwapInt32(&c.numWorker, id, id+1) {
c.workersWG.Add(1)
go func() {
defer c.workersWG.Done()
c.worker(ctx, id)
}()
return true
}
return false
}
func (c *Consumer) removeWorker(id int32) bool { //nolint:unused
return atomic.CompareAndSwapInt32(&c.numWorker, id+1, id)
}
func (c *Consumer) addFetcher(ctx context.Context, id int32) bool {
c.startStopMu.Lock()
defer c.startStopMu.Unlock()
if atomic.CompareAndSwapInt32(&c.numFetcher, id, id+1) {
c.fetchersWG.Add(1)
go func() {
defer c.fetchersWG.Done()
c.fetcher(ctx, id)
}()
return true
}
return false
}
func (c *Consumer) ensureFetcher(ctx context.Context) {
if atomic.LoadInt32(&c.numFetcher) == 0 {
c.addFetcher(ctx, 0)
}
}
func (c *Consumer) removeFetcher(num int32) bool {
return atomic.CompareAndSwapInt32(&c.numFetcher, num+1, num)
}
// ProcessAll starts workers to process messages in the queue and then stops
// them when all messages are processed.
func (c *Consumer) ProcessAll(ctx context.Context) error {
if err := c.Start(ctx); err != nil {
return err
}
var prev *ConsumerStats
var noWork int
for {
st := c.Stats()
if prev != nil &&
st.Buffered == 0 &&
st.InFlight == 0 &&
st.Processed == prev.Processed {
noWork++
if noWork == 2 {
break
}
} else {
noWork = 0
}
prev = st
time.Sleep(time.Second)
}
return c.Stop()
}
// ProcessOne processes at most one message in the queue.
func (c *Consumer) ProcessOne(ctx context.Context) error {
msg, err := c.reserveOne(ctx)
if err != nil {
return err
}
// TODO: wait
msg.Ctx = ctx
return c.Process(msg)
}
func (c *Consumer) reserveOne(ctx context.Context) (*Message, error) {
select {
case msg := <-c.buffer:
return msg, nil
default:
}
msgs, err := c.q.ReserveN(ctx, 1, c.opt.WaitTimeout)
if err != nil && err != internal.ErrNotSupported {
return nil, err
}
if len(msgs) == 0 {
return nil, errors.New("taskq: queue is empty")
}
if len(msgs) != 1 {
return nil, fmt.Errorf("taskq: queue returned %d messages", len(msgs))
}
return &msgs[0], nil
}
func (c *Consumer) fetcher(ctx context.Context, fetcherID int32) {
timer := time.NewTimer(time.Minute)
timer.Stop()
fetchTimeout := c.opt.ReservationTimeout
fetchTimeout -= fetchTimeout / 10
for {
if fetcherID >= atomic.LoadInt32(&c.numFetcher) {
return
}
if pauseTime := c.paused(); pauseTime > 0 {
internal.Logger.Printf("%s is automatically paused for dur=%s", c, pauseTime)
time.Sleep(pauseTime)
c.resetPause()
continue
}
timeout, err := c.fetchMessages(ctx, timer, fetchTimeout)
if err != nil {
if err == internal.ErrNotSupported {
atomic.StoreInt32(&c.numFetcher, -1)
continue
}
const backoff = time.Second
internal.Logger.Printf(
"%s fetchMessages failed: %s (sleeping for dur=%s)",
c, err, backoff)
time.Sleep(backoff)
continue
}
if timeout {
c.removeFetcher(fetcherID)
}
}
}
func (c *Consumer) fetchMessages(
ctx context.Context, timer *time.Timer, timeout time.Duration,
) (bool, error) {
size := c.limiter.Reserve(ctx, c.opt.ReservationSize)
msgs, err := c.q.ReserveN(ctx, size, c.opt.WaitTimeout)
if err != nil {
return false, err
}
if d := size - len(msgs); d > 0 {
c.limiter.Cancel(d)
c.voteQueueEmpty()
} else {
c.voteQueueFull()
}
timer.Reset(timeout)
for i := range msgs {
msg := &msgs[i]
select {
case c.buffer <- msg:
case <-timer.C:
for i := range msgs[i:] {
_ = c.q.Release(&msgs[i])
}
return true, nil
}
}
if !timer.Stop() {
<-timer.C
}
return false, nil
}
func (c *Consumer) worker(ctx context.Context, workerID int32) {
var lock *redislock.Lock
defer func() {
if lock != nil {
_ = lock.Release(ctx)
}
}()
timer := time.NewTimer(time.Minute)
timer.Stop()
for {
if workerID >= atomic.LoadInt32(&c.numWorker) {
return
}
if c.opt.WorkerLimit > 0 {
lock = c.lockWorker(ctx, lock, workerID)
}
msg := c.waitMessage(ctx, timer)
if msg == nil {
if atomic.LoadInt32(&c.state) >= stateStoppingWorkers {
return
}
continue
}
msg.Ctx = ctx
_ = c.Process(msg)
}
}
func (c *Consumer) waitMessage(ctx context.Context, timer *time.Timer) *Message {
const workerIdleTimeout = time.Second
select {
case msg := <-c.buffer:
return msg
default:
}
c.ensureFetcher(ctx)
timer.Reset(workerIdleTimeout)
select {
case msg := <-c.buffer:
if !timer.Stop() {
<-timer.C
}
return msg
case <-timer.C:
c.voteQueueEmpty()
return nil
case <-c.stopCh:
return nil
}
}
// Process is low-level API to process message bypassing the internal queue.
func (c *Consumer) Process(msg *Message) error {
atomic.AddUint32(&c.inFlight, 1)
if msg.Delay > 0 {
err := c.q.Add(msg)
if err != nil {
return err
}
c.delete(msg)
return nil
}
if msg.Err != nil {
msg.Delay = -1
c.Put(msg)
return msg.Err
}
evt, err := c.beforeProcessMessage(msg)
if err != nil {
msg.Err = err
c.Put(msg)
return err
}
msg.evt = evt
start := time.Now()
msgErr := c.opt.Handler.HandleMessage(msg)
if msgErr == ErrAsyncTask {
return ErrAsyncTask
}
c.updateTiming(msg.TaskName, time.Since(start))
msg.Err = msgErr
c.Put(msg)
return msg.Err
}
func (c *Consumer) updateTiming(taskName string, x time.Duration) {
const decay = float64(1) / 10
timing := new(int64)
if v, loaded := c.timings.LoadOrStore(taskName, timing); loaded {
timing = v.(*int64)
}
for i := 0; i < 100; i++ {
oldVal := atomic.LoadInt64(timing)
var newVal int64
if oldVal != 0 {
newVal = int64(float64(oldVal)*(1-decay) + float64(x)*decay)
} else {
newVal = int64(x)
}
if atomic.CompareAndSwapInt64(timing, oldVal, newVal) {
break
}
}
}
func (c *Consumer) timing() time.Duration {
var mean int64
c.timings.Range(func(_, value interface{}) bool {
x := atomic.LoadInt64(value.(*int64))
if mean != 0 {
mean = (mean + x) / 2
} else {
mean = x
}
return true
})
return time.Duration(mean)
}
func (c *Consumer) Put(msg *Message) {
if err := c.afterProcessMessage(msg); err != nil {
msg.Err = err
}
if msg.Err == nil {
c.resetPause()
atomic.AddUint32(&c.processed, 1)
c.delete(msg)
return
}
atomic.AddUint32(&c.consecutiveNumErr, 1)
if msg.Delay <= 0 {
atomic.AddUint32(&c.fails, 1)
c.delete(msg)
return
}
atomic.AddUint32(&c.retries, 1)
c.release(msg)
}
func (c *Consumer) release(msg *Message) {
if msg.Err != nil {
internal.Logger.Printf("task=%q failed (will retry=%d in dur=%s): %s",
msg.TaskName, msg.ReservedCount, msg.Delay, msg.Err)
}
err := c.q.Release(msg)
if err != nil {
internal.Logger.Printf("task=%q Release failed: %s", msg.TaskName, err)
}
atomic.AddUint32(&c.inFlight, ^uint32(0))
}
func (c *Consumer) delete(msg *Message) {
if msg.Err != nil {
internal.Logger.Printf("task=%q handler failed after retry=%d: %s",
msg.TaskName, msg.ReservedCount, msg.Err)
err := c.opt.Handler.HandleMessage(msg)
if err != nil {
internal.Logger.Printf("task=%q fallback handler failed: %s", msg.TaskName, err)
}
}
err := c.q.Delete(msg)
if err != nil {
internal.Logger.Printf("task=%q Delete failed: %s", msg.TaskName, err)
}
atomic.AddUint32(&c.inFlight, ^uint32(0))
}
// Purge discards messages from the internal queue.
func (c *Consumer) Purge() error {
for {
select {
case msg := <-c.buffer:
c.delete(msg)
default:
return nil
}
}
}
type ProcessMessageEvent struct {
Message *Message
StartTime time.Time
Stash map[interface{}]interface{}
}
type ConsumerHook interface {
BeforeProcessMessage(*ProcessMessageEvent) error
AfterProcessMessage(*ProcessMessageEvent) error
}
func (c *Consumer) beforeProcessMessage(msg *Message) (*ProcessMessageEvent, error) {
if len(c.hooks) == 0 {
return nil, nil
}
evt := &ProcessMessageEvent{
Message: msg,
StartTime: time.Now(),
}
for _, hook := range c.hooks {
if err := hook.BeforeProcessMessage(evt); err != nil {
return nil, err
}
}
return evt, nil
}
func (c *Consumer) afterProcessMessage(msg *Message) error {
if msg.evt == nil {
return nil
}
var firstErr error
for _, hook := range c.hooks {
if err := hook.AfterProcessMessage(msg.evt); err != nil && firstErr == nil {
firstErr = err
}
}
return firstErr
}
func (c *Consumer) resetPause() {
atomic.StoreUint32(&c.consecutiveNumErr, 0)
}
func (c *Consumer) lockWorker(
ctx context.Context,
lock *redislock.Lock,
workerID int32,
) *redislock.Lock {
lockTimeout := c.opt.ReservationTimeout + 10*time.Second
timer := time.NewTimer(time.Minute)
timer.Stop()
for {
var err error
if lock == nil {
key := fmt.Sprintf("%s:worker:lock:%d", c.q.Name(), workerID)
lock, err = redislock.Obtain(ctx, c.opt.Redis, key, lockTimeout, nil)
} else {
err = lock.Refresh(ctx, lockTimeout, nil)
}
if err == nil {
return lock
}
if err != redislock.ErrNotObtained {
internal.Logger.Printf("redislock.Lock failed: %s", err)
}
if lock != nil {
_ = lock.Release(ctx)
lock = nil
}
timer.Reset(500 * time.Millisecond)
select {
case <-timer.C:
case <-c.stopCh:
if lock != nil {
_ = lock.Release(ctx)
}
return nil
}
}
}
func (c *Consumer) String() string {
fnum := atomic.LoadInt32(&c.numFetcher)
wnum := atomic.LoadInt32(&c.numWorker)
inFlight := atomic.LoadUint32(&c.inFlight)
processed := atomic.LoadUint32(&c.processed)
retries := atomic.LoadUint32(&c.retries)
fails := atomic.LoadUint32(&c.fails)
timing := c.timing()
return fmt.Sprintf(
"%s %d/%d %d/%d/%d %d/%d/%d %s",
c.q.Name(),
fnum, wnum,
inFlight, len(c.buffer), cap(c.buffer),
processed, retries, fails,
timing)
}
func (c *Consumer) voteQueueEmpty() {
c.changeQueueEmptyVote(+1)
}
func (c *Consumer) voteQueueFull() {
c.changeQueueEmptyVote(-1)
}
func (c *Consumer) changeQueueEmptyVote(d int32) {
const quorum = 7
for i := 0; i < 100; i++ {
n := atomic.LoadInt32(&c.queueEmptyVote)
if (d < 0 && n <= -quorum) || (d > 0 && n >= quorum) {
break
}
if atomic.CompareAndSwapInt32(&c.queueEmptyVote, n, n+d) {
break
}
}
}
func (c *Consumer) queueEmpty() bool {
return atomic.LoadInt32(&c.queueEmptyVote) >= 3
}
func (c *Consumer) autotune(ctx context.Context, cfg *consumerConfig) {
timer := time.NewTimer(time.Hour)
defer timer.Stop()
for c.timing() == 0 {
timer.Reset(250 * time.Millisecond)
select {
case <-timer.C:
// continue
case <-c.stopCh:
return
}
}
for {
timer.Reset(c.autotuneInterval())
select {
case <-timer.C:
cfg = c.autotuneTick(ctx, cfg)
case <-c.stopCh:
return
}
}
}
func (c *Consumer) autotuneInterval() time.Duration {
const min = 500 * time.Millisecond
const max = time.Minute
d := 10 * c.timing()
if d < min {
return min
}
if d > max {
return max
}
return d
}
func (c *Consumer) autotuneTick(ctx context.Context, cfg *consumerConfig) *consumerConfig {
processed := int(atomic.LoadUint32(&c.processed))
retries := int(atomic.LoadUint32(&c.retries))
cfg.Update(processed, retries, c.timing())
if newCfg := c.cfgs.Select(cfg, c.queueEmpty()); newCfg != nil {
cfg = newCfg
c.replaceConfig(ctx, cfg)
}
return cfg
}
func (c *Consumer) replaceConfig(ctx context.Context, cfg *consumerConfig) {
if numFetcher := atomic.LoadInt32(&c.numFetcher); numFetcher != -1 {
if numFetcher > cfg.NumFetcher {
// Remove extra fetchers.
atomic.StoreInt32(&c.numFetcher, cfg.NumFetcher)
} else {
for id := numFetcher; id < cfg.NumFetcher; id++ {
if !c.addFetcher(ctx, id) {
internal.Logger.Printf("taskq: addFetcher id=%d failed", id)
}
}
}
}
numWorker := atomic.LoadInt32(&c.numWorker)
if numWorker > cfg.NumWorker {
// Remove extra workers.
atomic.StoreInt32(&c.numWorker, cfg.NumWorker)
} else {
for id := numWorker; id < cfg.NumWorker; id++ {
if !c.addWorker(ctx, id) {
internal.Logger.Printf("taskq: addWorker id=%d failed", id)
}
}
}
cfg.Reset(
int(atomic.LoadUint32(&c.processed)),
int(atomic.LoadUint32(&c.retries)))
}
//------------------------------------------------------------------------------
type limiter struct {
bucket string
limiter *redis_rate.Limiter
limit redis_rate.Limit
allowedCount uint32 // atomic
cancelled uint32 // atomic
}
func (l *limiter) Reserve(ctx context.Context, max int) int {
if l.limiter == nil || l.limit.IsZero() {
return max
}
for {
cancelled := atomic.LoadUint32(&l.cancelled)
if cancelled == 0 {
break
}
if cancelled >= uint32(max) {
if atomic.CompareAndSwapUint32(&l.cancelled, cancelled, uint32(max)-1) {
return max
}
continue
}
if atomic.CompareAndSwapUint32(&l.cancelled, cancelled, uint32(cancelled)-1) {
return int(cancelled)
}
}
for {
res, err := l.limiter.AllowAtMost(ctx, l.bucket, l.limit, max)
if err != nil {
time.Sleep(100 * time.Millisecond)
continue
}
if res.Allowed > 0 {
atomic.AddUint32(&l.allowedCount, 1)
return res.Allowed
}
atomic.StoreUint32(&l.allowedCount, 0)
time.Sleep(res.RetryAfter)
}
}
func (l *limiter) Cancel(n int) {
if l.limiter == nil {
return
}
atomic.AddUint32(&l.cancelled, uint32(n))
}
func (l *limiter) Limited() bool {
return l.limiter != nil && atomic.LoadUint32(&l.allowedCount) < 3
}
//------------------------------------------------------------------------------
func exponentialBackoff(min, max time.Duration, retry int) time.Duration {
var d time.Duration
if retry > 0 {
d = min << uint(retry-1)
}
if d < min {
return min
}
if d > max {
return max
}
return d
}