mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-14 02:33:03 +02:00
feat(config): add ErrorWatcherStopped and return on watcher.Next() after Watcher.Stop() (#2092)
* feat(config): add ErrorWatcherStopped and return watcher.Next() after watcher.Stop() * fix: remove unreachable code * fix: return err when etcd WatcheResponse hold error
This commit is contained in:
parent
752f011ba1
commit
4f8d8ef8da
@ -20,6 +20,8 @@ var (
|
||||
ErrNotFound = errors.New("key not found")
|
||||
// ErrTypeAssert is type assert error.
|
||||
ErrTypeAssert = errors.New("type assert error")
|
||||
// ErrWatcherStopped means watcher is stopped.
|
||||
ErrWatcherStopped = errors.New("warcher stopped")
|
||||
|
||||
_ Config = (*config)(nil)
|
||||
)
|
||||
|
@ -1,8 +1,6 @@
|
||||
package apollo
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/config"
|
||||
"github.com/go-kratos/kratos/v2/log"
|
||||
|
||||
@ -62,7 +60,7 @@ func newWatcher(a *apollo) (config.Watcher, error) {
|
||||
func (w *watcher) Next() ([]*config.KeyValue, error) {
|
||||
kv, ok := <-w.out
|
||||
if !ok {
|
||||
return nil, context.Canceled
|
||||
return nil, config.ErrWatcherStopped
|
||||
}
|
||||
return kv, nil
|
||||
}
|
||||
|
@ -54,13 +54,10 @@ func newWatcher(s *source) (*watcher, error) {
|
||||
|
||||
func (w *watcher) Next() ([]*config.KeyValue, error) {
|
||||
select {
|
||||
case _, ok := <-w.ch:
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
case <-w.ch:
|
||||
return w.source.Load()
|
||||
case <-w.closeChan:
|
||||
return nil, nil
|
||||
return nil, config.ErrWatcherStopped
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,13 +28,13 @@ func newWatcher(s *source) *watcher {
|
||||
|
||||
func (s *watcher) Next() ([]*config.KeyValue, error) {
|
||||
select {
|
||||
case _, ok := <-s.ch:
|
||||
if !ok {
|
||||
return nil, nil
|
||||
case resp := <-s.ch:
|
||||
if resp.Err() != nil {
|
||||
return nil, resp.Err()
|
||||
}
|
||||
return s.source.Load()
|
||||
case <-s.closeChan:
|
||||
return nil, nil
|
||||
return nil, config.ErrWatcherStopped
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -106,7 +106,7 @@ func (c *Config) Watch() (config.Watcher, error) {
|
||||
err := c.client.ListenConfig(vo.ConfigParam{
|
||||
DataId: c.opts.dataID,
|
||||
Group: c.opts.group,
|
||||
OnChange: func(namespace, group, dataId, data string) {
|
||||
OnChange: func(_, group, dataId, data string) {
|
||||
if dataId == watcher.dataID && group == watcher.group {
|
||||
watcher.content <- data
|
||||
}
|
||||
@ -144,8 +144,8 @@ func newWatcher(ctx context.Context, dataID string, group string, cancelListenCo
|
||||
|
||||
func (w *Watcher) Next() ([]*config.KeyValue, error) {
|
||||
select {
|
||||
case <-w.Context.Done():
|
||||
return nil, nil
|
||||
case <-w.Done():
|
||||
return nil, config.ErrWatcherStopped
|
||||
case content := <-w.content:
|
||||
k := w.dataID
|
||||
return []*config.KeyValue{
|
||||
|
Loading…
Reference in New Issue
Block a user