Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
init local randomizer for jitter (#11)
Browse files Browse the repository at this point in the history
* init local randomizer for jitter
  • Loading branch information
peakle committed Apr 6, 2022
1 parent 4417f88 commit bb38b40
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,10 @@ func (wp *WorkerPool) spawnWorker() {
wp.wg.Add(1)

go func() {
randomizer := rand.New(rand.NewSource(time.Now().UnixNano()))
// https://en.wikipedia.org/wiki/Exponential_backoff
// nolint:gosec
jitter := time.Millisecond * time.Duration(rand.Intn(wp.cfg.TimeoutJitter))
timeout := wp.cfg.KeepAliveTimeout + jitter

ticker := time.NewTicker(timeout)
ticker := time.NewTicker(wp.cfg.KeepAliveTimeout + time.Millisecond*time.Duration(randomizer.Intn(wp.cfg.TimeoutJitter)))
defer ticker.Stop()

defer func() {
Expand Down Expand Up @@ -200,7 +198,7 @@ func (wp *WorkerPool) spawnWorker() {
case <-ticker.C:
return
}
ticker.Reset(timeout)
ticker.Reset(wp.cfg.KeepAliveTimeout + time.Millisecond*time.Duration(randomizer.Intn(wp.cfg.TimeoutJitter)))
}
}()
}
Expand Down

0 comments on commit bb38b40

Please sign in to comment.