-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.go
168 lines (145 loc) · 3.65 KB
/
watcher.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package watcher
import (
"context"
"time"
"github.com/cenkalti/backoff/v4"
consul "github.com/hashicorp/consul/api"
)
// DefaultWaitTime is the maximum wait time allowed by Consul
const DefaultWaitTime = 10 * time.Minute
// Watcher is a wrapper around the Consul client that watches for changes to a keys and directories
type Watcher struct {
consul *consul.Client
backoff *backoff.ExponentialBackOff
debounceTime time.Duration
}
// New returns a new Watcher
func New(consulClient *consul.Client, retryTime time.Duration, debounceTime time.Duration) *Watcher {
bf := backoff.NewExponentialBackOff()
bf.InitialInterval = retryTime
return &Watcher{
consul: consulClient,
backoff: bf,
debounceTime: debounceTime,
}
}
// WatchTree watches for changes to a directory and emit key value pairs
func (w *Watcher) WatchTree(ctx context.Context, path string) (<-chan consul.KVPairs, error) {
out := make(chan consul.KVPairs)
kv := w.consul.KV()
var debounceTimer *time.Timer
var debounceStart time.Time
opts := &consul.QueryOptions{
AllowStale: true,
RequireConsistent: false,
UseCache: true,
WaitTime: DefaultWaitTime,
}
go func() {
defer close(out)
for {
select {
case <-ctx.Done():
return
default:
}
kvPairs, meta, err := kv.List(path, opts.WithContext(ctx))
if err != nil {
if consul.IsRetryableError(err) {
opts.WaitIndex = 0
select {
case <-ctx.Done():
return
case <-time.After(w.backoff.NextBackOff()):
continue
}
}
return
}
w.backoff.Reset()
if opts.WaitIndex != meta.LastIndex {
if debounceTimer != nil {
debounceTimer.Stop()
}
if opts.WaitIndex <= 0 ||
(!debounceStart.IsZero() && time.Since(debounceStart) > 2*w.debounceTime) {
out <- kvPairs
debounceStart = time.Time{}
} else {
if debounceStart.IsZero() {
debounceStart = time.Now()
}
debounceTimer = time.AfterFunc(w.debounceTime, func() {
select {
case out <- kvPairs:
case <-ctx.Done():
return
}
debounceStart = time.Time{}
})
}
opts.WaitIndex = meta.LastIndex
}
}
}()
return out, nil
}
// WatchKey watches for changes to a key and emits a key value pair
func (w *Watcher) WatchKey(ctx context.Context, key string) (<-chan *consul.KVPair, error) {
out := make(chan *consul.KVPair)
kv := w.consul.KV()
var debounceStart time.Time
var debounceTimer *time.Timer
opts := &consul.QueryOptions{
AllowStale: true,
RequireConsistent: false,
UseCache: true,
WaitTime: DefaultWaitTime,
}
go func() {
defer close(out)
for {
select {
case <-ctx.Done():
return
default:
}
kvPair, meta, err := kv.Get(key, opts.WithContext(ctx))
if err != nil {
if consul.IsRetryableError(err) {
opts.WaitIndex = 0
select {
case <-ctx.Done():
return
case <-time.After(w.backoff.NextBackOff()):
continue
}
}
return
}
// reset backoff after successful load
w.backoff.Reset()
if opts.WaitIndex != meta.LastIndex {
if debounceTimer != nil {
debounceTimer.Stop()
}
// don't debounce and wait if we start fresh without wait index
if opts.WaitIndex <= 0 ||
(!debounceStart.IsZero() && time.Since(debounceStart) > 2*w.debounceTime) {
out <- kvPair
debounceStart = time.Time{}
} else {
if debounceStart.IsZero() {
debounceStart = time.Now()
}
debounceTimer = time.AfterFunc(w.debounceTime, func() {
out <- kvPair
debounceStart = time.Time{}
})
}
opts.WaitIndex = meta.LastIndex
}
}
}()
return out, nil
}