feat(swarm): set default for idle-connection-timeout to 10s
Description
With the move to a global idle-connection-timeout, connections are being closed much more aggressively. This causes problems in situations where, e.g. an application wants to use a connection shortly after an event has been emitted from the Swarm. With a default of 0 seconds, such a connection is instantly considered idle and therefore closed, despite the application wanting to use it again just moments later. Whilst it is possible to structure application code to mitigate this, it is unnecessarily complicated.
Additionally, connections being closed instantly if not in use is a foot-gun for newcomers to the library.
From a technical point-of-view, instantly closing idle connections is nice. In reality, it is an impractical default. Hence, we change this default to 10s.
10 seconds is considered to be an acceptable default as it strikes a balance between allowing some pause between network activity, yet frees up resources that are (supposedly) no longer needed.
Resolves: #4912.
Notes & open questions
Change checklist
- [ ] I have performed a self-review of my own code
- [ ] I have made corresponding changes to the documentation
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] A changelog entry has been made in the appropriate crates
I like this idea. Anyone coming to libp2p is likely going to use their connection for more than 0s, so giving them this default makes sense.
I'm not that familiar with what makes each connection or protocol idle vs what keeps them alive (we recently discusse Ping in https://github.com/libp2p/rust-libp2p/issues/4950 but there are more) -- so neither would other newcomers to libp2p. Could we potentially add this description to the documentation somewhere? It's like Thomas says, we don't need new users fighting against auto-closing connections when they might want to keep an open connection for their app. Another part of this is a note in the docs where applicable.
Thanks for starting this!
I'm not that familiar with what makes each connection or protocol idle vs what keeps them alive (we recently discusse Ping in #4950 but there are more) -- so neither would other newcomers to libp2p. Could we potentially add this description to the documentation somewhere?
The main place where this is currently documented is in https://docs.rs/libp2p/latest/libp2p/swarm/trait.ConnectionHandler.html#method.connection_keep_alive. I think users shouldn't need to know this, only implementers of protocols.
I like this idea. Anyone coming to libp2p is likely going to use their connection for more than 0s, so giving them this default makes sense.
There is some (I guess unintended) irony in this. We automatically keep connections alive while you use them, i.e. while there are active streams.
What this default does is bridge brief moments of inactivity between uses which would otherwise result in an immediate shutdown of the connection because it is idle.
What do you think of the idea voiced in #4912 to not close idle connections at all by default and make it the users responsibility?