@@ -63,8 +63,9 @@ type Watcher struct {
63
63
certCAPool * x509.CertPool
64
64
leaf * certLeaf
65
65
66
- update chan struct {}
67
- log Logger
66
+ update chan struct {}
67
+ shutdownCh chan struct {}
68
+ log Logger
68
69
}
69
70
70
71
// New builds a new watcher
@@ -73,10 +74,11 @@ func New(service string, consul *api.Client, log Logger) *Watcher {
73
74
service : service ,
74
75
consul : consul ,
75
76
76
- C : make (chan Config ),
77
- upstreams : make (map [string ]* upstream ),
78
- update : make (chan struct {}, 1 ),
79
- log : log ,
77
+ C : make (chan Config ),
78
+ upstreams : make (map [string ]* upstream ),
79
+ update : make (chan struct {}, 1 ),
80
+ shutdownCh : make (chan struct {}),
81
+ log : log ,
80
82
}
81
83
}
82
84
@@ -189,9 +191,11 @@ func (w *Watcher) startUpstreamService(up api.Upstream, name string) {
189
191
go func () {
190
192
index := uint64 (0 )
191
193
for {
194
+ w .lock .Lock ()
192
195
if u .done {
193
196
return
194
197
}
198
+ w .lock .Unlock ()
195
199
nodes , meta , err := w .consul .Health ().Connect (up .DestinationName , "" , true , & api.QueryOptions {
196
200
Datacenter : up .Datacenter ,
197
201
WaitTime : 10 * time .Minute ,
@@ -331,6 +335,9 @@ func (w *Watcher) watchLeaf() {
331
335
w .ready .Done ()
332
336
first = false
333
337
}
338
+ if w .isStopped () {
339
+ return
340
+ }
334
341
}
335
342
}
336
343
@@ -361,6 +368,9 @@ func (w *Watcher) watchService(service string, handler func(first bool, srv *api
361
368
}
362
369
363
370
first = false
371
+ if w .isStopped () {
372
+ return
373
+ }
364
374
}
365
375
}
366
376
@@ -405,6 +415,9 @@ func (w *Watcher) watchCA() {
405
415
w .ready .Done ()
406
416
first = false
407
417
}
418
+ if w .isStopped () {
419
+ return
420
+ }
408
421
}
409
422
}
410
423
@@ -492,3 +505,16 @@ func (w *Watcher) notifyChanged() {
492
505
default :
493
506
}
494
507
}
508
+
509
+ func (w * Watcher ) Stop () {
510
+ close (w .shutdownCh )
511
+ }
512
+
513
+ func (w * Watcher ) isStopped () bool {
514
+ select {
515
+ case <- w .shutdownCh :
516
+ return true
517
+ default :
518
+ return false
519
+ }
520
+ }
0 commit comments