protect etcd watcher stop against race condition
This commit is contained in:
parent
712fe39a62
commit
e7cc3c2210
@ -3,6 +3,7 @@ package etcd
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/coreos/etcd/clientv3"
|
"github.com/coreos/etcd/clientv3"
|
||||||
@ -10,10 +11,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type etcdWatcher struct {
|
type etcdWatcher struct {
|
||||||
stop chan bool
|
|
||||||
w clientv3.WatchChan
|
w clientv3.WatchChan
|
||||||
client *clientv3.Client
|
client *clientv3.Client
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
|
|
||||||
|
mtx sync.Mutex
|
||||||
|
stop chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newEtcdWatcher(r *etcdRegistry, timeout time.Duration, opts ...registry.WatchOption) (registry.Watcher, error) {
|
func newEtcdWatcher(r *etcdRegistry, timeout time.Duration, opts ...registry.WatchOption) (registry.Watcher, error) {
|
||||||
@ -90,6 +93,9 @@ func (ew *etcdWatcher) Next() (*registry.Result, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ew *etcdWatcher) Stop() {
|
func (ew *etcdWatcher) Stop() {
|
||||||
|
ew.mtx.Lock()
|
||||||
|
defer ew.mtx.Unlock()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ew.stop:
|
case <-ew.stop:
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user