当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
2 Star 0 Fork 1

JUMEI_ARCH/go-plugins
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
watcher.go 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
Rene Kaufmann 提交于 2016-05-21 20:01 . some small fixes
package etcdv3
import (
"errors"
"path"
"time"
"github.com/coreos/etcd/clientv3"
"github.com/micro/go-micro/registry"
"golang.org/x/net/context"
)
type etcdv3Watcher struct {
stop chan bool
w clientv3.WatchChan
client *clientv3.Client
timeout time.Duration
}
func newEtcdv3Watcher(r *etcdv3Registry, timeout time.Duration) (registry.Watcher, error) {
ctx, cancel := context.WithCancel(context.Background())
stop := make(chan bool, 1)
go func() {
<-stop
cancel()
}()
return &etcdv3Watcher{
stop: stop,
w: r.client.Watch(ctx, prefix, clientv3.WithPrefix()),
client: r.client,
timeout: timeout,
}, nil
}
func (ew *etcdv3Watcher) Next() (*registry.Result, error) {
for wresp := range ew.w {
if wresp.Err() != nil {
return nil, wresp.Err()
}
for _, ev := range wresp.Events {
service := decode(ev.Kv.Value)
var action string
switch ev.Type {
case clientv3.EventTypePut:
if ev.IsCreate() {
action = "create"
} else if ev.IsModify() {
action = "update"
}
case clientv3.EventTypeDelete:
action = "delete"
// get the cached value
ctx, cancel := context.WithTimeout(context.Background(), ew.timeout)
defer cancel()
resp, err := ew.client.Get(ctx, path.Join(cachePrefix, string(ev.Kv.Key)))
if err != nil {
return nil, err
}
for _, ev := range resp.Kvs {
service = decode(ev.Value)
}
}
if service == nil {
continue
}
return &registry.Result{
Action: action,
Service: service,
}, nil
}
}
return nil, errors.New("could not get next")
}
func (ew *etcdv3Watcher) Stop() {
select {
case <-ew.stop:
return
default:
close(ew.stop)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/JMArch/go-plugins.git
git@gitee.com:JMArch/go-plugins.git
JMArch
go-plugins
go-plugins
v0.2.1

搜索帮助

0d507c66 1850385 C8b1a773 1850385