From 0dd647b14b72d2df7198ddf809d84e42fbaa8ef5 Mon Sep 17 00:00:00 2001 From: Josh Deprez Date: Fri, 26 Apr 2024 16:34:19 +1000 Subject: [PATCH] sort connected peers first --- main.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index a59121f..cb69358 100644 --- a/main.go +++ b/main.go @@ -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 +}