Hi there! New to libp2p, liking what I’ve been able to get done so far.
I’m trying to build a peer-to-peer application using the Gossip pub/sub implementation. While the basics seems to be working, the gossip implementation seems to be connecting to a lot of unrelated nodes before anything has been published or subscribed, and I’m trying to understand why.
After getting the Host
set up (details at end of this post), I create a local DHT client:
kademliaDHT, err := dht.New(*ctx, host, opts.Mode(dht.ModeAuto))
if err != nil {
panic(err)
}
if err = kademliaDHT.Bootstrap(*ctx); err != nil {
panic(err)
}
Then my node advertises itself and looks for peers:
routingDiscovery := discovery.NewRoutingDiscovery(kademliaDHT)
discovery.Advertise(*ctx, routingDiscovery, "my-application")
peerChan, err := routingDiscovery.FindPeers(*ctx, "my-application")
if err != nil {
panic(err)
}
for peer := range peerChan {
if peer.ID == host.ID() {
continue
}
logger.Info("Found peer:", peer)
}
So far that works as expected; I see the other peers I expect (1-2 nodes depending on what I’m running). Next, I set up the pubsub layer this way:
ps, err := pubsub.NewGossipSub(*ctx, host)
My node soon shows a lot of logs like this, with far more connections than known peers:
2020-06-12T19:03:40.828-0400 WARN pubsub go-libp2p-pubsub@v0.3.2/comm.go:74 opening new stream to peer: protocol not supportedQmUiEquVAe53THnGLJCGjb1Z4i4j8SqNNHSKYaHzPH4k1p
2020-06-12T19:03:41.175-0400 WARN pubsub go-libp2p-pubsub@v0.3.2/comm.go:74 opening new stream to peer: protocol not supportedQmNixNuseNf5LNbVkWpoen1kHi6EPGvDXtPij8tXUwSXgM
2020-06-12T19:03:41.769-0400 WARN pubsub go-libp2p-pubsub@v0.3.2/comm.go:74 opening new stream to peer: protocol not supportedQmXKVJ4Y3nXg5ZcnvH8kDcrRwX68ZFdayL29WZSHR8ap8e
2020-06-12T19:03:43.187-0400 WARN pubsub go-libp2p-pubsub@v0.3.2/comm.go:74 opening new stream to peer: protocol not supported12D3KooWPSw2pWRaRBE67TSdDzMeLTVrwXunjeBjCm9BiSefomyt
It seems like the gossip implementation is connecting to lots of other hosts, I guess discovered through the dht. For this application, which is bandwidth sensitive, I’d like to avoid connections to unrelated nodes (especially since many of them seem to be failing anyway). I’d love to understand this part of the protocol stack a bit better.
Question: How do I limit Gossip pubsub to interrogating/using only to those peers that have advertised themselves as part of my application?
thank you!
Appendix
Here’s how I set up the host on each peer:
host, err := libp2p.New(
context.Background(),
libp2p.ListenAddrs(listenAddr),
libp2p.Identity(*prvKey),
libp2p.Security(libp2ptls.ID, libp2ptls.New),
libp2p.DefaultTransports,
libp2p.ConnectionManager(connmgr.NewConnManager(
10, // Lowwater
50, // HighWater,
time.Minute, // GracePeriod
)),
libp2p.NATPortMap(),
libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
idht, err := dht.New(*ctx, h)
return idht, err
}),
libp2p.EnableAutoRelay(),
)