@@ -60,8 +60,9 @@ type Watcher struct {
60
60
certCAPool * x509.CertPool
61
61
leaf * certLeaf
62
62
63
- update chan struct {}
64
- log Logger
63
+ update chan struct {}
64
+ shutdownCh chan struct {}
65
+ log Logger
65
66
}
66
67
67
68
// New builds a new watcher
@@ -70,10 +71,11 @@ func New(service string, consul *api.Client, log Logger) *Watcher {
70
71
service : service ,
71
72
consul : consul ,
72
73
73
- C : make (chan Config ),
74
- upstreams : make (map [string ]* upstream ),
75
- update : make (chan struct {}, 1 ),
76
- log : log ,
74
+ C : make (chan Config ),
75
+ upstreams : make (map [string ]* upstream ),
76
+ update : make (chan struct {}, 1 ),
77
+ shutdownCh : make (chan struct {}),
78
+ log : log ,
77
79
}
78
80
}
79
81
@@ -182,9 +184,11 @@ func (w *Watcher) startUpstream(up api.Upstream) {
182
184
go func () {
183
185
index := uint64 (0 )
184
186
for {
187
+ w .lock .Lock ()
185
188
if u .done {
186
189
return
187
190
}
191
+ w .lock .Unlock ()
188
192
nodes , meta , err := w .consul .Health ().Connect (up .DestinationName , "" , true , & api.QueryOptions {
189
193
Datacenter : up .Datacenter ,
190
194
WaitTime : 10 * time .Minute ,
@@ -224,6 +228,7 @@ func (w *Watcher) watchLeaf() {
224
228
var lastIndex uint64
225
229
first := true
226
230
for {
231
+ w .notifyShutdownCh ()
227
232
cert , meta , err := w .consul .Agent ().ConnectCALeaf (w .serviceName , & api.QueryOptions {
228
233
WaitTime : 10 * time .Minute ,
229
234
WaitIndex : lastIndex ,
@@ -264,6 +269,7 @@ func (w *Watcher) watchService(service string, handler func(first bool, srv *api
264
269
hash := ""
265
270
first := true
266
271
for {
272
+ w .notifyShutdownCh ()
267
273
srv , meta , err := w .consul .Agent ().Service (service , & api.QueryOptions {
268
274
WaitHash : hash ,
269
275
WaitTime : 10 * time .Minute ,
@@ -294,6 +300,7 @@ func (w *Watcher) watchCA() {
294
300
first := true
295
301
var lastIndex uint64
296
302
for {
303
+ w .notifyShutdownCh ()
297
304
caList , meta , err := w .consul .Agent ().ConnectCARoots (& api.QueryOptions {
298
305
WaitIndex : lastIndex ,
299
306
WaitTime : 10 * time .Minute ,
@@ -416,3 +423,15 @@ func (w *Watcher) notifyChanged() {
416
423
default :
417
424
}
418
425
}
426
+
427
+ func (w * Watcher ) Stop () {
428
+ close (w .shutdownCh )
429
+ }
430
+
431
+ func (w * Watcher ) notifyShutdownCh () {
432
+ select {
433
+ case <- w .shutdownCh :
434
+ return
435
+ default :
436
+ }
437
+ }
0 commit comments