Remove the table watcher when stopped

This commit is contained in:
Asim Aslam 2019-08-02 14:59:08 +01:00
parent d7929ef8f3
commit 6719f8d655
2 changed files with 12 additions and 1 deletions

View File

@ -160,13 +160,23 @@ func (t *table) Watch(opts ...WatchOption) (Watcher, error) {
} }
w := &tableWatcher{ w := &tableWatcher{
id: uuid.New().String(),
opts: wopts, opts: wopts,
resChan: make(chan *Event, 10), resChan: make(chan *Event, 10),
done: make(chan struct{}), 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.Lock()
t.watchers[uuid.New().String()] = w t.watchers[w.id] = w
t.Unlock() t.Unlock()
return w, nil return w, nil

View File

@ -72,6 +72,7 @@ func WatchService(s string) WatchOption {
type tableWatcher struct { type tableWatcher struct {
sync.RWMutex sync.RWMutex
id string
opts WatchOptions opts WatchOptions
resChan chan *Event resChan chan *Event
done chan struct{} done chan struct{}