Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MEDIUM: watcher: fix race condition & plumbing stop for test #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions consul/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ type Watcher struct {
certCAPool *x509.CertPool
leaf *certLeaf

update chan struct{}
log Logger
update chan struct{}
shutdownCh chan struct{}
log Logger
}

// New builds a new watcher
Expand All @@ -73,10 +74,11 @@ func New(service string, consul *api.Client, log Logger) *Watcher {
service: service,
consul: consul,

C: make(chan Config),
upstreams: make(map[string]*upstream),
update: make(chan struct{}, 1),
log: log,
C: make(chan Config),
upstreams: make(map[string]*upstream),
update: make(chan struct{}, 1),
shutdownCh: make(chan struct{}),
log: log,
}
}

Expand Down Expand Up @@ -189,9 +191,11 @@ func (w *Watcher) startUpstreamService(up api.Upstream, name string) {
go func() {
index := uint64(0)
for {
w.lock.Lock()
if u.done {
return
}
w.lock.Unlock()
nodes, meta, err := w.consul.Health().Connect(up.DestinationName, "", true, &api.QueryOptions{
Datacenter: up.Datacenter,
WaitTime: 10 * time.Minute,
Expand Down Expand Up @@ -331,6 +335,9 @@ func (w *Watcher) watchLeaf() {
w.ready.Done()
first = false
}
if w.isStopped() {
return
}
}
}

Expand Down Expand Up @@ -361,6 +368,9 @@ func (w *Watcher) watchService(service string, handler func(first bool, srv *api
}

first = false
if w.isStopped() {
return
}
}
}

Expand Down Expand Up @@ -405,6 +415,9 @@ func (w *Watcher) watchCA() {
w.ready.Done()
first = false
}
if w.isStopped() {
return
}
}
}

Expand Down Expand Up @@ -492,3 +505,16 @@ func (w *Watcher) notifyChanged() {
default:
}
}

func (w *Watcher) Stop() {
close(w.shutdownCh)
}

func (w *Watcher) isStopped() bool {
select {
case <-w.shutdownCh:
return true
default:
return false
}
}
1 change: 1 addition & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func startConnectService(t *testing.T, sd *lib.Shutdown, client *api.Client, reg
errs <- err
}
}()
watcher.Stop()

sourceHap := haproxy.New(client, watcher.C, haproxy.Options{
EnableIntentions: true,
Expand Down