-
Notifications
You must be signed in to change notification settings - Fork 781
/
Copy pathwatcher.go
251 lines (202 loc) · 6.61 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package watch
import (
"log"
"sync"
"time"
dep "github.com/hashicorp/consul-template/dependency"
"github.com/pkg/errors"
)
// dataBufferSize is the default number of views to process in a batch.
const dataBufferSize = 2048
type RetryFunc func(int) (bool, time.Duration)
// Watcher is a top-level manager for views that poll Consul for data.
type Watcher struct {
sync.Mutex
// clients is the collection of API clients to talk to upstreams.
clients *dep.ClientSet
// dataCh is the chan where Views will be published.
dataCh chan *View
// errCh is the chan where any errors will be published.
errCh chan error
// blockQueryWaitTime is amount of time in seconds to do a blocking query for
blockQueryWaitTime time.Duration
// failLookupErrors triggers error when a dependency Fetch fails to
// return data after the first pass.
failLookupErrors bool
// depViewMap is a map of Templates to Views. Templates are keyed by
// their string.
depViewMap map[string]*View
// maxStale specifies the maximum staleness of a query response.
maxStale time.Duration
// once signals if this watcher should tell views to retrieve data exactly
// one time instead of polling infinitely.
once bool
// retryFuncs specifies the different ways to retry based on the upstream.
retryFuncConsul RetryFunc
retryFuncDefault RetryFunc
retryFuncVault RetryFunc
retryFuncNomad RetryFunc
}
type NewWatcherInput struct {
// Clients is the client set to communicate with upstreams.
Clients *dep.ClientSet
// MaxStale is the maximum staleness of a query.
MaxStale time.Duration
// Once specifies this watcher should tell views to poll exactly once.
Once bool
// WaitTime is amount of time in seconds to do a blocking query for
BlockQueryWaitTime time.Duration
// FailLookupErrors triggers error when a dependency Fetch fails to
// return data after the first pass.
FailLookupErrors bool
// RenewVault indicates if this watcher should renew Vault tokens.
RenewVault bool
// VaultToken is the vault token to renew.
VaultToken string
// VaultAgentTokenFile is the path to Vault Agent token file
VaultAgentTokenFile string
// RetryFuncs specify the different ways to retry based on the upstream.
RetryFuncConsul RetryFunc
RetryFuncDefault RetryFunc
RetryFuncVault RetryFunc
RetryFuncNomad RetryFunc
}
// NewWatcher creates a new watcher using the given API client.
func NewWatcher(i *NewWatcherInput) *Watcher {
w := &Watcher{
clients: i.Clients,
depViewMap: make(map[string]*View),
dataCh: make(chan *View, dataBufferSize),
errCh: make(chan error),
maxStale: i.MaxStale,
once: i.Once,
blockQueryWaitTime: i.BlockQueryWaitTime,
failLookupErrors: i.FailLookupErrors,
retryFuncConsul: i.RetryFuncConsul,
retryFuncDefault: i.RetryFuncDefault,
retryFuncVault: i.RetryFuncVault,
retryFuncNomad: i.RetryFuncNomad,
}
return w
}
// DataCh returns a read-only channel of Views which is populated when a view
// receives data from its upstream.
func (w *Watcher) DataCh() <-chan *View {
return w.dataCh
}
// ErrCh returns a read-only channel of errors returned by the upstream.
func (w *Watcher) ErrCh() <-chan error {
if w == nil {
return nil
}
return w.errCh
}
// Add adds the given dependency to the list of monitored dependencies
// and start the associated view. If the dependency already exists, no action is
// taken.
//
// If the Dependency already existed, it this function will return false. If the
// view was successfully created, it will return true. If an error occurs while
// creating the view, it will be returned here (but future errors returned by
// the view will happen on the channel).
func (w *Watcher) Add(d dep.Dependency) (bool, error) {
w.Lock()
defer w.Unlock()
log.Printf("[DEBUG] (watcher) adding %s", d)
if _, ok := w.depViewMap[d.String()]; ok {
log.Printf("[TRACE] (watcher) %s already exists, skipping", d)
return false, nil
}
// Choose the correct retry function based off of the dependency's type.
var retryFunc RetryFunc
switch d.Type() {
case dep.TypeConsul:
retryFunc = w.retryFuncConsul
case dep.TypeVault:
retryFunc = w.retryFuncVault
case dep.TypeNomad:
retryFunc = w.retryFuncNomad
default:
retryFunc = w.retryFuncDefault
}
v, err := NewView(&NewViewInput{
Dependency: d,
Clients: w.clients,
MaxStale: w.maxStale,
BlockQueryWaitTime: w.blockQueryWaitTime,
FailLookupErrors: w.failLookupErrors,
Once: w.once,
RetryFunc: retryFunc,
})
if err != nil {
return false, errors.Wrap(err, "watcher")
}
log.Printf("[TRACE] (watcher) %s starting", d)
w.depViewMap[d.String()] = v
go v.poll(w.dataCh, w.errCh)
return true, nil
}
// Watching determines if the given dependency is being watched.
func (w *Watcher) Watching(d dep.Dependency) bool {
w.Lock()
defer w.Unlock()
_, ok := w.depViewMap[d.String()]
return ok
}
// ForceWatching is used to force setting the internal state of watching
// a dependency. This is only used for unit testing purposes.
func (w *Watcher) ForceWatching(d dep.Dependency, enabled bool) {
w.Lock()
defer w.Unlock()
if enabled {
w.depViewMap[d.String()] = nil
} else {
delete(w.depViewMap, d.String())
}
}
// Remove removes the given dependency from the list and stops the
// associated View. If a View for the given dependency does not exist, this
// function will return false. If the View does exist, this function will return
// true upon successful deletion.
func (w *Watcher) Remove(d dep.Dependency) bool {
w.Lock()
defer w.Unlock()
log.Printf("[DEBUG] (watcher) removing %s", d)
if view, ok := w.depViewMap[d.String()]; ok {
log.Printf("[TRACE] (watcher) actually removing %s", d)
view.stop()
delete(w.depViewMap, d.String())
return true
}
log.Printf("[TRACE] (watcher) %s did not exist, skipping", d)
return false
}
// Size returns the number of views this watcher is watching.
func (w *Watcher) Size() int {
w.Lock()
defer w.Unlock()
return len(w.depViewMap)
}
// Stop halts this watcher and any currently polling views immediately. If a
// view was in the middle of a poll, no data will be returned.
func (w *Watcher) Stop() {
if w == nil {
return
}
w.Lock()
defer w.Unlock()
log.Printf("[DEBUG] (watcher) stopping all views")
for _, view := range w.depViewMap {
if view == nil {
continue
}
log.Printf("[TRACE] (watcher) stopping %s", view.Dependency())
view.stop()
}
// Reset the map to have no views
w.depViewMap = make(map[string]*View)
// Close any idle TCP connections
w.clients.Stop()
}