mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-27 18:55:54 -06:00
3be1d08b87
golint is deprecated. The author of the code no longer supports the codebase. golangci-lint is faster than golint, and is in use by other opa repositories (e.g. Gatekeeper). This commit changes tools.go to reference golangci (so it ends up in vendor) and modifies check-lint to use golangci instead. Breaking API Changes: - plugins/rest/rest.go: Fix typo "AllowInsureTLS" -> "AllowInsecureTLS" - storage/errors.go: Removed unused IndexingNotSupportedErr Signed-off-by: Will Beason <willbeason@google.com>
43 lines
1021 B
Go
43 lines
1021 B
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"
|
|
)
|
|
|
|
// 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)
|
|
}
|