Update registry util semantics

This commit is contained in:
Asim Aslam 2019-07-04 11:36:49 +01:00
parent 59a0e727e4
commit a412486c39
5 changed files with 71 additions and 68 deletions

View File

@ -97,7 +97,7 @@ func (c *cache) get(service string) ([]*registry.Service, error) {
// got services && within ttl so return cache
if c.isValid(services, ttl) {
// make a copy
cp := registry.CopyServices(services)
cp := registry.Copy(services)
// unlock the read
c.RUnlock()
// return servics
@ -114,7 +114,7 @@ func (c *cache) get(service string) ([]*registry.Service, error) {
// cache results
c.Lock()
c.set(service, registry.CopyServices(services))
c.set(service, registry.Copy(services))
c.Unlock()
return services, nil

View File

@ -626,7 +626,7 @@ func (g *gossipRegistry) run() {
g.services[u.Service.Name] = []*registry.Service{u.Service}
} else {
g.services[u.Service.Name] = registry.AddServices(service, []*registry.Service{u.Service})
g.services[u.Service.Name] = registry.Merge(service, []*registry.Service{u.Service})
}
g.Unlock()
@ -645,7 +645,7 @@ func (g *gossipRegistry) run() {
case actionTypeDelete:
g.Lock()
if service, ok := g.services[u.Service.Name]; ok {
if services := registry.DelServices(service, []*registry.Service{u.Service}); len(services) == 0 {
if services := registry.Remove(service, []*registry.Service{u.Service}); len(services) == 0 {
delete(g.services, u.Service.Name)
} else {
g.services[u.Service.Name] = services
@ -706,7 +706,7 @@ func (g *gossipRegistry) Register(s *registry.Service, opts ...registry.Register
if service, ok := g.services[s.Name]; !ok {
g.services[s.Name] = []*registry.Service{s}
} else {
g.services[s.Name] = registry.AddServices(service, []*registry.Service{s})
g.services[s.Name] = registry.Merge(service, []*registry.Service{s})
}
g.Unlock()
@ -754,7 +754,7 @@ func (g *gossipRegistry) Deregister(s *registry.Service) error {
g.Lock()
if service, ok := g.services[s.Name]; ok {
if services := registry.DelServices(service, []*registry.Service{s}); len(services) == 0 {
if services := registry.Remove(service, []*registry.Service{s}); len(services) == 0 {
delete(g.services, s.Name)
} else {
g.services[s.Name] = services

View File

@ -55,7 +55,7 @@ func (m *Registry) Init(opts ...registry.Option) error {
m.Lock()
for k, v := range getServices(m.options.Context) {
s := m.Services[k]
m.Services[k] = registry.AddServices(s, v)
m.Services[k] = registry.Merge(s, v)
}
m.Unlock()
return nil
@ -92,7 +92,7 @@ func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption
if service, ok := m.Services[s.Name]; !ok {
m.Services[s.Name] = []*registry.Service{s}
} else {
m.Services[s.Name] = registry.AddServices(service, []*registry.Service{s})
m.Services[s.Name] = registry.Merge(service, []*registry.Service{s})
}
m.Unlock()
@ -104,7 +104,7 @@ func (m *Registry) Deregister(s *registry.Service) error {
m.Lock()
if service, ok := m.Services[s.Name]; ok {
if service := registry.DelServices(service, []*registry.Service{s}); len(service) == 0 {
if service := registry.Remove(service, []*registry.Service{s}); len(service) == 0 {
delete(m.Services, s.Name)
} else {
m.Services[s.Name] = service

View File

@ -1,6 +1,56 @@
package registry
func CopyServices(current []*Service) []*Service {
func addNodes(old, neu []*Node) []*Node {
var nodes []*Node
// add all new nodes
for _, n := range neu {
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
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 {
var services []*Service
for _, service := range current {
@ -33,39 +83,8 @@ func CopyServices(current []*Service) []*Service {
return services
}
func addServiceNodes(old, neu []*Node) []*Node {
var nodes []*Node
// add all new nodes
for _, n := range neu {
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
if o.Id == n.Id {
exists = true
break
}
}
// keep old node
if !exists {
node := *o
nodes = append(nodes, &node)
}
}
return nodes
}
func AddServices(olist []*Service, nlist []*Service) []*Service {
// Merge merges two lists of services and returns a new copy
func Merge(olist []*Service, nlist []*Service) []*Service {
var srv []*Service
for _, n := range nlist {
@ -76,7 +95,7 @@ func AddServices(olist []*Service, nlist []*Service) []*Service {
// make copy
*sp = *o
// set nodes
sp.Nodes = addServiceNodes(o.Nodes, n.Nodes)
sp.Nodes = addNodes(o.Nodes, n.Nodes)
// mark as seen
seen = true
@ -90,30 +109,14 @@ func AddServices(olist []*Service, nlist []*Service) []*Service {
}
}
if !seen {
srv = append(srv, CopyServices([]*Service{n})...)
srv = append(srv, Copy([]*Service{n})...)
}
}
return srv
}
func delServiceNodes(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
}
func DelServices(old, del []*Service) []*Service {
// Remove removes services and returns a new copy
func Remove(old, del []*Service) []*Service {
var services []*Service
for _, o := range old {
@ -124,7 +127,7 @@ func DelServices(old, del []*Service) []*Service {
for _, s := range del {
if srv.Version == s.Version {
srv.Nodes = delServiceNodes(srv.Nodes, s.Nodes)
srv.Nodes = delNodes(srv.Nodes, s.Nodes)
if len(srv.Nodes) == 0 {
rem = true

View File

@ -4,7 +4,7 @@ import (
"testing"
)
func TestDelServices(t *testing.T) {
func TestRemove(t *testing.T) {
services := []*Service{
{
Name: "foo",
@ -30,14 +30,14 @@ func TestDelServices(t *testing.T) {
},
}
servs := DelServices([]*Service{services[0]}, []*Service{services[1]})
servs := Remove([]*Service{services[0]}, []*Service{services[1]})
if i := len(servs); i > 0 {
t.Errorf("Expected 0 nodes, got %d: %+v", i, servs)
}
t.Logf("Services %+v", servs)
}
func TestDelNodes(t *testing.T) {
func TestRemoveNodes(t *testing.T) {
services := []*Service{
{
Name: "foo",
@ -68,7 +68,7 @@ func TestDelNodes(t *testing.T) {
},
}
nodes := delServiceNodes(services[0].Nodes, services[1].Nodes)
nodes := delNodes(services[0].Nodes, services[1].Nodes)
if i := len(nodes); i != 1 {
t.Errorf("Expected only 1 node, got %d: %+v", i, nodes)
}