blob: 4e69a6305aa93045e10fe972f99e792aefbddb52 [file] [log] [blame]
nikesh.krishnan6dd882b2023-03-14 10:02:41 +05301// Copyright 2016 Michal Witkowski. All Rights Reserved.
2// See LICENSE for licensing terms.
3
4/*
5Backoff Helper Utilities
6
7Implements common backoff features.
8*/
9package backoffutils
10
11import (
12 "math/rand"
13 "time"
14)
15
16// JitterUp adds random jitter to the duration.
17//
18// This adds or subtracts time from the duration within a given jitter fraction.
19// For example for 10s and jitter 0.1, it will return a time within [9s, 11s])
20func JitterUp(duration time.Duration, jitter float64) time.Duration {
21 multiplier := jitter * (rand.Float64()*2 - 1)
22 return time.Duration(float64(duration) * (1 + multiplier))
23}
24
25// ExponentBase2 computes 2^(a-1) where a >= 1. If a is 0, the result is 0.
26func ExponentBase2(a uint) uint {
27 return (1 << a) >> 1
28}