1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-02-05 13:15:11 +02:00
kratos/contrib/config/etcd/watcher.go
Windfarer 1148bbd968
refactor(contrib/config): move etcd config (#1457)
* move etcd config

* add with prefix to options
2021-09-10 21:46:16 +08:00

45 lines
749 B
Go

package etcd
import (
"github.com/go-kratos/kratos/v2/config"
clientv3 "go.etcd.io/etcd/client/v3"
)
type watcher struct {
source *source
ch clientv3.WatchChan
closeChan chan struct{}
}
func newWatcher(s *source) *watcher {
w := &watcher{
source: s,
closeChan: make(chan struct{}),
}
var opts []clientv3.OpOption
if s.options.prefix {
opts = append(opts, clientv3.WithPrefix())
}
w.ch = s.client.Watch(s.options.ctx, s.options.path, opts...)
return w
}
func (s *watcher) Next() ([]*config.KeyValue, error) {
select {
case _, ok := <-s.ch:
if !ok {
return nil, nil
}
return s.source.Load()
case <-s.closeChan:
return nil, nil
}
}
func (s *watcher) Stop() error {
close(s.closeChan)
return nil
}