From 6719f8d655a1e4c0fafdbbcb29572c50c9efc909 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Fri, 2 Aug 2019 14:59:08 +0100 Subject: [PATCH] Remove the table watcher when stopped --- network/router/table.go | 12 +++++++++++- network/router/watcher.go | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/network/router/table.go b/network/router/table.go index 905944b7..6b34bb00 100644 --- a/network/router/table.go +++ b/network/router/table.go @@ -160,13 +160,23 @@ func (t *table) Watch(opts ...WatchOption) (Watcher, error) { } w := &tableWatcher{ + id: uuid.New().String(), opts: wopts, resChan: make(chan *Event, 10), done: make(chan struct{}), } + // when the watcher is stopped delete it + go func() { + <-w.done + t.Lock() + delete(t.watchers, w.id) + t.Unlock() + }() + + // save the watcher t.Lock() - t.watchers[uuid.New().String()] = w + t.watchers[w.id] = w t.Unlock() return w, nil diff --git a/network/router/watcher.go b/network/router/watcher.go index 1530e72c..f2c8beac 100644 --- a/network/router/watcher.go +++ b/network/router/watcher.go @@ -72,6 +72,7 @@ func WatchService(s string) WatchOption { type tableWatcher struct { sync.RWMutex + id string opts WatchOptions resChan chan *Event done chan struct{}