1// Copyright 2018 The OPA Authors.  All rights reserved.
2// Use of this source code is governed by an Apache2
3// license that can be found in the LICENSE file.
4
5package util
6
7import (
8	"math/rand"
9	"time"
10)
11
12// DefaultBackoff returns a delay with an expontential backoff based on the
13// number of retries.
14func DefaultBackoff(base, max float64, retries int) time.Duration {
15	return Backoff(base, max, .2, 1.6, retries)
16}
17
18// Backoff returns a delay with an expontential backoff based on the number of
19// retries. Same algorithm used in gRPC.
20func Backoff(base, max, jitter, factor float64, retries int) time.Duration {
21	if retries == 0 {
22		return 0
23	}
24
25	backoff, max := float64(base), float64(max)
26	for backoff < max && retries > 0 {
27		backoff *= factor
28		retries--
29	}
30	if backoff > max {
31		backoff = max
32	}
33
34	// Randomize backoff delays so that if a cluster of requests start at
35	// the same time, they won't operate in lockstep.
36	backoff *= 1 + jitter*(rand.Float64()*2-1)
37	if backoff < 0 {
38		return 0
39	}
40
41	return time.Duration(backoff)
42}
43