Skip to content

Commit

Permalink
Simplify for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
arjan-bal committed Oct 19, 2024
1 parent 6d8e920 commit 7b42222
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions balancer/pickfirst/pickfirstleaf/pickfirstleaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,24 +340,17 @@ func interleaveAddresses(addrs []resolver.Address) []resolver.Address {
}

interleavedAddrs := make([]resolver.Address, 0, len(addrs))
curTypeIndex := 0
for i := 0; i < len(addrs); i++ {

for curTypeIndex := 0; len(interleavedAddrs) < len(addrs); curTypeIndex = (curTypeIndex + 1) % len(interleavingOrder) {
// Some IP types may have fewer addresses than others, so we look for
// the next type that has a remaining member to add to the interleaved
// list.
for {
curType := interleavingOrder[curTypeIndex]
remainingMembers := familyAddrsMap[curType]
if len(remainingMembers) > 0 {
break
}
curTypeIndex = (curTypeIndex + 1) % len(interleavingOrder)
typ := interleavingOrder[curTypeIndex]
remainingMembers := familyAddrsMap[typ]
if len(remainingMembers) > 0 {
interleavedAddrs = append(interleavedAddrs, remainingMembers[0])
familyAddrsMap[typ] = remainingMembers[1:]
}
curType := interleavingOrder[curTypeIndex]
remainingMembers := familyAddrsMap[curType]
interleavedAddrs = append(interleavedAddrs, remainingMembers[0])
familyAddrsMap[curType] = remainingMembers[1:]
curTypeIndex = (curTypeIndex + 1) % len(interleavingOrder)
}

return interleavedAddrs
Expand Down

0 comments on commit 7b42222

Please sign in to comment.