2015-05-28 00:22:02 +03:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
2015-09-26 19:09:03 +03:00
|
|
|
etcd "github.com/coreos/etcd/client"
|
2015-11-20 19:17:33 +03:00
|
|
|
"github.com/micro/go-micro/registry"
|
2015-09-26 19:09:03 +03:00
|
|
|
"golang.org/x/net/context"
|
2015-05-28 00:22:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type etcdWatcher struct {
|
|
|
|
registry *etcdRegistry
|
|
|
|
stop chan bool
|
|
|
|
}
|
|
|
|
|
2015-11-08 04:48:48 +03:00
|
|
|
func addNodes(old, neu []*registry.Node) []*registry.Node {
|
|
|
|
for _, n := range neu {
|
|
|
|
var seen bool
|
|
|
|
for i, o := range old {
|
|
|
|
if o.Id == n.Id {
|
|
|
|
seen = true
|
|
|
|
old[i] = n
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !seen {
|
|
|
|
old = append(old, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return old
|
|
|
|
}
|
|
|
|
|
|
|
|
func addServices(old, neu []*registry.Service) []*registry.Service {
|
|
|
|
for _, s := range neu {
|
|
|
|
var seen bool
|
|
|
|
for i, o := range old {
|
|
|
|
if o.Version == s.Version {
|
|
|
|
s.Nodes = addNodes(o.Nodes, s.Nodes)
|
|
|
|
seen = true
|
|
|
|
old[i] = s
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !seen {
|
|
|
|
old = append(old, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return old
|
|
|
|
}
|
|
|
|
|
|
|
|
func delNodes(old, del []*registry.Node) []*registry.Node {
|
|
|
|
var nodes []*registry.Node
|
|
|
|
for _, o := range old {
|
|
|
|
var rem bool
|
|
|
|
for _, n := range del {
|
|
|
|
if o.Id == n.Id {
|
|
|
|
rem = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !rem {
|
|
|
|
nodes = append(nodes, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
func delServices(old, del []*registry.Service) []*registry.Service {
|
|
|
|
var services []*registry.Service
|
|
|
|
for i, o := range old {
|
|
|
|
var rem bool
|
|
|
|
for _, s := range del {
|
|
|
|
if o.Version == s.Version {
|
|
|
|
old[i].Nodes = delNodes(o.Nodes, s.Nodes)
|
|
|
|
if len(old[i].Nodes) == 0 {
|
|
|
|
rem = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !rem {
|
|
|
|
services = append(services, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
func newEtcdWatcher(r *etcdRegistry) (registry.Watcher, error) {
|
2015-05-28 00:22:02 +03:00
|
|
|
ew := &etcdWatcher{
|
|
|
|
registry: r,
|
|
|
|
stop: make(chan bool),
|
|
|
|
}
|
|
|
|
|
2015-09-26 19:09:03 +03:00
|
|
|
w := r.client.Watcher(prefix, &etcd.WatcherOptions{AfterIndex: 0, Recursive: true})
|
2015-05-28 00:22:02 +03:00
|
|
|
|
2015-09-26 19:09:03 +03:00
|
|
|
c := context.Background()
|
|
|
|
ctx, cancel := context.WithCancel(c)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-ew.stop
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
|
|
|
go ew.watch(ctx, w)
|
2015-05-28 00:22:02 +03:00
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
return ew, nil
|
|
|
|
}
|
2015-05-28 00:22:02 +03:00
|
|
|
|
2015-09-26 19:09:03 +03:00
|
|
|
func (e *etcdWatcher) watch(ctx context.Context, w etcd.Watcher) {
|
|
|
|
for {
|
|
|
|
rsp, err := w.Next(ctx)
|
|
|
|
if err != nil && ctx.Err() != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
if rsp.Node.Dir {
|
|
|
|
continue
|
|
|
|
}
|
2015-05-28 00:22:02 +03:00
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
s := decode(rsp.Node.Value)
|
|
|
|
if s == nil {
|
|
|
|
continue
|
|
|
|
}
|
2015-05-28 00:22:02 +03:00
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
e.registry.Lock()
|
|
|
|
|
|
|
|
service, ok := e.registry.services[s.Name]
|
|
|
|
if !ok {
|
|
|
|
if rsp.Action == "create" {
|
2015-11-08 04:48:48 +03:00
|
|
|
e.registry.services[s.Name] = []*registry.Service{s}
|
2015-05-28 00:22:02 +03:00
|
|
|
}
|
2015-06-01 20:55:27 +03:00
|
|
|
e.registry.Unlock()
|
|
|
|
continue
|
|
|
|
}
|
2015-05-28 00:22:02 +03:00
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
switch rsp.Action {
|
|
|
|
case "delete":
|
2015-11-08 04:48:48 +03:00
|
|
|
services := delServices(service, []*registry.Service{s})
|
|
|
|
if len(services) > 0 {
|
|
|
|
e.registry.services[s.Name] = services
|
|
|
|
} else {
|
|
|
|
delete(e.registry.services, s.Name)
|
2015-05-28 00:22:02 +03:00
|
|
|
}
|
2015-06-01 20:55:27 +03:00
|
|
|
case "create":
|
2015-11-08 04:48:48 +03:00
|
|
|
e.registry.services[s.Name] = addServices(service, []*registry.Service{s})
|
2015-05-28 00:22:02 +03:00
|
|
|
}
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
e.registry.Unlock()
|
|
|
|
}
|
2015-05-28 00:22:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ew *etcdWatcher) Stop() {
|
|
|
|
ew.stop <- true
|
|
|
|
}
|