1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-30 04:31:03 +02:00
Cluas 6fa5700c3c
chore(contrib/config): uniformly canceled by CancelFunc (#2111)
* chore: remove sentinel error for compatibility

* chore: core use lssentinel error

* chore: uniformly canceled by CancelFunc

* chore: remove error
2022-06-17 23:42:32 +08:00

83 lines
1.7 KiB
Go

package apollo
import (
"context"
"github.com/go-kratos/kratos/v2/config"
"github.com/go-kratos/kratos/v2/log"
"github.com/apolloconfig/agollo/v4/storage"
)
type watcher struct {
out <-chan []*config.KeyValue
ctx context.Context
cancelFn func()
}
type customChangeListener struct {
in chan<- []*config.KeyValue
apollo *apollo
}
func (c *customChangeListener) onChange(namespace string, changes map[string]*storage.ConfigChange) []*config.KeyValue {
kv := make([]*config.KeyValue, 0, 2)
value, err := c.apollo.client.GetConfigCache(namespace).Get("content")
if err != nil {
log.Warnw("apollo get config failed", "err", err)
}
kv = append(kv, &config.KeyValue{
Key: namespace,
Value: []byte(value.(string)),
Format: format(namespace),
})
return kv
}
func (c *customChangeListener) OnChange(changeEvent *storage.ChangeEvent) {
change := c.onChange(changeEvent.Namespace, changeEvent.Changes)
if len(change) == 0 {
return
}
c.in <- change
}
func (c *customChangeListener) OnNewestChange(changeEvent *storage.FullChangeEvent) {}
func newWatcher(a *apollo) (config.Watcher, error) {
changeCh := make(chan []*config.KeyValue)
listener := &customChangeListener{in: changeCh, apollo: a}
a.client.AddChangeListener(listener)
ctx, cancel := context.WithCancel(context.Background())
return &watcher{
out: changeCh,
ctx: ctx,
cancelFn: func() {
a.client.RemoveChangeListener(listener)
cancel()
},
}, nil
}
// Next will be blocked until the Stop method is called
func (w *watcher) Next() ([]*config.KeyValue, error) {
select {
case kv := <-w.out:
return kv, nil
case <-w.ctx.Done():
return nil, w.ctx.Err()
}
}
func (w *watcher) Stop() error {
if w.cancelFn != nil {
w.cancelFn()
}
return nil
}