github.com/Jeffail/benthos/v3@v3.65.0/lib/util/retries/type.go (about)

     1  package retries
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/cenkalti/backoff/v4"
     8  )
     9  
    10  //------------------------------------------------------------------------------
    11  
    12  // Backoff contains configuration params for the exponential backoff of the
    13  // retry mechanism.
    14  type Backoff struct {
    15  	InitialInterval string `json:"initial_interval" yaml:"initial_interval"`
    16  	MaxInterval     string `json:"max_interval" yaml:"max_interval"`
    17  	MaxElapsedTime  string `json:"max_elapsed_time" yaml:"max_elapsed_time"`
    18  }
    19  
    20  // Config contains configuration params for a retries mechanism.
    21  type Config struct {
    22  	MaxRetries uint64  `json:"max_retries" yaml:"max_retries"`
    23  	Backoff    Backoff `json:"backoff" yaml:"backoff"`
    24  }
    25  
    26  // NewConfig creates a new Config with default values.
    27  func NewConfig() Config {
    28  	return Config{
    29  		MaxRetries: 0,
    30  		Backoff: Backoff{
    31  			InitialInterval: "500ms",
    32  			MaxInterval:     "3s",
    33  			MaxElapsedTime:  "0s",
    34  		},
    35  	}
    36  }
    37  
    38  //------------------------------------------------------------------------------
    39  
    40  // Get returns a valid *backoff.ExponentialBackoff based on the configuration
    41  // values of Config.
    42  func (c *Config) Get() (backoff.BackOff, error) {
    43  	ctor, err := c.GetCtor()
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	return ctor(), nil
    48  }
    49  
    50  // GetCtor returns a constructor for a backoff.Backoff based on the
    51  // configuration values of Config.
    52  func (c *Config) GetCtor() (func() backoff.BackOff, error) {
    53  	var initInterval, maxInterval, maxElapsed time.Duration
    54  	var err error
    55  	if c.Backoff.InitialInterval != "" {
    56  		if initInterval, err = time.ParseDuration(c.Backoff.InitialInterval); err != nil {
    57  			return nil, fmt.Errorf("invalid backoff initial interval: %v", err)
    58  		}
    59  	}
    60  	if c.Backoff.MaxInterval != "" {
    61  		if maxInterval, err = time.ParseDuration(c.Backoff.MaxInterval); err != nil {
    62  			return nil, fmt.Errorf("invalid backoff max interval: %v", err)
    63  		}
    64  	}
    65  	if c.Backoff.MaxElapsedTime != "" {
    66  		if maxElapsed, err = time.ParseDuration(c.Backoff.MaxElapsedTime); err != nil {
    67  			return nil, fmt.Errorf("invalid backoff max elapsed interval: %v", err)
    68  		}
    69  	}
    70  
    71  	return func() backoff.BackOff {
    72  		boff := backoff.NewExponentialBackOff()
    73  
    74  		boff.InitialInterval = initInterval
    75  		boff.MaxInterval = maxInterval
    76  		boff.MaxElapsedTime = maxElapsed
    77  
    78  		if c.MaxRetries > 0 {
    79  			return backoff.WithMaxRetries(boff, c.MaxRetries)
    80  		}
    81  		return boff
    82  	}, nil
    83  }
    84  
    85  //------------------------------------------------------------------------------