| 1 |
// Code generated by gotmpl. DO NOT MODIFY. |
| 2 |
// source: internal/shared/otlp/retry/retry.go.tmpl |
| 3 |
|
| 4 |
// Copyright The OpenTelemetry Authors |
| 5 |
// SPDX-License-Identifier: Apache-2.0 |
| 6 |
|
| 7 |
// Package retry provides request retry functionality that can perform |
| 8 |
// configurable exponential backoff for transient errors and honor any |
| 9 |
// explicit throttle responses received. |
| 10 |
package retry // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/retry" |
| 11 |
|
| 12 |
import ( |
| 13 |
"context" |
| 14 |
"fmt" |
| 15 |
"time" |
| 16 |
|
| 17 |
"github.com/cenkalti/backoff/v5" |
| 18 |
) |
| 19 |
|
| 20 |
// DefaultConfig are the recommended defaults to use. |
| 21 |
var DefaultConfig = Config{ |
| 22 |
Enabled: true, |
| 23 |
InitialInterval: 5 * time.Second, |
| 24 |
MaxInterval: 30 * time.Second, |
| 25 |
MaxElapsedTime: time.Minute, |
| 26 |
} |
| 27 |
|
| 28 |
// Config defines configuration for retrying batches in case of export failure |
| 29 |
// using an exponential backoff. |
| 30 |
type Config struct { |
| 31 |
// Enabled indicates whether to not retry sending batches in case of |
| 32 |
// export failure. |
| 33 |
Enabled bool |
| 34 |
// InitialInterval the time to wait after the first failure before |
| 35 |
// retrying. |
| 36 |
InitialInterval time.Duration |
| 37 |
// MaxInterval is the upper bound on backoff interval. Once this value is |
| 38 |
// reached the delay between consecutive retries will always be |
| 39 |
// `MaxInterval`. |
| 40 |
MaxInterval time.Duration |
| 41 |
// MaxElapsedTime is the maximum amount of time (including retries) spent |
| 42 |
// trying to send a request/batch. Once this value is reached, the data |
| 43 |
// is discarded. |
| 44 |
MaxElapsedTime time.Duration |
| 45 |
} |
| 46 |
|
| 47 |
// RequestFunc wraps a request with retry logic. |
| 48 |
type RequestFunc func(context.Context, func(context.Context) error) error |
| 49 |
|
| 50 |
// EvaluateFunc returns if an error is retry-able and if an explicit throttle |
| 51 |
// duration should be honored that was included in the error. |
| 52 |
// |
| 53 |
// The function must return true if the error argument is retry-able, |
| 54 |
// otherwise it must return false for the first return parameter. |
| 55 |
// |
| 56 |
// The function must return a non-zero time.Duration if the error contains |
| 57 |
// explicit throttle duration that should be honored, otherwise it must return |
| 58 |
// a zero valued time.Duration. |
| 59 |
type EvaluateFunc func(error) (bool, time.Duration) |
| 60 |
|
| 61 |
// RequestFunc returns a RequestFunc using the evaluate function to determine |
| 62 |
// if requests can be retried and based on the exponential backoff |
| 63 |
// configuration of c. |
| 64 |
func (c Config) RequestFunc(evaluate EvaluateFunc) RequestFunc { |
| 65 |
if !c.Enabled { |
| 66 |
return func(ctx context.Context, fn func(context.Context) error) error { |
| 67 |
return fn(ctx) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
return func(ctx context.Context, fn func(context.Context) error) error { |
| 72 |
// Do not use NewExponentialBackOff since it calls Reset and the code here |
| 73 |
// must call Reset after changing the InitialInterval (this saves an |
| 74 |
// unnecessary call to Now). |
| 75 |
b := &backoff.ExponentialBackOff{ |
| 76 |
InitialInterval: c.InitialInterval, |
| 77 |
RandomizationFactor: backoff.DefaultRandomizationFactor, |
| 78 |
Multiplier: backoff.DefaultMultiplier, |
| 79 |
MaxInterval: c.MaxInterval, |
| 80 |
} |
| 81 |
b.Reset() |
| 82 |
|
| 83 |
maxElapsedTime := c.MaxElapsedTime |
| 84 |
startTime := time.Now() |
| 85 |
|
| 86 |
for { |
| 87 |
err := fn(ctx) |
| 88 |
if err == nil { |
| 89 |
return nil |
| 90 |
} |
| 91 |
|
| 92 |
retryable, throttle := evaluate(err) |
| 93 |
if !retryable { |
| 94 |
return err |
| 95 |
} |
| 96 |
|
| 97 |
// Check if context is canceled before attempting to wait and retry. |
| 98 |
if ctx.Err() != nil { |
| 99 |
return fmt.Errorf("%w: %w", ctx.Err(), err) |
| 100 |
} |
| 101 |
|
| 102 |
if maxElapsedTime != 0 && time.Since(startTime) > maxElapsedTime { |
| 103 |
return fmt.Errorf("max retry time elapsed: %w", err) |
| 104 |
} |
| 105 |
|
| 106 |
// Wait for the greater of the backoff or throttle delay. |
| 107 |
bOff := b.NextBackOff() |
| 108 |
delay := max(throttle, bOff) |
| 109 |
|
| 110 |
elapsed := time.Since(startTime) |
| 111 |
if maxElapsedTime != 0 && elapsed+throttle > maxElapsedTime { |
| 112 |
return fmt.Errorf("max retry time would elapse: %w", err) |
| 113 |
} |
| 114 |
|
| 115 |
if ctxErr := waitFunc(ctx, delay); ctxErr != nil { |
| 116 |
return fmt.Errorf("%w: %w", ctxErr, err) |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// Allow override for testing. |
| 123 |
var waitFunc = wait |
| 124 |
|
| 125 |
// wait takes the caller's context, and the amount of time to wait. It will |
| 126 |
// return nil if the timer fires before or at the same time as the context's |
| 127 |
// deadline. This indicates that the call can be retried. |
| 128 |
func wait(ctx context.Context, delay time.Duration) error { |
| 129 |
timer := time.NewTimer(delay) |
| 130 |
defer timer.Stop() |
| 131 |
|
| 132 |
select { |
| 133 |
case <-ctx.Done(): |
| 134 |
// Handle the case where the timer and context deadline end |
| 135 |
// simultaneously by prioritizing the timer expiration nil value |
| 136 |
// response. |
| 137 |
select { |
| 138 |
case <-timer.C: |
| 139 |
default: |
| 140 |
return context.Cause(ctx) |
| 141 |
} |
| 142 |
case <-timer.C: |
| 143 |
} |
| 144 |
|
| 145 |
return nil |
| 146 |
} |