Skip to content

Commit

Permalink
replace pow util function with bit operation
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinForReal committed Jun 2, 2023
1 parent cc8963d commit 4fb046c
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions sdk/azcore/runtime/policy_retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,11 @@ func setDefaults(o *policy.RetryOptions) {
}

func calcDelay(o policy.RetryOptions, try int32) time.Duration { // try is >=1; never 0
pow := func(number int64, exponent int32) int64 { // pow is nested helper function
var result int64 = 1
for n := int32(0); n < exponent; n++ {
result *= number
}
return result
pow := func(exponent int32) uint64 { // pow is nested helper function
return 1 << exponent
}

delay := time.Duration(pow(2, try)-1) * o.RetryDelay
delay := time.Duration(pow(try)-1) * o.RetryDelay

// Introduce some jitter: [0.0, 1.0) / 2 = [0.0, 0.5) + 0.8 = [0.8, 1.3)
delay = time.Duration(delay.Seconds() * (rand.Float64()/2 + 0.8) * float64(time.Second)) // NOTE: We want math/rand; not crypto/rand
Expand Down

0 comments on commit 4fb046c

Please sign in to comment.