From e0aa5d5bdd6a5fb889e576cea3ff8d302af8c990 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 28 Jan 2023 13:29:57 +0100 Subject: [PATCH] packages/hyprspace: cancel parallel routing shortly after finding first address --- packages/networking/hyprspace/p2p/routing.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/networking/hyprspace/p2p/routing.go b/packages/networking/hyprspace/p2p/routing.go index 7e3cea7..92367d4 100644 --- a/packages/networking/hyprspace/p2p/routing.go +++ b/packages/networking/hyprspace/p2p/routing.go @@ -3,6 +3,7 @@ package p2p import ( "context" "sync" + "time" "github.com/libp2p/go-libp2p/core/peer" routedhost "github.com/libp2p/go-libp2p/p2p/host/routed" @@ -18,20 +19,24 @@ func (pr ParallelRouting) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInf var info peer.AddrInfo info.ID = p + subCtx, cancelSubCtx := context.WithTimeout(ctx, 30*time.Second) for _, r := range pr.routings { wg.Add(1) r2 := r go func() { defer wg.Done() - ai, err := r2.FindPeer(ctx, p) + ai, err := r2.FindPeer(subCtx, p) if err == nil { mutex.Lock() defer mutex.Unlock() info.Addrs = append(info.Addrs, ai.Addrs...) + // give the other routings a short time period to find a better address + time.AfterFunc(500*time.Millisecond, cancelSubCtx) } }() } wg.Wait() + cancelSubCtx() return info, nil }