sort connected peers first

This commit is contained in:
Josh Deprez 2024-04-26 16:34:19 +10:00
parent 2b1ec2832e
commit 0dd647b14b
No known key found for this signature in database

19
main.go
View file

@ -236,7 +236,17 @@ func main() {
}
}()
slices.SortFunc(peerInfo, func(pa, pb *router.Peer) int {
return cmp.Compare(pa.ConfiguredAddr, pb.ConfiguredAddr)
return cmp.Or(
-cmp.Compare(
bool2Int(pa.ReceiverState() == router.ReceiverConnected),
bool2Int(pb.ReceiverState() == router.ReceiverConnected),
),
-cmp.Compare(
bool2Int(pa.SenderState() == router.SenderConnected),
bool2Int(pb.SenderState() == router.SenderConnected),
),
cmp.Compare(pa.ConfiguredAddr, pb.ConfiguredAddr),
)
})
return peerInfo, nil
})
@ -639,3 +649,10 @@ func (u udpAddr) toNet() *net.UDPAddr {
Port: int(u.port),
}
}
func bool2Int(b bool) int {
if b {
return 1
}
return 0
}