2021-10-02 19:55:07 +03:00
|
|
|
package register // import "go.unistack.org/micro/v3/util/register"
|
2018-12-04 19:41:40 +03:00
|
|
|
|
2020-04-10 19:41:10 +03:00
|
|
|
import (
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/register"
|
2020-04-10 19:41:10 +03:00
|
|
|
)
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
func addNodes(old, neu []*register.Node) []*register.Node {
|
2021-04-27 08:32:47 +03:00
|
|
|
nodes := make([]*register.Node, 0, len(neu))
|
2019-07-04 13:36:49 +03:00
|
|
|
// add all new nodes
|
2021-04-27 08:32:47 +03:00
|
|
|
for _, n := range neu {
|
2019-07-04 13:36:49 +03:00
|
|
|
node := *n
|
2021-04-27 08:32:47 +03:00
|
|
|
nodes = append(nodes, &node)
|
2019-07-04 13:36:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// look at old nodes
|
|
|
|
for _, o := range old {
|
|
|
|
var exists bool
|
|
|
|
|
|
|
|
// check against new nodes
|
|
|
|
for _, n := range nodes {
|
|
|
|
// ids match then skip
|
2021-04-27 08:32:47 +03:00
|
|
|
if o.ID == n.ID {
|
2019-07-04 13:36:49 +03:00
|
|
|
exists = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep old node
|
|
|
|
if !exists {
|
|
|
|
node := *o
|
|
|
|
nodes = append(nodes, &node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
func delNodes(old, del []*register.Node) []*register.Node {
|
|
|
|
var nodes []*register.Node
|
2019-07-04 13:36:49 +03:00
|
|
|
for _, o := range old {
|
|
|
|
var rem bool
|
|
|
|
for _, n := range del {
|
2021-04-27 08:32:47 +03:00
|
|
|
if o.ID == n.ID {
|
2019-07-04 13:36:49 +03:00
|
|
|
rem = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !rem {
|
|
|
|
nodes = append(nodes, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2019-09-30 05:36:43 +03:00
|
|
|
// CopyService make a copy of service
|
2021-01-29 13:17:32 +03:00
|
|
|
func CopyService(service *register.Service) *register.Service {
|
2019-09-30 05:36:43 +03:00
|
|
|
// copy service
|
2021-01-29 13:17:32 +03:00
|
|
|
s := ®ister.Service{}
|
2019-09-30 05:36:43 +03:00
|
|
|
*s = *service
|
|
|
|
|
|
|
|
// copy nodes
|
2021-01-29 13:17:32 +03:00
|
|
|
nodes := make([]*register.Node, len(service.Nodes))
|
2019-09-30 05:36:43 +03:00
|
|
|
for j, node := range service.Nodes {
|
2021-01-29 13:17:32 +03:00
|
|
|
n := ®ister.Node{}
|
2019-09-30 05:36:43 +03:00
|
|
|
*n = *node
|
|
|
|
nodes[j] = n
|
|
|
|
}
|
|
|
|
s.Nodes = nodes
|
|
|
|
|
|
|
|
// copy endpoints
|
2021-01-29 13:17:32 +03:00
|
|
|
eps := make([]*register.Endpoint, len(service.Endpoints))
|
2019-09-30 05:36:43 +03:00
|
|
|
for j, ep := range service.Endpoints {
|
2021-01-29 13:17:32 +03:00
|
|
|
e := ®ister.Endpoint{}
|
2019-09-30 05:36:43 +03:00
|
|
|
*e = *ep
|
|
|
|
eps[j] = e
|
|
|
|
}
|
|
|
|
s.Endpoints = eps
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-07-04 13:36:49 +03:00
|
|
|
// Copy makes a copy of services
|
2021-01-29 13:17:32 +03:00
|
|
|
func Copy(current []*register.Service) []*register.Service {
|
|
|
|
services := make([]*register.Service, len(current))
|
2019-07-15 13:13:58 +03:00
|
|
|
for i, service := range current {
|
2019-09-30 05:36:43 +03:00
|
|
|
services[i] = CopyService(service)
|
2018-12-04 19:41:40 +03:00
|
|
|
}
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2019-07-04 13:36:49 +03:00
|
|
|
// Merge merges two lists of services and returns a new copy
|
2021-01-29 13:17:32 +03:00
|
|
|
func Merge(olist []*register.Service, nlist []*register.Service) []*register.Service {
|
|
|
|
var srv []*register.Service
|
2019-02-01 12:05:03 +03:00
|
|
|
|
2019-07-04 00:15:44 +03:00
|
|
|
for _, n := range nlist {
|
2018-12-04 19:41:40 +03:00
|
|
|
var seen bool
|
2019-07-04 00:15:44 +03:00
|
|
|
for _, o := range olist {
|
|
|
|
if o.Version == n.Version {
|
2021-01-29 13:17:32 +03:00
|
|
|
sp := ®ister.Service{}
|
2019-02-01 12:05:03 +03:00
|
|
|
// make copy
|
|
|
|
*sp = *o
|
|
|
|
// set nodes
|
2019-07-04 13:36:49 +03:00
|
|
|
sp.Nodes = addNodes(o.Nodes, n.Nodes)
|
2019-02-01 12:05:03 +03:00
|
|
|
|
|
|
|
// 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
|
2019-07-04 00:15:44 +03:00
|
|
|
} else {
|
2021-01-29 13:17:32 +03:00
|
|
|
sp := ®ister.Service{}
|
2019-07-04 00:15:44 +03:00
|
|
|
// make copy
|
|
|
|
*sp = *o
|
|
|
|
srv = append(srv, sp)
|
2018-12-04 19:41:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !seen {
|
2021-01-29 13:17:32 +03:00
|
|
|
srv = append(srv, Copy([]*register.Service{n})...)
|
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
|
|
|
}
|
|
|
|
|
2019-07-04 13:36:49 +03:00
|
|
|
// Remove removes services and returns a new copy
|
2021-01-29 13:17:32 +03:00
|
|
|
func Remove(old, del []*register.Service) []*register.Service {
|
|
|
|
var services []*register.Service
|
2019-06-22 20:58:12 +03:00
|
|
|
|
2018-12-29 18:44:51 +03:00
|
|
|
for _, o := range old {
|
2021-01-29 13:17:32 +03:00
|
|
|
srv := ®ister.Service{}
|
2019-06-22 20:58:12 +03:00
|
|
|
*srv = *o
|
|
|
|
|
2018-12-04 19:41:40 +03:00
|
|
|
var rem bool
|
2019-06-22 20:58:12 +03:00
|
|
|
|
2018-12-04 19:41:40 +03:00
|
|
|
for _, s := range del {
|
2019-06-22 20:58:12 +03:00
|
|
|
if srv.Version == s.Version {
|
2019-07-04 13:36:49 +03:00
|
|
|
srv.Nodes = delNodes(srv.Nodes, s.Nodes)
|
2019-06-22 20:58:12 +03:00
|
|
|
|
|
|
|
if len(srv.Nodes) == 0 {
|
2018-12-04 19:41:40 +03:00
|
|
|
rem = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-22 20:58:12 +03:00
|
|
|
|
2018-12-04 19:41:40 +03:00
|
|
|
if !rem {
|
2019-06-22 20:58:12 +03:00
|
|
|
services = append(services, srv)
|
2018-12-04 19:41:40 +03:00
|
|
|
}
|
|
|
|
}
|
2019-06-22 20:58:12 +03:00
|
|
|
|
2018-12-04 19:41:40 +03:00
|
|
|
return services
|
|
|
|
}
|