diff --git a/client.go b/client.go index 303b06f..b5d76ae 100644 --- a/client.go +++ b/client.go @@ -37,7 +37,7 @@ type RendezvousClient interface { DiscoverAsync(ctx context.Context, ns string) (<-chan peer.AddrInfo, error) } -func NewRendezvousPoint(host host.Host, p peer.ID) RendezvousPoint { +func NewRendezvousPoint(host host.Host, p []peer.ID) RendezvousPoint { return &rendezvousPoint{ host: host, p: p, @@ -46,10 +46,10 @@ func NewRendezvousPoint(host host.Host, p peer.ID) RendezvousPoint { type rendezvousPoint struct { host host.Host - p peer.ID + p []peer.ID } -func NewRendezvousClient(host host.Host, rp peer.ID) RendezvousClient { +func NewRendezvousClient(host host.Host, rp []peer.ID) RendezvousClient { return NewRendezvousClientWithPoint(NewRendezvousPoint(host, rp)) } @@ -61,8 +61,12 @@ type rendezvousClient struct { rp RendezvousPoint } +func (r *rendezvousPoint) getRandomPeer() peer.ID { + return r.p[rand.Intn(len(r.p))] // nolint: gosec +} + func (rp *rendezvousPoint) Register(ctx context.Context, ns string, ttl int) (time.Duration, error) { - s, err := rp.host.NewStream(ctx, rp.p, RendezvousProto) + s, err := rp.host.NewStream(ctx, rp.getRandomPeer(), RendezvousProto) if err != nil { return 0, err } @@ -143,7 +147,7 @@ func registerRefresh(ctx context.Context, rz RendezvousPoint, ns string, ttl int } func (rp *rendezvousPoint) Discover(ctx context.Context, ns string, limit int, cookie []byte) ([]Registration, []byte, error) { - s, err := rp.host.NewStream(ctx, rp.p, RendezvousProto) + s, err := rp.host.NewStream(ctx, rp.getRandomPeer(), RendezvousProto) if err != nil { return nil, nil, err } @@ -192,7 +196,7 @@ func discoverQuery(ns string, limit int, cookie []byte, r ggio.Reader, w ggio.Wr } func (rp *rendezvousPoint) DiscoverAsync(ctx context.Context, ns string) (<-chan Registration, error) { - s, err := rp.host.NewStream(ctx, rp.p, RendezvousProto) + s, err := rp.host.NewStream(ctx, rp.getRandomPeer(), RendezvousProto) if err != nil { return nil, err } diff --git a/discovery.go b/discovery.go index dae1565..3b45dc6 100644 --- a/discovery.go +++ b/discovery.go @@ -10,6 +10,8 @@ import ( "github.com/libp2p/go-libp2p-core/discovery" "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/peer" + + "github.com/multiformats/go-multiaddr" ) type rendezvousDiscovery struct { @@ -31,8 +33,8 @@ type record struct { expire int64 } -func NewRendezvousDiscovery(host host.Host, rendezvousPeer peer.ID) discovery.Discovery { - rp := NewRendezvousPoint(host, rendezvousPeer) +func NewRendezvousDiscovery(host host.Host, rendezvousPeers []multiaddr.Multiaddr) discovery.Discovery { + rp := NewRendezvousPoint(host, rendezvousPeers) return &rendezvousDiscovery{rp: rp, peerCache: make(map[string]*discoveryCache), rng: rand.New(rand.NewSource(rand.Int63()))} }