depot/packages/networking/hyprspace/p2p/routing.go

37 lines
661 B
Go

package p2p
import (
"context"
"sync"
"github.com/libp2p/go-libp2p/core/peer"
routedhost "github.com/libp2p/go-libp2p/p2p/host/routed"
)
type ParallelRouting struct {
routings []routedhost.Routing
}
func (pr ParallelRouting) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
var wg sync.WaitGroup
var mutex sync.Mutex
var info peer.AddrInfo
info.ID = p
for _, r := range pr.routings {
wg.Add(1)
r2 := r
go func() {
defer wg.Done()
ai, err := r2.FindPeer(ctx, p)
if err == nil {
mutex.Lock()
defer mutex.Unlock()
info.Addrs = append(info.Addrs, ai.Addrs...)
}
}()
}
wg.Wait()
return info, nil
}