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

JUMEI_ARCH/go-plugins
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
watcher.go 1.37 KB
一键复制 编辑 原始数据 按行查看 历史
Asim Aslam 提交于 2017-10-03 11:53 . Fix vetting code
package etcd
import (
"sync"
etcd "github.com/coreos/etcd/client"
"github.com/micro/go-micro/registry"
"golang.org/x/net/context"
)
type etcdWatcher struct {
ctx context.Context
once *sync.Once
stop chan bool
w etcd.Watcher
}
func newEtcdWatcher(r *etcdRegistry) (registry.Watcher, error) {
var once sync.Once
ctx, cancel := context.WithCancel(context.Background())
stop := make(chan bool, 1)
go func() {
<-stop
cancel()
}()
return &etcdWatcher{
ctx: ctx,
w: r.client.Watcher(prefix, &etcd.WatcherOptions{AfterIndex: 0, Recursive: true}),
once: &once,
stop: stop,
}, nil
}
func (ew *etcdWatcher) Next() (*registry.Result, error) {
for {
rsp, err := ew.w.Next(ew.ctx)
if err != nil && ew.ctx.Err() != nil {
return nil, err
}
if rsp.Node.Dir {
continue
}
service := decode(rsp.Node.Value)
if service == nil {
switch {
case rsp.Action != "delete":
continue
case rsp.PrevNode == nil:
continue
}
// last ditch effort
service = decode(rsp.PrevNode.Value)
if service == nil {
continue
}
}
switch rsp.Action {
case "set", "delete", "create", "update":
if rsp.Action == "set" {
rsp.Action = "update"
}
return &registry.Result{
Action: rsp.Action,
Service: service,
}, nil
default:
continue
}
}
}
func (ew *etcdWatcher) Stop() {
ew.once.Do(func() {
ew.stop <- true
})
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/JMArch/go-plugins.git
git@gitee.com:JMArch/go-plugins.git
JMArch
go-plugins
go-plugins
v0.6.1

搜索帮助