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.
46 lines
998 B
46 lines
998 B
3 years ago
|
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) {
|
||
|
}
|
||
|
|
||
|
func (c *customChangeListener) OnNewestChange(changeEvent *storage.FullChangeEvent) {
|
||
3 years ago
|
kv := make([]*config.KeyValue, len(changeEvent.Changes))
|
||
3 years ago
|
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) {
|
||
3 years ago
|
e := make(chan []*config.KeyValue)
|
||
3 years ago
|
a.client.AddChangeListener(&customChangeListener{})
|
||
|
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
|
||
|
}
|