2018-12-04 16:41:40 +00: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 15:44:51 +00:00
|
|
|
var nodes []*registry.Node
|
|
|
|
|
|
|
|
// add all new nodes
|
2018-12-04 16:41:40 +00:00
|
|
|
for _, n := range neu {
|
2018-12-29 15:44:51 +00: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 16:41:40 +00:00
|
|
|
if o.Id == n.Id {
|
2018-12-29 15:44:51 +00:00
|
|
|
exists = true
|
2018-12-04 16:41:40 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-12-29 15:44:51 +00:00
|
|
|
|
|
|
|
// keep old node
|
|
|
|
if !exists {
|
|
|
|
node := *o
|
|
|
|
nodes = append(nodes, &node)
|
2018-12-04 16:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-29 15:44:51 +00:00
|
|
|
|
|
|
|
return nodes
|
2018-12-04 16:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func addServices(old, neu []*registry.Service) []*registry.Service {
|
2019-02-01 09:05:03 +00:00
|
|
|
var srv []*registry.Service
|
|
|
|
|
2018-12-04 16:41:40 +00:00
|
|
|
for _, s := range neu {
|
|
|
|
var seen bool
|
2019-02-01 09:05:03 +00:00
|
|
|
for _, o := range old {
|
2018-12-04 16:41:40 +00:00
|
|
|
if o.Version == s.Version {
|
2019-02-01 16:01:51 +00:00
|
|
|
sp := new(registry.Service)
|
2019-02-01 09:05:03 +00:00
|
|
|
// make copy
|
|
|
|
*sp = *o
|
|
|
|
// set nodes
|
|
|
|
sp.Nodes = addNodes(o.Nodes, s.Nodes)
|
|
|
|
|
|
|
|
// mark as seen
|
2018-12-04 16:41:40 +00:00
|
|
|
seen = true
|
2019-02-01 09:05:03 +00:00
|
|
|
srv = append(srv, sp)
|
2018-12-04 16:41:40 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !seen {
|
2019-02-01 09:05:03 +00:00
|
|
|
srv = append(srv, cp([]*registry.Service{s})...)
|
2018-12-04 16:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-01 09:05:03 +00:00
|
|
|
|
|
|
|
return srv
|
2018-12-04 16:41:40 +00: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 15:44:51 +00:00
|
|
|
for _, o := range old {
|
2018-12-04 16:41:40 +00:00
|
|
|
var rem bool
|
|
|
|
for _, s := range del {
|
2019-06-18 21:39:00 +01:00
|
|
|
if o.Version == s.Version {
|
|
|
|
s.Nodes = delNodes(s.Nodes, o.Nodes)
|
|
|
|
if len(s.Nodes) == 0 {
|
2018-12-04 16:41:40 +00:00
|
|
|
rem = true
|
2019-06-18 21:39:00 +01:00
|
|
|
break
|
2018-12-04 16:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !rem {
|
2019-06-18 21:39:00 +01:00
|
|
|
services = append(services, o)
|
2018-12-04 16:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return services
|
|
|
|
}
|