From 866631df1d27167f6c77e8fa0bac668868cc7295 Mon Sep 17 00:00:00 2001 From: lubaoquan Date: Mon, 30 Sep 2019 10:36:43 +0800 Subject: [PATCH] fixes https://github.com/micro/go-micro/issues/793 --- registry/consul/watcher.go | 2 +- registry/util.go | 52 ++++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/registry/consul/watcher.go b/registry/consul/watcher.go index 031d02b7..62829136 100644 --- a/registry/consul/watcher.go +++ b/registry/consul/watcher.go @@ -163,7 +163,7 @@ func (cw *consulWatcher) serviceHandler(idx uint64, data interface{}) { // it's an update rather than creation if len(nodes) > 0 { - delService := oldService + delService := registry.CopyService(oldService) delService.Nodes = nodes cw.next <- ®istry.Result{Action: "delete", Service: delService} } diff --git a/registry/util.go b/registry/util.go index 5cd04eb9..729d3ca8 100644 --- a/registry/util.go +++ b/registry/util.go @@ -48,36 +48,38 @@ func delNodes(old, del []*Node) []*Node { return nodes } +// CopyService make a copy of service +func CopyService(service *Service) *Service { + // copy service + s := new(Service) + *s = *service + + // copy nodes + nodes := make([]*Node, len(service.Nodes)) + for j, node := range service.Nodes { + n := new(Node) + *n = *node + nodes[j] = n + } + s.Nodes = nodes + + // copy endpoints + eps := make([]*Endpoint, len(service.Endpoints)) + for j, ep := range service.Endpoints { + e := new(Endpoint) + *e = *ep + eps[j] = e + } + s.Endpoints = eps + return s +} + // Copy makes a copy of services func Copy(current []*Service) []*Service { services := make([]*Service, len(current)) for i, service := range current { - // copy service - s := new(Service) - *s = *service - - // copy nodes - nodes := make([]*Node, len(service.Nodes)) - for j, node := range service.Nodes { - n := new(Node) - *n = *node - nodes[j] = n - } - s.Nodes = nodes - - // copy endpoints - eps := make([]*Endpoint, len(service.Endpoints)) - for j, ep := range service.Endpoints { - e := new(Endpoint) - *e = *ep - eps[j] = e - } - s.Endpoints = eps - - // append service - services[i] = s + services[i] = CopyService(service) } - return services }