-
Notifications
You must be signed in to change notification settings - Fork 0
/
pusher.go
50 lines (42 loc) · 1.11 KB
/
pusher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package scrimplb
import (
"log"
"math/rand"
"time"
)
// PushTask runs a Pusher on a regular, config-defined basis
type PushTask struct {
config *ScrimpConfig
sleepTime time.Duration
maxJitter time.Duration
failureCount int
}
// NewPushTask creates a new PushTask with the given config
func NewPushTask(config *ScrimpConfig) *PushTask {
return &PushTask{
config,
config.LoadBalancerConfig.PushPeriod,
config.LoadBalancerConfig.PushJitter,
0,
}
}
// Loop should be called in/as a goroutine and will regularly push state
func (p *PushTask) Loop() {
for {
if p.failureCount > 0 {
backoffSleep := time.Second * 5 * time.Duration(p.failureCount)
log.Printf("sleeping for %v extra due to previous failure\n", backoffSleep)
time.Sleep(backoffSleep)
}
time.Sleep(p.sleepTime)
randSleep := time.Duration(rand.Int63n(p.maxJitter.Nanoseconds())).Round(time.Millisecond)
time.Sleep(randSleep)
err := p.config.Provider.PushSeed(p.config.Resolver, p.config.PortRaw)
if err != nil {
log.Printf("failed to push seed: %v\n", err)
p.failureCount++
} else {
p.failureCount = 0
}
}
}