Skip to content

Commit

Permalink
Merge pull request #117 from adjust/jitteredDuration-all-cases
Browse files Browse the repository at this point in the history
consume: use jittered poll duration in all cases
  • Loading branch information
psampaz authored Mar 21, 2022
2 parents d41de3e + 68ca549 commit 6edd67b
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,8 @@ func (queue *redisQueue) consume() {
case queue.errChan <- &ConsumeError{RedisErr: err, Count: errorCount}:
default:
}

time.Sleep(jitteredDuration(queue.pollDuration))
}
time.Sleep(jitteredDuration(queue.pollDuration))
}
}

Expand All @@ -199,8 +198,6 @@ func (queue *redisQueue) consumeBatch() error {

batchSize := queue.prefetchLimit - unackedCount
if batchSize <= 0 {
// already at prefetch limit, wait for consumers to finish
time.Sleep(queue.pollDuration) // sleep before retry
return nil
}

Expand All @@ -213,8 +210,6 @@ func (queue *redisQueue) consumeBatch() error {

payload, err := queue.redisClient.RPopLPush(queue.readyKey, queue.unackedKey)
if err == ErrorNotFound {
// ready list currently empty, wait for new deliveries
time.Sleep(queue.pollDuration)
return nil
}

Expand Down Expand Up @@ -540,6 +535,6 @@ func (queue *redisQueue) ensureConsuming() error {

// jitteredDuration calculates and returns a value that is +/-10% the input duration
func jitteredDuration(duration time.Duration) time.Duration {
factor := 0.9 + randSrc.Float64() * 0.2 // a jitter factor between 0.9 and 1.1 (+-10%)
factor := 0.9 + randSrc.Float64()*0.2 // a jitter factor between 0.9 and 1.1 (+-10%)
return time.Duration(float64(duration) * factor)
}

0 comments on commit 6edd67b

Please sign in to comment.