ipn/ipnstate: address TODO about garbage during peer sorting

Updates #cleanup

Change-Id: I34938bca70a95571cc62ce1f76eaab5db8c2c3ef
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/9323/head
Brad Fitzpatrick 9 months ago committed by Brad Fitzpatrick
parent 60e9bd6047
commit d506a55c8a

@ -12,6 +12,7 @@ import (
"io" "io"
"log" "log"
"net/netip" "net/netip"
"slices"
"sort" "sort"
"strings" "strings"
"sync" "sync"
@ -680,25 +681,29 @@ func (pr *PingResult) ToPingResponse(pingType tailcfg.PingType) *tailcfg.PingRes
} }
} }
// SortPeers sorts peers by either the DNS name, hostname, Tailscale IP, // SortPeers sorts peers by either their DNS name, hostname, Tailscale IP,
// or current public key. // or ultimately their current public key.
func SortPeers(peers []*PeerStatus) { func SortPeers(peers []*PeerStatus) {
sort.Slice(peers, func(i, j int) bool { return sortKey(peers[i]) < sortKey(peers[j]) }) slices.SortStableFunc(peers, (*PeerStatus).compare)
} }
func sortKey(ps *PeerStatus) string { func (a *PeerStatus) compare(b *PeerStatus) int {
if ps.DNSName != "" { if a.DNSName != "" || b.DNSName != "" {
return ps.DNSName if v := strings.Compare(a.DNSName, b.DNSName); v != 0 {
return v
}
} }
if ps.HostName != "" { if a.HostName != "" || b.HostName != "" {
return ps.HostName if v := strings.Compare(a.HostName, b.HostName); v != 0 {
return v
}
} }
// TODO(bradfitz): add PeerStatus.Less and avoid these allocs in a Less func. if len(a.TailscaleIPs) > 0 && len(b.TailscaleIPs) > 0 {
if len(ps.TailscaleIPs) > 0 { if v := a.TailscaleIPs[0].Compare(b.TailscaleIPs[0]); v != 0 {
return ps.TailscaleIPs[0].String() return v
}
} }
raw := ps.PublicKey.Raw32() return a.PublicKey.Compare(b.PublicKey)
return string(raw[:])
} }
// DebugDERPRegionReport is the result of a "tailscale debug derp" command, // DebugDERPRegionReport is the result of a "tailscale debug derp" command,

@ -168,6 +168,12 @@ func (p NodePublic) Shard() uint8 {
return s ^ uint8(p.k[2]+p.k[12]) return s ^ uint8(p.k[2]+p.k[12])
} }
// Compare returns -1, 0, or 1, depending on whether p orders before p2,
// using bytes.Compare on the bytes of the public key.
func (p NodePublic) Compare(p2 NodePublic) int {
return bytes.Compare(p.k[:], p2.k[:])
}
// ParseNodePublicUntyped parses an untyped 64-character hex value // ParseNodePublicUntyped parses an untyped 64-character hex value
// as a NodePublic. // as a NodePublic.
// //

Loading…
Cancel
Save