Skip to content

Commit 1093717

Browse files
gitforbitamelhusic
authored andcommitted
MEDIUM: watcher: fix race condition & plumbing stop for test
1 parent f8963ae commit 1093717

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

consul/watcher.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ type Watcher struct {
6060
certCAPool *x509.CertPool
6161
leaf *certLeaf
6262

63-
update chan struct{}
64-
log Logger
63+
update chan struct{}
64+
shutdownCh chan struct{}
65+
log Logger
6566
}
6667

6768
// New builds a new watcher
@@ -70,10 +71,11 @@ func New(service string, consul *api.Client, log Logger) *Watcher {
7071
service: service,
7172
consul: consul,
7273

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,
7779
}
7880
}
7981

@@ -182,9 +184,11 @@ func (w *Watcher) startUpstream(up api.Upstream) {
182184
go func() {
183185
index := uint64(0)
184186
for {
187+
w.lock.Lock()
185188
if u.done {
186189
return
187190
}
191+
w.lock.Unlock()
188192
nodes, meta, err := w.consul.Health().Connect(up.DestinationName, "", true, &api.QueryOptions{
189193
Datacenter: up.Datacenter,
190194
WaitTime: 10 * time.Minute,
@@ -224,6 +228,7 @@ func (w *Watcher) watchLeaf() {
224228
var lastIndex uint64
225229
first := true
226230
for {
231+
w.notifyShutdownCh()
227232
cert, meta, err := w.consul.Agent().ConnectCALeaf(w.serviceName, &api.QueryOptions{
228233
WaitTime: 10 * time.Minute,
229234
WaitIndex: lastIndex,
@@ -264,6 +269,7 @@ func (w *Watcher) watchService(service string, handler func(first bool, srv *api
264269
hash := ""
265270
first := true
266271
for {
272+
w.notifyShutdownCh()
267273
srv, meta, err := w.consul.Agent().Service(service, &api.QueryOptions{
268274
WaitHash: hash,
269275
WaitTime: 10 * time.Minute,
@@ -294,6 +300,7 @@ func (w *Watcher) watchCA() {
294300
first := true
295301
var lastIndex uint64
296302
for {
303+
w.notifyShutdownCh()
297304
caList, meta, err := w.consul.Agent().ConnectCARoots(&api.QueryOptions{
298305
WaitIndex: lastIndex,
299306
WaitTime: 10 * time.Minute,
@@ -416,3 +423,15 @@ func (w *Watcher) notifyChanged() {
416423
default:
417424
}
418425
}
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+
}

utils_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func startConnectService(t *testing.T, sd *lib.Shutdown, client *api.Client, reg
7070
errs <- err
7171
}
7272
}()
73+
watcher.Stop()
7374

7475
sourceHap := haproxy.New(client, watcher.C, haproxy.Options{
7576
EnableIntentions: true,

0 commit comments

Comments
 (0)