diff --git a/cluster/bootstrap.go b/cluster/bootstrap.go index c98a32b3..50259807 100644 --- a/cluster/bootstrap.go +++ b/cluster/bootstrap.go @@ -112,7 +112,7 @@ func (b *Bootstrapper) SetBasicAuth(username, password string) { func (b *Bootstrapper) Boot(id, raftAddr string, done func() bool, timeout time.Duration) error { timeoutT := time.NewTimer(timeout) defer timeoutT.Stop() - tickerT := time.NewTimer(jitter(time.Millisecond)) + tickerT := time.NewTimer(random.Jitter(time.Millisecond)) defer tickerT.Stop() for { @@ -127,7 +127,7 @@ func (b *Bootstrapper) Boot(id, raftAddr string, done func() bool, timeout time. b.setBootStatus(BootDone) return nil } - tickerT.Reset(jitter(b.Interval)) // Move to longer-period polling + tickerT.Reset(random.Jitter(b.Interval)) // Move to longer-period polling targets, err := b.provider.Lookup() if err != nil { @@ -251,10 +251,3 @@ func (s *stringAddressProvider) Lookup() ([]string, error) { func NewAddressProviderString(ss []string) AddressProvider { return &stringAddressProvider{ss} } - -// jitter adds a little bit of randomness to a given duration. This is -// useful to prevent nodes across the cluster performing certain operations -// all at the same time. -func jitter(duration time.Duration) time.Duration { - return duration + time.Duration(random.Float64()*float64(duration)) -} diff --git a/disco/service.go b/disco/service.go index e58a0b92..510b2a7b 100644 --- a/disco/service.go +++ b/disco/service.go @@ -76,7 +76,7 @@ func (s *Service) Register(id, apiAddr, addr string) (bool, string, error) { return true, apiAddr, nil } - time.Sleep(jitter(s.RegisterInterval)) + time.Sleep(random.Jitter(s.RegisterInterval)) } } @@ -138,7 +138,3 @@ func (s *Service) updateContact(t time.Time) { defer s.mu.Unlock() s.lastContact = t } - -func jitter(duration time.Duration) time.Duration { - return duration + time.Duration(random.Float64()*float64(duration)) -} diff --git a/random/random.go b/random/random.go index 54d58033..856db65f 100644 --- a/random/random.go +++ b/random/random.go @@ -41,3 +41,10 @@ func Intn(n int) int { defer mu.Unlock() return r.Intn(n) } + +// Jitter adds a little bit of randomness to a given duration. This is +// useful to prevent nodes across the cluster performing certain operations +// all at the same time. +func Jitter(d time.Duration) time.Duration { + return d + time.Duration(Float64()*float64(d)) +}