Files
releases/util/backoff.go
T
Stephan Renatus c9ec05d3fe bump: go 1.19.5 -> 1.20.1
This PR bumps go to 1.20.1 (https://go.dev/doc/go1.20) which
addresses the following vulnerabilities:
https://pkg.go.dev/vuln/GO-2023-1571
https://pkg.go.dev/vuln/GO-2023-1570
https://pkg.go.dev/vuln/GO-2023-1568

As part of the migration, general Golang
stdlib deprecations and test failures were addressed as well.
Some of those changes are:

* Bump golangci-lint for support with go1.20
* Migrate rand.Seed() calls to the newer rand.New(rand.NewSource(seed))

Co-authored-by: Stephan Renatus <stephan@styra.com>
Co-authored-by: Philip Conrad <philipaconrad@gmail.com>

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2023-02-21 13:51:23 -08:00

54 lines
1.5 KiB
Go

// Copyright 2018 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package util
import (
"math/rand"
"time"
)
func init() {
// NOTE(sr): We don't need good random numbers here; it's used for jittering
// the backup timing a bit. But anyways, let's make it random enough; without
// a call to rand.Seed() we'd get the same stream of numbers for each program
// run. (Or not, if some other packages happens to seed the global randomness
// source.)
// Note(philipc): rand.Seed() was deprecated in Go 1.20, so we've switched to
// using the recommended rand.New(rand.NewSource(seed)) style.
rand.New(rand.NewSource(time.Now().UnixNano()))
}
// DefaultBackoff returns a delay with an exponential backoff based on the
// number of retries.
func DefaultBackoff(base, max float64, retries int) time.Duration {
return Backoff(base, max, .2, 1.6, retries)
}
// Backoff returns a delay with an exponential backoff based on the number of
// retries. Same algorithm used in gRPC.
func Backoff(base, max, jitter, factor float64, retries int) time.Duration {
if retries == 0 {
return 0
}
backoff, max := base, max
for backoff < max && retries > 0 {
backoff *= factor
retries--
}
if backoff > max {
backoff = max
}
// Randomize backoff delays so that if a cluster of requests start at
// the same time, they won't operate in lockstep.
backoff *= 1 + jitter*(rand.Float64()*2-1)
if backoff < 0 {
return 0
}
return time.Duration(backoff)
}