mirror of
https://github.com/rocky-linux/peridot.git
synced 2024-12-25 03:48:30 +00:00
45 lines
1.5 KiB
Go
45 lines
1.5 KiB
Go
|
// Copyright 2016 Michal Witkowski. All Rights Reserved.
|
||
|
// See LICENSE for licensing terms.
|
||
|
|
||
|
package grpc_retry
|
||
|
|
||
|
import (
|
||
|
"time"
|
||
|
|
||
|
"github.com/grpc-ecosystem/go-grpc-middleware/util/backoffutils"
|
||
|
)
|
||
|
|
||
|
// BackoffLinear is very simple: it waits for a fixed period of time between calls.
|
||
|
func BackoffLinear(waitBetween time.Duration) BackoffFunc {
|
||
|
return func(attempt uint) time.Duration {
|
||
|
return waitBetween
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// BackoffLinearWithJitter waits a set period of time, allowing for jitter (fractional adjustment).
|
||
|
//
|
||
|
// For example waitBetween=1s and jitter=0.10 can generate waits between 900ms and 1100ms.
|
||
|
func BackoffLinearWithJitter(waitBetween time.Duration, jitterFraction float64) BackoffFunc {
|
||
|
return func(attempt uint) time.Duration {
|
||
|
return backoffutils.JitterUp(waitBetween, jitterFraction)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// BackoffExponential produces increasing intervals for each attempt.
|
||
|
//
|
||
|
// The scalar is multiplied times 2 raised to the current attempt. So the first
|
||
|
// retry with a scalar of 100ms is 100ms, while the 5th attempt would be 1.6s.
|
||
|
func BackoffExponential(scalar time.Duration) BackoffFunc {
|
||
|
return func(attempt uint) time.Duration {
|
||
|
return scalar * time.Duration(backoffutils.ExponentBase2(attempt))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// BackoffExponentialWithJitter creates an exponential backoff like
|
||
|
// BackoffExponential does, but adds jitter.
|
||
|
func BackoffExponentialWithJitter(scalar time.Duration, jitterFraction float64) BackoffFunc {
|
||
|
return func(attempt uint) time.Duration {
|
||
|
return backoffutils.JitterUp(scalar*time.Duration(backoffutils.ExponentBase2(attempt)), jitterFraction)
|
||
|
}
|
||
|
}
|