Use non-blocking connect for TcpStream. #687
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Instead of spawning a background thread which is unaware of any timeouts but continues to run until the TCP stack decides that the remote is not reachable we use mio's non-blocking connect.
mio's
TcpStream::connect
returns immediately but the actual connection is usually just in progress and we have to be sure the socket is writeable before we can consider the connection as established.TheWatcher
andReactor
APIs have changed a bit to (a) allow registration together with an initial set ofWaker
s in order to avoid missing any wakeups that may occur before we even registered aWaker
withWatcher::{poll_read_with, poll_write_with}
, and (b) to allow registration with readiness interests other thanPollOpt::all()
.Update: Following a suggestion of @stjepang we offer methods to check for read/write readiness of a
Watcher
instead of the approach in the previous paragraph to accept a set ofWaker
s when registering an event source. The changes relative to master are smaller and both methods look more useful in other contexts. Also the code is more robust w.r.t. wakeups of theWaker
from clones outside theReactor
.I am not sure if we need to add protection mechanisms against spurious wakeups from mio. Currently we treat the
Poll::Ready(())
fromWatcher::poll_write_ready
as proof that the non-blocking connect has finished, but if the event from mio was a spurious one, it might still be ongoing.Context: #678.