add randomness to reconnections
This commit is contained in:
1 parent
ff21aad59e
commit
c36c561e35
6 files changed
+59
-72
No files matched your search
@@ -0,0 +1,26 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"math/rand/v2"
|
||||
"time"
|
||||
)
|
||||
|
||||
const backoffJitterFloorDivisor = 4
|
||||
|
||||
func BackoffWithJitter(attempt int, initialDelay, maxDelay time.Duration) time.Duration {
|
||||
if initialDelay <= 0 {
|
||||
return 0
|
||||
}
|
||||
if maxDelay < initialDelay {
|
||||
maxDelay = initialDelay
|
||||
}
|
||||
if attempt < 0 {
|
||||
attempt = 0
|
||||
}
|
||||
ceiling := maxDelay
|
||||
if shifted := initialDelay << uint(attempt); shifted > 0 && shifted < maxDelay {
|
||||
ceiling = shifted
|
||||
}
|
||||
floor := ceiling / backoffJitterFloorDivisor
|
||||
return floor + time.Duration(rand.Int64N(int64(ceiling-floor)+1))
|
||||
}
|
||||
Reference in new issue
Block a user