Debian Codegraph @

config/configretry/backoff.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package configretry // import "go.opentelemetry.io/collector/config/configretry"
5
6 import (
7 "errors"
8 "time"
9
10 "github.com/cenkalti/backoff/v5"
11 )
12
13 // NewDefaultBackOffConfig returns the default settings for RetryConfig.
14 func NewDefaultBackOffConfig() BackOffConfig {
15 return BackOffConfig{
16 Enabled: true,
17 InitialInterval: 5 * time.Second,
18 RandomizationFactor: backoff.DefaultRandomizationFactor,
19 Multiplier: backoff.DefaultMultiplier,
20 MaxInterval: 30 * time.Second,
21 MaxElapsedTime: 5 * time.Minute,
22 }
23 }
24
25 // BackOffConfig defines configuration for retrying batches in case of export failure.
26 // The current supported strategy is exponential backoff.
27 type BackOffConfig struct {
28 // Enabled indicates whether to not retry sending batches in case of export failure.
29 Enabled bool `mapstructure:"enabled"`
30 // InitialInterval the time to wait after the first failure before retrying.
31 InitialInterval time.Duration `mapstructure:"initial_interval"`
32 // RandomizationFactor is a random factor used to calculate next backoffs
33 // Randomized interval = RetryInterval * (1 ± RandomizationFactor)
34 RandomizationFactor float64 `mapstructure:"randomization_factor"`
35 // Multiplier is the value multiplied by the backoff interval bounds
36 Multiplier float64 `mapstructure:"multiplier"`
37 // MaxInterval is the upper bound on backoff interval. Once this value is reached the delay between
38 // consecutive retries will always be `MaxInterval`.
39 MaxInterval time.Duration `mapstructure:"max_interval"`
40 // MaxElapsedTime is the maximum amount of time (including retries) spent trying to send a request/batch.
41 // Once this value is reached, the data is discarded. If set to 0, the retries are never stopped.
42 MaxElapsedTime time.Duration `mapstructure:"max_elapsed_time"`
43 // prevent unkeyed literal initialization
44 _ struct{}
45 }
46
47 func (bs *BackOffConfig) Validate() error {
48 if !bs.Enabled {
49 return nil
50 }
51 if bs.InitialInterval < 0 {
52 return errors.New("'initial_interval' must be non-negative")
53 }
54 if bs.RandomizationFactor < 0 || bs.RandomizationFactor > 1 {
55 return errors.New("'randomization_factor' must be within [0, 1]")
56 }
57 if bs.Multiplier < 0 {
58 return errors.New("'multiplier' must be non-negative")
59 }
60 if bs.MaxInterval < 0 {
61 return errors.New("'max_interval' must be non-negative")
62 }
63 if bs.MaxElapsedTime < 0 {
64 return errors.New("'max_elapsed_time' must be non-negative")
65 }
66 if bs.MaxElapsedTime > 0 {
67 if bs.MaxElapsedTime < bs.InitialInterval {
68 return errors.New("'max_elapsed_time' must not be less than 'initial_interval'")
69 }
70 if bs.MaxElapsedTime < bs.MaxInterval {
71 return errors.New("'max_elapsed_time' must not be less than 'max_interval'")
72 }
73 }
74 return nil
75 }