github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/internal/registry/registry.go (about) 1 package registry 2 3 import ( 4 "github.com/volts-dev/volts/registry" 5 ) 6 7 func addNodes(old, neu []*registry.Node) []*registry.Node { 8 nodes := make([]*registry.Node, len(neu)) 9 // add all new nodes 10 for i, n := range neu { 11 node := *n 12 nodes[i] = &node 13 } 14 15 // look at old nodes 16 for _, o := range old { 17 var exists bool 18 19 // check against new nodes 20 for _, n := range nodes { 21 // ids match then skip 22 if o.Id == n.Id { 23 exists = true 24 break 25 } 26 } 27 28 // keep old node 29 if !exists { 30 node := *o 31 nodes = append(nodes, &node) 32 } 33 } 34 35 return nodes 36 } 37 38 func delNodes(old, del []*registry.Node) []*registry.Node { 39 var nodes []*registry.Node 40 for _, o := range old { 41 var rem bool 42 for _, n := range del { 43 if o.Id == n.Id { 44 rem = true 45 break 46 } 47 } 48 if !rem { 49 nodes = append(nodes, o) 50 } 51 } 52 return nodes 53 } 54 55 // CopyService make a copy of service 56 func CopyService(service *registry.Service) *registry.Service { 57 // copy service 58 s := new(registry.Service) 59 *s = *service 60 61 // copy nodes 62 nodes := make([]*registry.Node, len(service.Nodes)) 63 for j, node := range service.Nodes { 64 n := new(registry.Node) 65 *n = *node 66 nodes[j] = n 67 } 68 s.Nodes = nodes 69 70 // copy endpoints 71 eps := make([]*registry.Endpoint, len(service.Endpoints)) 72 for j, ep := range service.Endpoints { 73 e := new(registry.Endpoint) 74 *e = *ep 75 eps[j] = e 76 } 77 s.Endpoints = eps 78 return s 79 } 80 81 // Copy makes a copy of services 82 func Copy(current []*registry.Service) []*registry.Service { 83 services := make([]*registry.Service, len(current)) 84 for i, service := range current { 85 services[i] = CopyService(service) 86 } 87 return services 88 } 89 90 // Merge merges two lists of services and returns a new copy 91 func Merge(olist []*registry.Service, nlist []*registry.Service) []*registry.Service { 92 var srv []*registry.Service 93 94 for _, n := range nlist { 95 var seen bool 96 for _, o := range olist { 97 if o.Version == n.Version { 98 sp := new(registry.Service) 99 // make copy 100 *sp = *o 101 // set nodes 102 sp.Nodes = addNodes(o.Nodes, n.Nodes) 103 104 // mark as seen 105 seen = true 106 srv = append(srv, sp) 107 break 108 } else { 109 sp := new(registry.Service) 110 // make copy 111 *sp = *o 112 srv = append(srv, sp) 113 } 114 } 115 if !seen { 116 srv = append(srv, Copy([]*registry.Service{n})...) 117 } 118 } 119 return srv 120 } 121 122 // Remove removes services and returns a new copy 123 func Remove(old, del []*registry.Service) []*registry.Service { 124 var services []*registry.Service 125 126 for _, o := range old { 127 srv := new(registry.Service) 128 *srv = *o 129 130 var rem bool 131 132 for _, s := range del { 133 if srv.Version == s.Version { 134 srv.Nodes = delNodes(srv.Nodes, s.Nodes) 135 136 if len(srv.Nodes) == 0 { 137 rem = true 138 } 139 } 140 } 141 142 if !rem { 143 services = append(services, srv) 144 } 145 } 146 147 return services 148 }