github.com/franciscocpg/up@v0.1.10/config/relay.go (about)

     1  package config
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/jpillora/backoff"
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  // Relay config.
    11  type Relay struct {
    12  	// Command run to start your server.
    13  	Command string `json:"command"`
    14  
    15  	// Backoff configuration.
    16  	Backoff Backoff `json:"backoff"`
    17  }
    18  
    19  // Default implementation.
    20  func (r *Relay) Default() error {
    21  	if r.Command == "" {
    22  		r.Command = "./server"
    23  	}
    24  
    25  	if err := r.Backoff.Default(); err != nil {
    26  		return errors.Wrap(err, ".backoff")
    27  	}
    28  
    29  	return nil
    30  }
    31  
    32  // Backoff config.
    33  type Backoff struct {
    34  	// Min time in milliseconds.
    35  	Min int `json:"min"`
    36  
    37  	// Max time in milliseconds.
    38  	Max int `json:"max"`
    39  
    40  	// Factor applied for every attempt.
    41  	Factor float64 `json:"factor"`
    42  
    43  	// Attempts performed before failing.
    44  	Attempts int `json:"attempts"`
    45  
    46  	// Jitter is applied when true.
    47  	Jitter bool `json:"jitter"`
    48  }
    49  
    50  // Default implementation.
    51  func (b *Backoff) Default() error {
    52  	if b.Min == 0 {
    53  		b.Min = 100
    54  	}
    55  
    56  	if b.Max == 0 {
    57  		b.Max = 500
    58  	}
    59  
    60  	if b.Factor == 0 {
    61  		b.Factor = 2
    62  	}
    63  
    64  	if b.Attempts == 0 {
    65  		b.Attempts = 3
    66  	}
    67  
    68  	return nil
    69  }
    70  
    71  // Backoff returns the backoff from config.
    72  func (b *Backoff) Backoff() *backoff.Backoff {
    73  	return &backoff.Backoff{
    74  		Min:    time.Duration(b.Min) * time.Millisecond,
    75  		Max:    time.Duration(b.Max) * time.Millisecond,
    76  		Factor: b.Factor,
    77  		Jitter: b.Jitter,
    78  	}
    79  }