...

Source file src/github.com/lestrrat-go/backoff/v2/options.go

Documentation: github.com/lestrrat-go/backoff/v2

     1  package backoff
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/lestrrat-go/option"
     7  )
     8  
     9  type identInterval struct{}
    10  type identJitterFactor struct{}
    11  type identMaxInterval struct{}
    12  type identMaxRetries struct{}
    13  type identMinInterval struct{}
    14  type identMultiplier struct{}
    15  type identRNG struct{}
    16  
    17  // ControllerOption is an option that may be passed to Policy objects,
    18  // but are ultimately passed down to the Controller objects.
    19  // (Normally you do not have to care about the distinction)
    20  type ControllerOption interface {
    21  	ConstantOption
    22  	ExponentialOption
    23  	CommonOption
    24  	controllerOption()
    25  }
    26  
    27  type controllerOption struct {
    28  	Option
    29  }
    30  
    31  func (*controllerOption) exponentialOption() {}
    32  func (*controllerOption) controllerOption()  {}
    33  func (*controllerOption) constantOption()    {}
    34  
    35  // ConstantOption is an option that is used by the Constant policy.
    36  type ConstantOption interface {
    37  	Option
    38  	constantOption()
    39  }
    40  
    41  type constantOption struct {
    42  	Option
    43  }
    44  
    45  func (*constantOption) constantOption() {}
    46  
    47  // ExponentialOption is an option that is used by the Exponential policy.
    48  type ExponentialOption interface {
    49  	Option
    50  	exponentialOption()
    51  }
    52  
    53  type exponentialOption struct {
    54  	Option
    55  }
    56  
    57  func (*exponentialOption) exponentialOption() {}
    58  
    59  // CommonOption is an option that can be passed to any of the backoff policies.
    60  type CommonOption interface {
    61  	ExponentialOption
    62  	ConstantOption
    63  }
    64  
    65  type commonOption struct {
    66  	Option
    67  }
    68  
    69  func (*commonOption) constantOption()    {}
    70  func (*commonOption) exponentialOption() {}
    71  
    72  // WithMaxRetries specifies the maximum number of attempts that can be made
    73  // by the backoff policies. By default each policy tries up to 10 times.
    74  //
    75  // If you would like to retry forever, specify "0" and pass to the constructor
    76  // of each policy.
    77  //
    78  // This option can be passed to all policy constructors except for NullPolicy
    79  func WithMaxRetries(v int) ControllerOption {
    80  	return &controllerOption{option.New(identMaxRetries{}, v)}
    81  }
    82  
    83  // WithInterval specifies the constant interval used in ConstantPolicy and
    84  // ConstantInterval.
    85  // The default value is 1 minute.
    86  func WithInterval(v time.Duration) ConstantOption {
    87  	return &constantOption{option.New(identInterval{}, v)}
    88  }
    89  
    90  // WithMaxInterval specifies the maximum duration used in exponential backoff
    91  // The default value is 1 minute.
    92  func WithMaxInterval(v time.Duration) ExponentialOption {
    93  	return &exponentialOption{option.New(identMaxInterval{}, v)}
    94  }
    95  
    96  // WithMinInterval specifies the minimum duration used in exponential backoff.
    97  // The default value is 500ms.
    98  func WithMinInterval(v time.Duration) ExponentialOption {
    99  	return &exponentialOption{option.New(identMinInterval{}, v)}
   100  }
   101  
   102  // WithMultiplier specifies the factor in which the backoff intervals are
   103  // increased. By default this value is set to 1.5, which means that for
   104  // every iteration a 50% increase in the interval for every iteration
   105  // (up to the value specified by WithMaxInterval). this value must be greater
   106  // than 1.0. If the value is less than equal to 1.0, the default value
   107  // of 1.5 is used.
   108  func WithMultiplier(v float64) ExponentialOption {
   109  	return &exponentialOption{option.New(identMultiplier{}, v)}
   110  }
   111  
   112  // WithJitterFactor enables some randomness (jittering) in the computation of
   113  // the backoff intervals. This value must be between 0.0 < v < 1.0. If a
   114  // value outside of this range is specified, the value will be silently
   115  // ignored and jittering is disabled.
   116  //
   117  // This option can be passed to ExponentialPolicy or ConstantPolicy constructor
   118  func WithJitterFactor(v float64) CommonOption {
   119  	return &commonOption{option.New(identJitterFactor{}, v)}
   120  }
   121  
   122  // WithRNG specifies the random number generator used for jittering.
   123  // If not provided one will be created, but if you want a truly random
   124  // jittering, make sure to provide one that you explicitly initialized
   125  func WithRNG(v Random) CommonOption {
   126  	return &commonOption{option.New(identRNG{}, v)}
   127  }
   128  

View as plain text