github.com/webonyx/up@v0.7.4-0.20180808230834-91b94e551323/config/backoff.go (about)

     1  package config
     2  
     3  import (
     4  	"time"
     5  	
     6  	"github.com/tj/backoff"
     7  )
     8  
     9  // Backoff config.
    10  type Backoff struct {
    11  	// Min time in milliseconds.
    12  	Min int `json:"min"`
    13  
    14  	// Max time in milliseconds.
    15  	Max int `json:"max"`
    16  
    17  	// Factor applied for every attempt.
    18  	Factor float64 `json:"factor"`
    19  
    20  	// Attempts performed before failing.
    21  	Attempts int `json:"attempts"`
    22  
    23  	// Jitter is applied when true.
    24  	Jitter bool `json:"jitter"`
    25  }
    26  
    27  // Default implementation.
    28  func (b *Backoff) Default() error {
    29  	if b.Min == 0 {
    30  		b.Min = 100
    31  	}
    32  
    33  	if b.Max == 0 {
    34  		b.Max = 500
    35  	}
    36  
    37  	if b.Factor == 0 {
    38  		b.Factor = 2
    39  	}
    40  
    41  	if b.Attempts == 0 {
    42  		b.Attempts = 3
    43  	}
    44  
    45  	return nil
    46  }
    47  
    48  // Backoff returns the backoff from config.
    49  func (b *Backoff) Backoff() *backoff.Backoff {
    50  	return &backoff.Backoff{
    51  		Min:    time.Duration(b.Min) * time.Millisecond,
    52  		Max:    time.Duration(b.Max) * time.Millisecond,
    53  		Factor: b.Factor,
    54  		Jitter: b.Jitter,
    55  	}
    56  }