1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
package backoff
import (
"context"
"time"
)
type ConstantInterval struct {
interval time.Duration
jitter jitter
}
func NewConstantInterval(options ...ConstantOption) *ConstantInterval {
jitterFactor := 0.0
interval := time.Minute
var rng Random
for _, option := range options {
switch option.Ident() {
case identInterval{}:
interval = option.Value().(time.Duration)
case identJitterFactor{}:
jitterFactor = option.Value().(float64)
case identRNG{}:
rng = option.Value().(Random)
}
}
return &ConstantInterval{
interval: interval,
jitter: newJitter(jitterFactor, rng),
}
}
func (g *ConstantInterval) Next() time.Duration {
return time.Duration(g.jitter.apply(float64(g.interval)))
}
type ConstantPolicy struct {
cOptions []ControllerOption
igOptions []ConstantOption
}
func NewConstantPolicy(options ...Option) *ConstantPolicy {
var cOptions []ControllerOption
var igOptions []ConstantOption
for _, option := range options {
switch opt := option.(type) {
case ControllerOption:
cOptions = append(cOptions, opt)
default:
igOptions = append(igOptions, opt.(ConstantOption))
}
}
return &ConstantPolicy{
cOptions: cOptions,
igOptions: igOptions,
}
}
func (p *ConstantPolicy) Start(ctx context.Context) Controller {
ig := NewConstantInterval(p.igOptions...)
return newController(ctx, ig, p.cOptions...)
}
|