Skip to content

Commit

Permalink
Refactor: improve service compaction
Browse files Browse the repository at this point in the history
  • Loading branch information
betamos committed Aug 31, 2023
1 parent 3aa9678 commit 0f31727
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,26 @@ func mergeRecords(svcs ...*Service) *Service {

// Compacts a list of services, removes duplicates.
func compact(svcs []*Service) (comp []*Service) {
// Reverse iteration to populate the latest entry, in case of duplicates
// Populated as addrs are added
addrMap := make(map[netip.Addr]bool)

// Reverse iteration to populate the latest first
for i := len(svcs) - 1; i >= 0; i-- {
if !slices.ContainsFunc(comp, svcs[i].deepEqual) {
comp = append(comp, svcs[i])
svc := svcs[i]
var addrs []netip.Addr

// Only add if not already previously added
for _, addr := range svc.Addrs {
if !addrMap[addr] {
addrMap[addr] = true
addrs = append(addrs, addr)
}
}
// The last record is always added
if len(addrs) > 0 || i == len(svcs)-1 {
svc = template(svc)
svc.Addrs = addrs
comp = append(comp, svc)
}
}
slices.Reverse(comp) // Restore order
Expand Down

0 comments on commit 0f31727

Please sign in to comment.