代码拉取完成,页面将自动刷新
package etcd
import (
"context"
"gitee.com/monobytes/gcore/gconfig"
"go.etcd.io/etcd/api/v3/mvccpb"
"go.etcd.io/etcd/client/v3"
"path/filepath"
"strings"
)
type watcher struct {
ctx context.Context
cancel context.CancelFunc
source *Source
watcher clientv3.Watcher
chWatch clientv3.WatchChan
}
func newWatcher(ctx context.Context, s *Source) (gconfig.Watcher, error) {
w := &watcher{}
w.ctx, w.cancel = context.WithCancel(ctx)
w.source = s
w.watcher = clientv3.NewWatcher(w.source.opts.client)
w.chWatch = w.watcher.Watch(w.ctx, w.source.opts.path, clientv3.WithPrefix())
return w, nil
}
// Next 返回服务实例列表
func (w *watcher) Next() ([]*gconfig.Configuration, error) {
select {
case <-w.ctx.Done():
return nil, w.ctx.Err()
case res, ok := <-w.chWatch:
if !ok {
if err := w.ctx.Err(); err != nil {
return nil, err
}
}
if res.Err() != nil {
return nil, res.Err()
}
configs := make([]*gconfig.Configuration, 0, len(res.Events))
for _, ev := range res.Events {
switch ev.Type {
case mvccpb.PUT:
fullPath := string(ev.Kv.Key)
path := strings.TrimPrefix(fullPath, w.source.opts.path)
file := filepath.Base(fullPath)
ext := filepath.Ext(file)
configs = append(configs, &gconfig.Configuration{
Path: path,
File: file,
Name: strings.TrimSuffix(file, ext),
Format: strings.TrimPrefix(ext, "."),
Content: ev.Kv.Value,
FullPath: fullPath,
})
}
}
return configs, nil
}
}
// Stop 停止监听
func (w *watcher) Stop() error {
w.cancel()
return w.watcher.Close()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。