1
0
Fork 0

Implement Jitter()

master
Philip O'Toole 1 year ago
parent 9c5d9de501
commit 170772587e

@ -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 { func (b *Bootstrapper) Boot(id, raftAddr string, done func() bool, timeout time.Duration) error {
timeoutT := time.NewTimer(timeout) timeoutT := time.NewTimer(timeout)
defer timeoutT.Stop() defer timeoutT.Stop()
tickerT := time.NewTimer(jitter(time.Millisecond)) tickerT := time.NewTimer(random.Jitter(time.Millisecond))
defer tickerT.Stop() defer tickerT.Stop()
for { for {
@ -127,7 +127,7 @@ func (b *Bootstrapper) Boot(id, raftAddr string, done func() bool, timeout time.
b.setBootStatus(BootDone) b.setBootStatus(BootDone)
return nil 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() targets, err := b.provider.Lookup()
if err != nil { if err != nil {
@ -251,10 +251,3 @@ func (s *stringAddressProvider) Lookup() ([]string, error) {
func NewAddressProviderString(ss []string) AddressProvider { func NewAddressProviderString(ss []string) AddressProvider {
return &stringAddressProvider{ss} 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))
}

@ -76,7 +76,7 @@ func (s *Service) Register(id, apiAddr, addr string) (bool, string, error) {
return true, apiAddr, nil 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() defer s.mu.Unlock()
s.lastContact = t s.lastContact = t
} }
func jitter(duration time.Duration) time.Duration {
return duration + time.Duration(random.Float64()*float64(duration))
}

@ -41,3 +41,10 @@ func Intn(n int) int {
defer mu.Unlock() defer mu.Unlock()
return r.Intn(n) 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))
}

Loading…
Cancel
Save