cashless2ecash

cashless2ecash: pay with cards for digital cash (experimental)
Log | Files | Refs | README

commit a6742512f6c0e20de02e7c19f94f7dde56b38d58
parent 1360db78585be5e1785ee7968d8757f4ef5f4dae
Author: Joel-Haeberli <haebu@rubigen.ch>
Date:   Wed, 22 May 2024 00:08:00 +0200

fix: last retry will be set infinitely often

Diffstat:
Mc2ec/exponential-backoff.go | 24++++++++++++++++++------
1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/c2ec/exponential-backoff.go b/c2ec/exponential-backoff.go @@ -1,9 +1,10 @@ package main import ( + "crypto/rand" "fmt" "math" - "math/rand" + "math/big" "time" ) @@ -50,15 +51,26 @@ func randomizeBackoff(backoff int64) int64 { // it's about randomizing on millisecond base... we mustn't care about rounding threshold := int64(math.Floor(float64(backoff)*RANDOMIZATION_THRESHOLD_FACTOR)) + 1 // +1 to guarantee positive threshold - randomizedThreshold := rand.Int63n(threshold) - subtract := rand.Int31n(100) // upper boundary is exclusive (value is between 0 and 99) + randomizedThreshold, err := rand.Int(rand.Reader, big.NewInt(backoff+threshold)) + if err != nil { + LogError("exponential-backoff", err) + } + subtract, err := rand.Int(rand.Reader, big.NewInt(100)) // upper boundary is exclusive (value is between 0 and 99) + if err != nil { + LogError("exponential-backoff", err) + } + + if !randomizedThreshold.IsInt64() { + LogWarn("exponential-backoff", "the threshold is not int64") + return backoff + } - if subtract < 50 { - subtracted := backoff - randomizedThreshold + if subtract.Int64() < 50 { + subtracted := backoff - randomizedThreshold.Int64() if subtracted < 0 { return 0 } return subtracted } - return backoff + randomizedThreshold + return backoff + randomizedThreshold.Int64() }