-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
background_updater.go
42 lines (37 loc) · 1.05 KB
/
background_updater.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
package ffclient
import (
"math/rand"
"time"
)
// backgroundUpdater contains what is needed to manage the
// background update of the flags.
type backgroundUpdater struct {
ticker *time.Ticker
updaterChan chan struct{}
}
// newBackgroundUpdater init default value for the ticker and the channel.
func newBackgroundUpdater(pollingInterval time.Duration, useJitter bool) backgroundUpdater {
tickerDuration := pollingInterval
if useJitter {
// we accept a deviation of maximum 10% of the polling interval
maxJitter := float64(pollingInterval) * 0.1
jitter := time.Duration(0)
if int64(maxJitter) > 0 {
jitter = time.Duration(rand.Int63n(int64(maxJitter))) // nolint: gosec
}
if jitter%2 == 0 {
tickerDuration = pollingInterval + jitter
} else {
tickerDuration = pollingInterval - jitter
}
}
return backgroundUpdater{
ticker: time.NewTicker(tickerDuration),
updaterChan: make(chan struct{}),
}
}
// close stops the ticker and closes the channel.
func (bgu *backgroundUpdater) close() {
bgu.ticker.Stop()
close(bgu.updaterChan)
}