diff --git a/contrib/config/apollo/watcher.go b/contrib/config/apollo/watcher.go index b1eea6a31..c36d30046 100644 --- a/contrib/config/apollo/watcher.go +++ b/contrib/config/apollo/watcher.go @@ -1,6 +1,7 @@ package apollo import ( + "context" "fmt" "github.com/go-kratos/kratos/v2/config" @@ -66,11 +67,13 @@ func newWatcher(a *apollo, logger log.Logger) (config.Watcher, error) { } changeCh := make(chan []*config.KeyValue) - a.client.AddChangeListener(&customChangeListener{in: changeCh, logger: logger}) + listener := &customChangeListener{in: changeCh, logger: logger} + a.client.AddChangeListener(listener) return &watcher{ out: changeCh, cancelFn: func() { + a.client.RemoveChangeListener(listener) close(changeCh) }, }, nil @@ -78,7 +81,11 @@ func newWatcher(a *apollo, logger log.Logger) (config.Watcher, error) { // Next will be blocked until the Stop method is called func (w *watcher) Next() ([]*config.KeyValue, error) { - return <-w.out, nil + kv, ok := <-w.out + if !ok { + return nil, context.Canceled + } + return kv, nil } func (w *watcher) Stop() error {