2023-01-23 20:27:20 +02:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2023-01-28 02:23:31 +02:00
|
|
|
"context"
|
2023-01-23 20:27:20 +02:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2023-01-28 02:23:31 +02:00
|
|
|
routedhost "github.com/libp2p/go-libp2p/p2p/host/routed"
|
2023-01-23 20:27:20 +02:00
|
|
|
)
|
|
|
|
|
2023-01-28 02:23:31 +02:00
|
|
|
type ParallelRouting struct {
|
|
|
|
routings []routedhost.Routing
|
2023-01-23 20:27:20 +02:00
|
|
|
}
|
|
|
|
|
2023-01-28 02:23:31 +02:00
|
|
|
func (pr ParallelRouting) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
var mutex sync.Mutex
|
2023-01-23 20:27:20 +02:00
|
|
|
|
2023-01-28 02:23:31 +02:00
|
|
|
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...)
|
2023-01-23 20:27:20 +02:00
|
|
|
}
|
2023-01-28 02:23:31 +02:00
|
|
|
}()
|
2023-01-23 20:27:20 +02:00
|
|
|
}
|
|
|
|
|
2023-01-28 02:23:31 +02:00
|
|
|
wg.Wait()
|
|
|
|
return info, nil
|
2023-01-23 20:27:20 +02:00
|
|
|
}
|