options.go 3.8 KB

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