mirror of
https://github.com/go-micro/go-micro.git
synced 2025-08-04 21:42:57 +02:00
add support for streaming requests. cleanup watcher initilisation
This commit is contained in:
@ -147,8 +147,9 @@ func (e *etcdRegistry) ListServices() ([]*registry.Service, error) {
|
||||
return services, nil
|
||||
}
|
||||
|
||||
func (e *etcdRegistry) Watch() {
|
||||
newEtcdWatcher(e)
|
||||
func (e *etcdRegistry) Watch() (registry.Watcher, error) {
|
||||
// todo: fix watcher
|
||||
return newEtcdWatcher(e)
|
||||
}
|
||||
|
||||
func NewRegistry(addrs []string, opt ...registry.Option) registry.Registry {
|
||||
@ -170,8 +171,5 @@ func NewRegistry(addrs []string, opt ...registry.Option) registry.Registry {
|
||||
services: make(map[string]*registry.Service),
|
||||
}
|
||||
|
||||
// Need to fix watcher
|
||||
// e.Watch()
|
||||
|
||||
return e
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ type etcdWatcher struct {
|
||||
stop chan bool
|
||||
}
|
||||
|
||||
func newEtcdWatcher(r *etcdRegistry) *etcdWatcher {
|
||||
func newEtcdWatcher(r *etcdRegistry) (registry.Watcher, error) {
|
||||
ew := &etcdWatcher{
|
||||
registry: r,
|
||||
stop: make(chan bool),
|
||||
@ -19,53 +19,55 @@ func newEtcdWatcher(r *etcdRegistry) *etcdWatcher {
|
||||
ch := make(chan *etcd.Response)
|
||||
|
||||
go r.client.Watch(prefix, 0, true, ch, ew.stop)
|
||||
go ew.watch(ch)
|
||||
|
||||
go func() {
|
||||
for rsp := range ch {
|
||||
if rsp.Node.Dir {
|
||||
continue
|
||||
}
|
||||
return ew, nil
|
||||
}
|
||||
|
||||
s := decode(rsp.Node.Value)
|
||||
if s == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
r.Lock()
|
||||
|
||||
service, ok := r.services[s.Name]
|
||||
if !ok {
|
||||
if rsp.Action == "create" {
|
||||
r.services[s.Name] = s
|
||||
}
|
||||
r.Unlock()
|
||||
continue
|
||||
}
|
||||
|
||||
switch rsp.Action {
|
||||
case "delete":
|
||||
var nodes []*registry.Node
|
||||
for _, node := range service.Nodes {
|
||||
var seen bool
|
||||
for _, n := range s.Nodes {
|
||||
if node.Id == n.Id {
|
||||
seen = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !seen {
|
||||
nodes = append(nodes, node)
|
||||
}
|
||||
}
|
||||
service.Nodes = nodes
|
||||
case "create":
|
||||
service.Nodes = append(service.Nodes, s.Nodes...)
|
||||
}
|
||||
r.Unlock()
|
||||
func (e *etcdWatcher) watch(ch chan *etcd.Response) {
|
||||
for rsp := range ch {
|
||||
if rsp.Node.Dir {
|
||||
continue
|
||||
}
|
||||
}()
|
||||
|
||||
return ew
|
||||
s := decode(rsp.Node.Value)
|
||||
if s == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
e.registry.Lock()
|
||||
|
||||
service, ok := e.registry.services[s.Name]
|
||||
if !ok {
|
||||
if rsp.Action == "create" {
|
||||
e.registry.services[s.Name] = s
|
||||
}
|
||||
e.registry.Unlock()
|
||||
continue
|
||||
}
|
||||
|
||||
switch rsp.Action {
|
||||
case "delete":
|
||||
var nodes []*registry.Node
|
||||
for _, node := range service.Nodes {
|
||||
var seen bool
|
||||
for _, n := range s.Nodes {
|
||||
if node.Id == n.Id {
|
||||
seen = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !seen {
|
||||
nodes = append(nodes, node)
|
||||
}
|
||||
}
|
||||
service.Nodes = nodes
|
||||
case "create":
|
||||
service.Nodes = append(service.Nodes, s.Nodes...)
|
||||
}
|
||||
|
||||
e.registry.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
func (ew *etcdWatcher) Stop() {
|
||||
|
Reference in New Issue
Block a user