go-libp2p icon indicating copy to clipboard operation
go-libp2p copied to clipboard

peerstore: add new `GetPeersForProtocol` method to `Protobook`

Open synzhu opened this issue 4 years ago • 2 comments

The proposal is to add a new GetPeersForProtocol method to Protobook interface and implementations.

Currently, the protobook only maps from peers to protocols, and not the other way around. It's possible to simply iterate over all peers and filter them out by calling SupportsProtocols, but this could potentially become inefficient if we need to do this frequently and we have a large number of peers in the peerstore.

Alternatively, it is possible to listen for events (e.g EvtPeerProtocolsUpdated) and build the reverse mapping ourselves, but this is not 100% accurate as for example atm there is no event for when a protocol is removed. In fact, currently the Protobook data never gets purged at all, which is another issue entirely.

cc: @vyzo @Stebalien @marten-seemann

synzhu avatar Oct 22 '21 20:10 synzhu

This sounds like a reasonable addition.

Stebalien avatar Oct 22 '21 21:10 Stebalien

The implementation will need to take care to not create any (additional) mutex contention.

We use this trick to reduce it for peer IDs and we might need something similar for protocols: https://github.com/libp2p/go-libp2p-peerstore/blob/a1f426f3d046825d53b6d775edef4f854f976166/pstoremem/protobook.go#L11-L20

marten-seemann avatar Oct 22 '21 21:10 marten-seemann