Skip to content

Commit

Permalink
Fix queue processor throttling logic (#3195)
Browse files Browse the repository at this point in the history
  • Loading branch information
yycptt committed Aug 12, 2022
1 parent f8a59f7 commit dae8a51
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions service/history/queueProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ type (
scheduler queues.Scheduler
rescheduler queues.Rescheduler

lastPollTime time.Time
backoffTimer *time.Timer
readTaskRetrier backoff.Retrier
lastPollTime time.Time
backoffTimerLock sync.Mutex
backoffTimer *time.Timer
readTaskRetrier backoff.Retrier

notifyCh chan struct{}
status int32
Expand Down Expand Up @@ -261,21 +262,23 @@ func (p *queueProcessorBase) processBatch() {

func (p *queueProcessorBase) verifyReschedulerSize() bool {
passed := p.rescheduler.Len() < p.options.MaxReschdulerSize()
if passed && p.backoffTimer != nil {
p.backoffTimer.Stop()
p.backoffTimer = nil
}
if !passed && p.backoffTimer == nil {
if !passed {
p.throttle(p.options.PollBackoffInterval())
}

return passed
}

func (p *queueProcessorBase) throttle(duration time.Duration) {
p.backoffTimerLock.Lock()
defer p.backoffTimerLock.Unlock()

if p.backoffTimer == nil {
p.backoffTimer = time.AfterFunc(duration, func() {
p.backoffTimerLock.Lock()
defer p.backoffTimerLock.Unlock()

p.notifyNewTask() // re-enqueue the event
p.backoffTimer = nil
})
}
}
Expand Down

0 comments on commit dae8a51

Please sign in to comment.