You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.2 KiB
53 lines
1.2 KiB
package apollo
|
|
|
|
import (
|
|
"github.com/go-kratos/kratos/v2/config"
|
|
|
|
"github.com/apolloconfig/agollo/v4/storage"
|
|
)
|
|
|
|
type watcher struct {
|
|
event chan []*config.KeyValue
|
|
}
|
|
|
|
type customChangeListener struct {
|
|
event chan []*config.KeyValue
|
|
}
|
|
|
|
func (c *customChangeListener) OnChange(changeEvent *storage.ChangeEvent) {
|
|
kv := make([]*config.KeyValue, 0)
|
|
for key, value := range changeEvent.Changes {
|
|
kv = append(kv, &config.KeyValue{
|
|
Key: key,
|
|
Value: []byte(value.NewValue.(string)),
|
|
})
|
|
}
|
|
c.event <- kv
|
|
}
|
|
|
|
func (c *customChangeListener) OnNewestChange(changeEvent *storage.FullChangeEvent) {
|
|
kv := make([]*config.KeyValue, 0)
|
|
for key, value := range changeEvent.Changes {
|
|
kv = append(kv, &config.KeyValue{
|
|
Key: key,
|
|
Value: []byte(value.(string)),
|
|
})
|
|
}
|
|
c.event <- kv
|
|
}
|
|
|
|
func NewWatcher(a *apollo) (config.Watcher, error) {
|
|
e := make(chan []*config.KeyValue)
|
|
a.client.AddChangeListener(&customChangeListener{event: e})
|
|
return &watcher{event: e}, nil
|
|
}
|
|
|
|
// Next will be blocked until the Stop method is called
|
|
func (w *watcher) Next() ([]*config.KeyValue, error) {
|
|
return <-w.event, nil
|
|
}
|
|
|
|
func (w *watcher) Stop() error {
|
|
close(w.event)
|
|
return nil
|
|
}
|
|
|