Merge pull request #800 from kordenlu/master
fix rcache node overwrited issue
This commit is contained in:
		| @@ -163,7 +163,7 @@ func (cw *consulWatcher) serviceHandler(idx uint64, data interface{}) { | |||||||
|  |  | ||||||
| 			// it's an update rather than creation | 			// it's an update rather than creation | ||||||
| 			if len(nodes) > 0 { | 			if len(nodes) > 0 { | ||||||
| 				delService := oldService | 				delService := registry.CopyService(oldService) | ||||||
| 				delService.Nodes = nodes | 				delService.Nodes = nodes | ||||||
| 				cw.next <- ®istry.Result{Action: "delete", Service: delService} | 				cw.next <- ®istry.Result{Action: "delete", Service: delService} | ||||||
| 			} | 			} | ||||||
|   | |||||||
| @@ -48,36 +48,38 @@ func delNodes(old, del []*Node) []*Node { | |||||||
| 	return nodes | 	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 | // Copy makes a copy of services | ||||||
| func Copy(current []*Service) []*Service { | func Copy(current []*Service) []*Service { | ||||||
| 	services := make([]*Service, len(current)) | 	services := make([]*Service, len(current)) | ||||||
| 	for i, service := range current { | 	for i, service := range current { | ||||||
| 		// copy service | 		services[i] = CopyService(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 |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return services | 	return services | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user