libp2p / js-libp2p

The JavaScript Implementation of libp2p networking stack.
https://libp2p.io
Other
2.27k stars 436 forks source link

fix: prune connections based on stream counts and direction #2521

Closed achingbrain closed 2 months ago

achingbrain commented 2 months ago

Changes how we sort connections to prune to be more than just tags and age.

Now we'll sort by tags, the number of open streams, direction and age.

This should choose idle connections without streams to close over those that are in use.

Change checklist

achingbrain commented 2 months ago

would like to hear the rationale behind the changes.

If we do something like a DHT query, we open a connection to a peer, open a /ipfs/kad/1.0.0 stream, send a message and read the response. We do this repeatedly and eventually hit the max connections limit. The pruner tries to close some connections to bring us back under the limit while we're trying to continue the query.

The older connections are less useful now, since that peer plays no further part in the query. The more useful connections are the ones we're trying to open to continue the query - these are the newest connections so right now would be the ones that get closed by the pruner, almost immediately after being opened. Oops.

Instead, we should look for connections that have no open streams - these are safe(r) to close because no protocol is using them.

maschad commented 2 months ago

Thanks for the clarification