micro/util/registry/util.go

149 lines
2.7 KiB
Go
Raw Normal View History

package registry
2018-12-04 19:41:40 +03:00
import (
"github.com/unistack-org/micro/v3/registry"
)
func addNodes(old, neu []*registry.Node) []*registry.Node {
nodes := make([]*registry.Node, len(neu))
2019-07-04 13:36:49 +03:00
// add all new nodes
for i, n := range neu {
2019-07-04 13:36:49 +03:00
node := *n
nodes[i] = &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
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 []*registry.Node) []*registry.Node {
var nodes []*registry.Node
2019-07-04 13:36:49 +03:00
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
}
// CopyService make a copy of service
func CopyService(service *registry.Service) *registry.Service {
// copy service
s := &registry.Service{}
*s = *service
// copy nodes
nodes := make([]*registry.Node, len(service.Nodes))
for j, node := range service.Nodes {
n := &registry.Node{}
*n = *node
nodes[j] = n
}
s.Nodes = nodes
// copy endpoints
eps := make([]*registry.Endpoint, len(service.Endpoints))
for j, ep := range service.Endpoints {
e := &registry.Endpoint{}
*e = *ep
eps[j] = e
}
s.Endpoints = eps
return s
}
2019-07-04 13:36:49 +03:00
// Copy makes a copy of services
func Copy(current []*registry.Service) []*registry.Service {
services := make([]*registry.Service, len(current))
for i, service := range current {
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
func Merge(olist []*registry.Service, nlist []*registry.Service) []*registry.Service {
var srv []*registry.Service
2019-02-01 12:05:03 +03:00
for _, n := range nlist {
2018-12-04 19:41:40 +03:00
var seen bool
for _, o := range olist {
if o.Version == n.Version {
sp := &registry.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
} else {
sp := &registry.Service{}
// make copy
*sp = *o
srv = append(srv, sp)
2018-12-04 19:41:40 +03:00
}
}
if !seen {
srv = append(srv, Copy([]*registry.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
func Remove(old, del []*registry.Service) []*registry.Service {
var services []*registry.Service
for _, o := range old {
srv := &registry.Service{}
*srv = *o
2018-12-04 19:41:40 +03:00
var rem bool
2018-12-04 19:41:40 +03:00
for _, s := range del {
if srv.Version == s.Version {
2019-07-04 13:36:49 +03:00
srv.Nodes = delNodes(srv.Nodes, s.Nodes)
if len(srv.Nodes) == 0 {
2018-12-04 19:41:40 +03:00
rem = true
}
}
}
2018-12-04 19:41:40 +03:00
if !rem {
services = append(services, srv)
2018-12-04 19:41:40 +03:00
}
}
2018-12-04 19:41:40 +03:00
return services
}