2018-12-04 19:41:40 +03:00
|
|
|
package gossip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/micro/go-micro/registry"
|
|
|
|
)
|
|
|
|
|
|
|
|
func cp(current []*registry.Service) []*registry.Service {
|
|
|
|
var services []*registry.Service
|
|
|
|
|
|
|
|
for _, service := range current {
|
|
|
|
// copy service
|
|
|
|
s := new(registry.Service)
|
|
|
|
*s = *service
|
|
|
|
|
|
|
|
// copy nodes
|
|
|
|
var nodes []*registry.Node
|
|
|
|
for _, node := range service.Nodes {
|
|
|
|
n := new(registry.Node)
|
|
|
|
*n = *node
|
|
|
|
nodes = append(nodes, n)
|
|
|
|
}
|
|
|
|
s.Nodes = nodes
|
|
|
|
|
|
|
|
// copy endpoints
|
|
|
|
var eps []*registry.Endpoint
|
|
|
|
for _, ep := range service.Endpoints {
|
|
|
|
e := new(registry.Endpoint)
|
|
|
|
*e = *ep
|
|
|
|
eps = append(eps, e)
|
|
|
|
}
|
|
|
|
s.Endpoints = eps
|
|
|
|
|
|
|
|
// append service
|
|
|
|
services = append(services, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
|
|
|
func addNodes(old, neu []*registry.Node) []*registry.Node {
|
2018-12-29 18:44:51 +03:00
|
|
|
var nodes []*registry.Node
|
|
|
|
|
|
|
|
// add all new nodes
|
2018-12-04 19:41:40 +03:00
|
|
|
for _, n := range neu {
|
2018-12-29 18:44:51 +03:00
|
|
|
node := *n
|
|
|
|
nodes = append(nodes, &node)
|
|
|
|
}
|
|
|
|
|
|
|
|
// look at old nodes
|
|
|
|
for _, o := range old {
|
|
|
|
var exists bool
|
|
|
|
|
|
|
|
// check against new nodes
|
|
|
|
for _, n := range nodes {
|
|
|
|
// ids match then skip
|
2018-12-04 19:41:40 +03:00
|
|
|
if o.Id == n.Id {
|
2018-12-29 18:44:51 +03:00
|
|
|
exists = true
|
2018-12-04 19:41:40 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-12-29 18:44:51 +03:00
|
|
|
|
|
|
|
// keep old node
|
|
|
|
if !exists {
|
|
|
|
node := *o
|
|
|
|
nodes = append(nodes, &node)
|
2018-12-04 19:41:40 +03:00
|
|
|
}
|
|
|
|
}
|
2018-12-29 18:44:51 +03:00
|
|
|
|
|
|
|
return nodes
|
2018-12-04 19:41:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func addServices(old, neu []*registry.Service) []*registry.Service {
|
2019-02-01 12:05:03 +03:00
|
|
|
var srv []*registry.Service
|
|
|
|
|
2018-12-04 19:41:40 +03:00
|
|
|
for _, s := range neu {
|
|
|
|
var seen bool
|
2019-02-01 12:05:03 +03:00
|
|
|
for _, o := range old {
|
2018-12-04 19:41:40 +03:00
|
|
|
if o.Version == s.Version {
|
2019-02-01 19:01:51 +03:00
|
|
|
sp := new(registry.Service)
|
2019-02-01 12:05:03 +03:00
|
|
|
// make copy
|
|
|
|
*sp = *o
|
|
|
|
// set nodes
|
|
|
|
sp.Nodes = addNodes(o.Nodes, s.Nodes)
|
|
|
|
|
|
|
|
// mark as seen
|
2018-12-04 19:41:40 +03:00
|
|
|
seen = true
|
2019-02-01 12:05:03 +03:00
|
|
|
srv = append(srv, sp)
|
2018-12-04 19:41:40 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !seen {
|
2019-02-01 12:05:03 +03:00
|
|
|
srv = append(srv, cp([]*registry.Service{s})...)
|
2018-12-04 19:41:40 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-01 12:05:03 +03:00
|
|
|
|
|
|
|
return srv
|
2018-12-04 19:41:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-12-29 18:44:51 +03:00
|
|
|
|
|
|
|
for _, o := range old {
|
|
|
|
srv := new(registry.Service)
|
|
|
|
*srv = *o
|
|
|
|
|
2018-12-04 19:41:40 +03:00
|
|
|
var rem bool
|
2018-12-29 18:44:51 +03:00
|
|
|
|
2018-12-04 19:41:40 +03:00
|
|
|
for _, s := range del {
|
2018-12-29 18:44:51 +03:00
|
|
|
if srv.Version == s.Version {
|
|
|
|
srv.Nodes = delNodes(srv.Nodes, s.Nodes)
|
|
|
|
|
|
|
|
if len(srv.Nodes) == 0 {
|
2018-12-04 19:41:40 +03:00
|
|
|
rem = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-29 18:44:51 +03:00
|
|
|
|
2018-12-04 19:41:40 +03:00
|
|
|
if !rem {
|
2018-12-29 18:44:51 +03:00
|
|
|
services = append(services, srv)
|
2018-12-04 19:41:40 +03:00
|
|
|
}
|
|
|
|
}
|
2018-12-29 18:44:51 +03:00
|
|
|
|
2018-12-04 19:41:40 +03:00
|
|
|
return services
|
|
|
|
}
|