| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package configretry |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/cenkalti/backoff/v5" |
| 11 |
) |
| 12 |
|
| 13 |
|
| 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 |
|
| 26 |
|
| 27 |
type BackOffConfig struct { |
| 28 |
|
| 29 |
Enabled bool `mapstructure:"enabled"` |
| 30 |
|
| 31 |
InitialInterval time.Duration `mapstructure:"initial_interval"` |
| 32 |
|
| 33 |
) |
| 34 |
RandomizationFactor float64 `mapstructure:"randomization_factor"` |
| 35 |
|
| 36 |
Multiplier float64 `mapstructure:"multiplier"` |
| 37 |
|
| 38 |
|
| 39 |
MaxInterval time.Duration `mapstructure:"max_interval"` |
| 40 |
|
| 41 |
|
| 42 |
MaxElapsedTime time.Duration `mapstructure:"max_elapsed_time"` |
| 43 |
|
| 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 |
} |