packages/hyprspace: cancel parallel routing shortly after finding first address

This commit is contained in:
Max Headroom 2023-01-28 13:29:57 +01:00
parent 92f1e8b71d
commit e0aa5d5bdd

View file

@ -3,6 +3,7 @@ package p2p
import ( import (
"context" "context"
"sync" "sync"
"time"
"github.com/libp2p/go-libp2p/core/peer" "github.com/libp2p/go-libp2p/core/peer"
routedhost "github.com/libp2p/go-libp2p/p2p/host/routed" 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 var info peer.AddrInfo
info.ID = p info.ID = p
subCtx, cancelSubCtx := context.WithTimeout(ctx, 30*time.Second)
for _, r := range pr.routings { for _, r := range pr.routings {
wg.Add(1) wg.Add(1)
r2 := r r2 := r
go func() { go func() {
defer wg.Done() defer wg.Done()
ai, err := r2.FindPeer(ctx, p) ai, err := r2.FindPeer(subCtx, p)
if err == nil { if err == nil {
mutex.Lock() mutex.Lock()
defer mutex.Unlock() defer mutex.Unlock()
info.Addrs = append(info.Addrs, ai.Addrs...) 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() wg.Wait()
cancelSubCtx()
return info, nil return info, nil
} }