...

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

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

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

View as plain text