github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/go-grpc-middleware/util/backoffutils/backoff.go (about) 1 // Copyright 2016 Michal Witkowski. All Rights Reserved. 2 // See LICENSE for licensing terms. 3 4 /* 5 Backoff Helper Utilities 6 7 Implements common backoff features. 8 */ 9 package backoffutils 10 11 import ( 12 "math/rand" 13 "time" 14 ) 15 16 // JitterUp adds random jitter to the duration. 17 // 18 // This adds or substracts time from the duration within a given jitter fraction. 19 // For example for 10s and jitter 0.1, it will returna time within [9s, 11s]) 20 func 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. 26 func ExponentBase2(a uint) uint { 27 return (1 << a) >> 1 28 }