go.temporal.io/server/common/backoff/retry.go
207 LOC · 94 covered · 113 uncovered · 43 ranges · 2242 concepts · 35 introducers · 1071 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
package backoff
import (
"context"
"math"
"slices"
"time"
commonpb "go.temporal.io/api/common/v1"
"go.temporal.io/api/serviceerror"
"go.temporal.io/server/common/clock"
"google.golang.org/protobuf/types/known/durationpb"
)
const (
throttleRetryInitialInterval = time.Second
throttleRetryMaxInterval = 10 * time.Second
throttleRetryExpirationInterval = NoInterval
)
var (
throttleRetryPolicy = NewExponentialRetryPolicy(throttleRetryInitialInterval).
WithMaximumInterval(throttleRetryMaxInterval).
WithExpirationInterval(throttleRetryExpirationInterval)
)
type (
// Operation to retry
Operation func() error
// OperationCtx plays the same role as Operation but for context-aware
// retryable functions.
OperationCtx func(context.Context) error
// IsRetryable handler can be used to exclude certain errors during retry
IsRetryable func(error) bool
)
// ThrottleRetry is a resource aware version of Retry.
// Resource exhausted error will be retried using a different throttle retry policy, instead of the specified one.
func ThrottleRetry(operation Operation, policy RetryPolicy, isRetryable IsRetryable) error {
retry.go ×1
ctxOp := func(context.Context) error { return operation() }
return ThrottleRetryContext(context.Background(), ctxOp, policy, isRetryable)
}
// ThrottleRetryContext is a context and resource aware version of Retry.
// Context timeout/cancellation errors are never retried, regardless of IsRetryable.
// Resource exhausted error will be retried using a different throttle retry policy, instead of the specified one.
// TODO: allow customizing throttle retry policy and what kind of error are categorized as throttle error.
func ThrottleRetryContext(
ctx context.Context,
operation OperationCtx,
policy RetryPolicy,
isRetryable IsRetryable,
var err error
var next time.Duration
if isRetryable == nil {
}
timeSrc := clock.NewRealTimeSource()
r := NewRetrier(policy, timeSrc)
t := NewRetrier(throttleRetryPolicy, timeSrc)
for ctx.Err() == nil {
}
}
}
}
}
select {
timer.Stop()
}
}
// always return the last error we got from operation, even if it is not useful
// this retry utility does not have enough information to do any filtering/mapping
}
}
// ThrottleRetryContextWithReturn is a context and resource aware version of Retry.
// Context timeout/cancellation errors are never retried, regardless of IsRetryable.
// Resource exhausted error will be retried using a different throttle retry policy, instead of the specified one.
// TODO: allow customizing throttle retry policy and what kind of error are categorized as throttle error.
func ThrottleRetryContextWithReturn[T any](
ctx context.Context,
fn func(context.Context) (T, error),
policy RetryPolicy,
isRetryable IsRetryable,
var zero T
var result T
var err error
var next time.Duration
if isRetryable == nil {
}
timeSrc := clock.NewRealTimeSource()
r := NewRetrier(policy, timeSrc)
t := NewRetrier(throttleRetryPolicy, timeSrc)
for ctx.Err() == nil {
if err == nil {
}
}
}
next = max(next, t.NextBackOff(err))
}
}
select {
case <-timer.C:
case <-ctx.Done():
timer.Stop()
}
}
// always return the last error we got from operation, even if it is not useful
// this retry utility does not have enough information to do any filtering/mapping
}
}
// IgnoreErrors can be used as IsRetryable handler for Retry function to exclude certain errors from the retry list
return func(err error) bool {
return !slices.Contains(errorsToExclude, err)
}
}
// BackoffCalculatorAlgorithmFunc is a function type that calculates backoff duration based on
// initial duration, coefficient, and current attempt number.
type BackoffCalculatorAlgorithmFunc func(duration *durationpb.Duration, coefficient float64, currentAttempt int32) time.Duration
// ExponentialBackoffAlgorithm calculates the backoff duration using exponential algorithm.
// The result is initInterval * (backoffCoefficient ^ (currentAttempt - 1)).
// If the calculation overflows int64, it returns the maximum possible duration. A negative result will also never be returned.
func ExponentialBackoffAlgorithm(initInterval *durationpb.Duration, backoffCoefficient float64, currentAttempt int32) time.Duration {
retry.go ×1
result := float64(initInterval.AsDuration().Nanoseconds()) * math.Pow(backoffCoefficient, float64(currentAttempt-1))
return time.Duration(max(0, min(int64(result), math.MaxInt64)))
}
// MakeBackoffAlgorithm creates a BackoffCalculatorAlgorithmFunc that returns a fixed delay if requestedDelay is non-nil,
// otherwise falls back to exponential backoff algorithm.
func MakeBackoffAlgorithm(requestedDelay *time.Duration) BackoffCalculatorAlgorithmFunc {
mutable_state_impl.go ×4
return func(duration *durationpb.Duration, coefficient float64, currentAttempt int32) time.Duration {
if requestedDelay != nil {
}
}
}
// CalculateExponentialRetryInterval calculates the retry interval using exponential backoff algorithm
func CalculateExponentialRetryInterval(retryPolicy *commonpb.RetryPolicy, attempt int32) time.Duration {
activity.go ×12
interval := ExponentialBackoffAlgorithm(retryPolicy.GetInitialInterval(), retryPolicy.GetBackoffCoefficient(), attempt)
maxInterval := retryPolicy.GetMaximumInterval()
// Cap interval to maximum if it's set
if maxInterval.AsDuration() != 0 && interval > maxInterval.AsDuration() {
interval = maxInterval.AsDuration()
}
}