/
auto_polling_policy.go
121 lines (108 loc) · 2.72 KB
/
auto_polling_policy.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package configcat
import (
"context"
"sync"
"time"
)
// autoPollConfig describes the configuration for auto polling.
type autoPollConfig struct {
interval time.Duration
changeNotify func()
}
func (config autoPollConfig) getModeIdentifier() string {
return "a"
}
func (config autoPollConfig) refreshPolicy(rconfig refreshPolicyConfig) refreshPolicy {
return newAutoPollingPolicy(config, rconfig)
}
// AutoPoll creates an auto polling refresh mode that polls for changes
// at the given interval.
func AutoPoll(interval time.Duration) RefreshMode {
return autoPollConfig{
interval: interval,
}
}
// AutoPollWithChangeListener creates an auto polling refresh mode. It polls for changes
// at the given interval, and calls changeNotify whenever the configuration has changed.
//
// If changeNotify is nil, this is equvalent to AutoPoll.
func AutoPollWithChangeListener(
interval time.Duration,
changeNotify func(),
) RefreshMode {
return autoPollConfig{
interval: interval,
changeNotify: changeNotify,
}
}
// autoPollingPolicy implements the AutoPoll policy.
type autoPollingPolicy struct {
fetcher *configFetcher
changeNotify func()
logger *leveledLogger
// mu guards the closing of the closed channel.
mu sync.Mutex
closed chan struct{}
}
// newAutoPollingPolicy initializes a new autoPollingPolicy.
func newAutoPollingPolicy(
config autoPollConfig,
rconfig refreshPolicyConfig,
) *autoPollingPolicy {
policy := &autoPollingPolicy{
fetcher: rconfig.fetcher,
closed: make(chan struct{}),
changeNotify: config.changeNotify,
logger: rconfig.logger,
}
policy.fetcher.startRefresh()
go policy.poller(config.interval)
return policy
}
func (p *autoPollingPolicy) refresh(ctx context.Context) {
p.fetcher.refresh(ctx)
}
func (p *autoPollingPolicy) get(ctx context.Context) *config {
return p.fetcher.config(ctx)
}
func (policy *autoPollingPolicy) poller(interval time.Duration) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-policy.closed
cancel()
}()
config := policy.fetcher.config(ctx)
policy.notify()
for {
select {
case <-policy.closed:
return
case <-ticker.C:
policy.refresh(ctx)
newConfig := policy.fetcher.config(ctx)
if newConfig.body() != config.body() {
config = newConfig
policy.notify()
}
}
}
}
// notify calls the changeNotify function if necessary.
func (policy *autoPollingPolicy) notify() {
if policy.changeNotify != nil {
go policy.changeNotify()
}
}
func (policy *autoPollingPolicy) close() {
// Guard against double-close.
policy.mu.Lock()
defer policy.mu.Unlock()
select {
case <-policy.closed:
return
default:
close(policy.closed)
}
}