@@ -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 ,
@@ -255,6 +259,9 @@ func (w *Watcher) watchLeaf() {
255
259
w .ready .Done ()
256
260
first = false
257
261
}
262
+ if w .isStopped () {
263
+ return
264
+ }
258
265
}
259
266
}
260
267
@@ -285,6 +292,9 @@ func (w *Watcher) watchService(service string, handler func(first bool, srv *api
285
292
}
286
293
287
294
first = false
295
+ if w .isStopped () {
296
+ return
297
+ }
288
298
}
289
299
}
290
300
@@ -329,6 +339,9 @@ func (w *Watcher) watchCA() {
329
339
w .ready .Done ()
330
340
first = false
331
341
}
342
+ if w .isStopped () {
343
+ return
344
+ }
332
345
}
333
346
}
334
347
@@ -416,3 +429,16 @@ func (w *Watcher) notifyChanged() {
416
429
default :
417
430
}
418
431
}
432
+
433
+ func (w * Watcher ) Stop () {
434
+ close (w .shutdownCh )
435
+ }
436
+
437
+ func (w * Watcher ) isStopped () bool {
438
+ select {
439
+ case <- w .shutdownCh :
440
+ return true
441
+ default :
442
+ return false
443
+ }
444
+ }
0 commit comments