1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00
go-micro/registry/util.go

143 lines
2.4 KiB
Go
Raw Normal View History

package registry
2018-12-04 18:41:40 +02:00
2019-07-04 12:36:49 +02:00
func addNodes(old, neu []*Node) []*Node {
nodes := make([]*Node, len(neu))
2019-07-04 12:36:49 +02:00
// add all new nodes
for i, n := range neu {
2019-07-04 12:36:49 +02:00
node := *n
nodes[i] = &node
2019-07-04 12:36:49 +02:00
}
// look at old nodes
for _, o := range old {
var exists bool
// check against new nodes
for _, n := range nodes {
// ids match then skip
if o.Id == n.Id {
exists = true
break
}
}
// keep old node
if !exists {
node := *o
nodes = append(nodes, &node)
}
}
return nodes
}
func delNodes(old, del []*Node) []*Node {
var nodes []*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
}
// Copy makes a copy of services
func Copy(current []*Service) []*Service {
services := make([]*Service, len(current))
for i, service := range current {
2018-12-04 18:41:40 +02:00
// copy service
s := new(Service)
2018-12-04 18:41:40 +02:00
*s = *service
// copy nodes
nodes := make([]*Node, len(service.Nodes))
for j, node := range service.Nodes {
n := new(Node)
2018-12-04 18:41:40 +02:00
*n = *node
nodes[j] = n
2018-12-04 18:41:40 +02:00
}
s.Nodes = nodes
// copy endpoints
eps := make([]*Endpoint, len(service.Endpoints))
for j, ep := range service.Endpoints {
e := new(Endpoint)
2018-12-04 18:41:40 +02:00
*e = *ep
eps[j] = e
2018-12-04 18:41:40 +02:00
}
s.Endpoints = eps
// append service
services[i] = s
2018-12-04 18:41:40 +02:00
}
return services
}
2019-07-04 12:36:49 +02:00
// Merge merges two lists of services and returns a new copy
func Merge(olist []*Service, nlist []*Service) []*Service {
var srv []*Service
2019-02-01 11:05:03 +02:00
for _, n := range nlist {
2018-12-04 18:41:40 +02:00
var seen bool
for _, o := range olist {
if o.Version == n.Version {
sp := new(Service)
2019-02-01 11:05:03 +02:00
// make copy
*sp = *o
// set nodes
2019-07-04 12:36:49 +02:00
sp.Nodes = addNodes(o.Nodes, n.Nodes)
2019-02-01 11:05:03 +02:00
// mark as seen
2018-12-04 18:41:40 +02:00
seen = true
2019-02-01 11:05:03 +02:00
srv = append(srv, sp)
2018-12-04 18:41:40 +02:00
break
} else {
sp := new(Service)
// make copy
*sp = *o
srv = append(srv, sp)
2018-12-04 18:41:40 +02:00
}
}
if !seen {
2019-07-04 12:36:49 +02:00
srv = append(srv, Copy([]*Service{n})...)
2018-12-04 18:41:40 +02:00
}
}
2019-02-01 11:05:03 +02:00
return srv
2018-12-04 18:41:40 +02:00
}
2019-07-04 12:36:49 +02:00
// Remove removes services and returns a new copy
func Remove(old, del []*Service) []*Service {
var services []*Service
for _, o := range old {
srv := new(Service)
*srv = *o
2018-12-04 18:41:40 +02:00
var rem bool
2018-12-04 18:41:40 +02:00
for _, s := range del {
if srv.Version == s.Version {
2019-07-04 12:36:49 +02:00
srv.Nodes = delNodes(srv.Nodes, s.Nodes)
if len(srv.Nodes) == 0 {
2018-12-04 18:41:40 +02:00
rem = true
}
}
}
2018-12-04 18:41:40 +02:00
if !rem {
services = append(services, srv)
2018-12-04 18:41:40 +02:00
}
}
2018-12-04 18:41:40 +02:00
return services
}