-
Notifications
You must be signed in to change notification settings - Fork 957
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use tk_listen to absorb errors on the TcpListener #402
Conversation
tcp-transport/src/lib.rs
Outdated
@@ -108,6 +111,8 @@ impl Transport for TcpConfig { | |||
.map(|listener| { | |||
// Pull out a stream of sockets for incoming connections | |||
listener.incoming() | |||
.sleep_on_error(Duration::from_secs(1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The examples all use 100ms, why did you opt for waiting a full second on the connection that errored?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this should be in the TcpConfig
.
.map_err(|err| { | ||
debug!("Error in TCP listener: {:?}", err); | ||
err | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While on it, why not also add the .listen(MAX_SIMULTANEOUS_CONNECTIONS)
from the example - we could even make that configurable, putting the TcpConfig
to good use finally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What listen
does is handled by the swarm
, although the swarm doesn't have a limit to the maximum number of connections at the moment and should have one.
The `sleep_on_error` mechanism in `libp2p-tcp` would delay the next poll on the listener stream when an error happens. This mechanism was introduced in libp2p#402 based on the [`tk_listen`](https://docs.rs/tk-listen/latest/tk_listen/) crate also referenced in the tokio documentation. When running out of file descriptors, the listening socket would return an `EMFILE`. Instead of polling the socket again, thus likely receiving another `EMFILE`, one would instead wait for 100ms. Modern operating systems should run with high file descriptor limits and thus this error should not happen in the wild. In addition, delaying the next poll only covers up the issue, but does not solve it. With the above in mind, this pull request removes the daly. (The mention of `tk_listen` has since been removed from tokio.)
The documentation of TcpListener suggests doing what this PR does.
The
TcpListener
can sometimes produce an error which would just close the listener.I think handling this directly in the TCP transport is the right location. There might be valid reasons why a listener produces an error, therefore the upper layers should continue to drop the listener if an error happens.