Skip to content

Commit

Permalink
Copies endpoint slices before any sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Reed committed Jan 22, 2015
1 parent 79a6bfb commit 38241c7
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions pkg/proxy/roundrobin.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,21 @@ func filterValidEndpoints(endpoints []string) []string {
return result
}

func endpointsAreEqual(left, right []string) bool {
if len(left) != len(right) {
return false
}

leftSorted := make([]string, len(left))
copy(leftSorted, left)
sort.Strings(leftSorted)
rightSorted := make([]string, len(right))
copy(rightSorted, right)
sort.Strings(rightSorted)

return reflect.DeepEqual(leftSorted, rightSorted)
}

func shuffleEndpoints(endpoints []string) []string {
shuffled := make([]string, len(endpoints))
perm := rand.Perm(len(endpoints))
Expand Down Expand Up @@ -221,11 +236,7 @@ func (lb *LoadBalancerRR) OnUpdate(endpoints []api.Endpoints) {
for _, endpoint := range endpoints {
existingEndpoints, exists := lb.endpointsMap[endpoint.Name]
validEndpoints := filterValidEndpoints(endpoint.Endpoints)
// Need to compare sorted endpoints here, since they are shuffled below
// before being put into endpointsMap
sort.Strings(existingEndpoints)
sort.Strings(validEndpoints)
if !exists || !reflect.DeepEqual(existingEndpoints, validEndpoints) {
if !exists || !endpointsAreEqual(existingEndpoints, validEndpoints) {
glog.V(3).Infof("LoadBalancerRR: Setting endpoints for %s to %+v", endpoint.Name, endpoint.Endpoints)
updateServiceDetailMap(lb, endpoint.Name, validEndpoints)
// On update can be called without NewService being called externally.
Expand Down

0 comments on commit 38241c7

Please sign in to comment.