@@ -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,8 @@ func (w *Watcher) watchLeaf() {
255
259
w .ready .Done ()
256
260
first = false
257
261
}
262
+
263
+ w .notifyShutdownCh ("done watching leaf cert for" , w .serviceName )
258
264
}
259
265
}
260
266
@@ -285,6 +291,8 @@ func (w *Watcher) watchService(service string, handler func(first bool, srv *api
285
291
}
286
292
287
293
first = false
294
+
295
+ w .notifyShutdownCh ("done watching service" , service )
288
296
}
289
297
}
290
298
@@ -329,6 +337,8 @@ func (w *Watcher) watchCA() {
329
337
w .ready .Done ()
330
338
first = false
331
339
}
340
+
341
+ w .notifyShutdownCh ("done watching ca certs" , caList .ActiveRootID )
332
342
}
333
343
}
334
344
@@ -416,3 +426,18 @@ func (w *Watcher) notifyChanged() {
416
426
default :
417
427
}
418
428
}
429
+
430
+ func (w * Watcher ) Stop () {
431
+ close (w .shutdownCh )
432
+ }
433
+
434
+ func (w * Watcher ) notifyShutdownCh (message , watcher string ) {
435
+ for {
436
+ select {
437
+ case <- w .shutdownCh :
438
+ w .log .Infof ("%s %s" , message , watcher )
439
+ return
440
+ default :
441
+ }
442
+ }
443
+ }
0 commit comments