From a412486c39ecb4de949297bda6bcdd89333fd7fa Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Thu, 4 Jul 2019 11:36:49 +0100 Subject: [PATCH] Update registry util semantics --- registry/cache/rcache.go | 4 +- registry/gossip/gossip.go | 8 +-- registry/memory/memory.go | 6 +- registry/util.go | 113 +++++++++++++++++++------------------- registry/util_test.go | 8 +-- 5 files changed, 71 insertions(+), 68 deletions(-) diff --git a/registry/cache/rcache.go b/registry/cache/rcache.go index cc5ca319..e2e0da35 100644 --- a/registry/cache/rcache.go +++ b/registry/cache/rcache.go @@ -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 diff --git a/registry/gossip/gossip.go b/registry/gossip/gossip.go index 508c9961..24006438 100644 --- a/registry/gossip/gossip.go +++ b/registry/gossip/gossip.go @@ -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 diff --git a/registry/memory/memory.go b/registry/memory/memory.go index a978e1ac..6c03df67 100644 --- a/registry/memory/memory.go +++ b/registry/memory/memory.go @@ -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 diff --git a/registry/util.go b/registry/util.go index 7954cb0d..5eb51a62 100644 --- a/registry/util.go +++ b/registry/util.go @@ -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 diff --git a/registry/util_test.go b/registry/util_test.go index 6e4dccd5..9d14c659 100644 --- a/registry/util_test.go +++ b/registry/util_test.go @@ -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) }