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"
|
2023-01-28 14:29:57 +02:00
|
|
|
"time"
|
2023-01-23 20:27:20 +02:00
|
|
|
|
|
|
|
"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
|
2023-01-28 14:29:57 +02:00
|
|
|
subCtx, cancelSubCtx := context.WithTimeout(ctx, 30*time.Second)
|
2023-01-28 02:23:31 +02:00
|
|
|
for _, r := range pr.routings {
|
|
|
|
wg.Add(1)
|
|
|
|
r2 := r
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2023-01-28 14:29:57 +02:00
|
|
|
ai, err := r2.FindPeer(subCtx, p)
|
2023-01-28 02:23:31 +02:00
|
|
|
if err == nil {
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
info.Addrs = append(info.Addrs, ai.Addrs...)
|
2023-01-28 14:29:57 +02:00
|
|
|
// give the other routings a short time period to find a better address
|
|
|
|
time.AfterFunc(500*time.Millisecond, cancelSubCtx)
|
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()
|
2023-01-28 14:29:57 +02:00
|
|
|
cancelSubCtx()
|
2023-01-28 02:23:31 +02:00
|
|
|
return info, nil
|
2023-01-23 20:27:20 +02:00
|
|
|
}
|