fix(config): apollo close function: useless infinite loop causes high cpu usage (#1674)

Co-authored-by: wangshaosen <wangshaosen@videopls.com>
pull/1681/head
xiaoxiaodek 3 years ago committed by GitHub
parent c9e19f1d98
commit 413cc77f7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      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 {

Loading…
Cancel
Save