Skip to content

RUST-1464 Use async connection establishment when using async-std (2.3.x backport) #746

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/runtime/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,25 @@ impl AsyncTcpStream {

#[cfg(feature = "async-std-runtime")]
async fn try_connect(address: &SocketAddr, connect_timeout: Duration) -> Result<Self> {
use async_std::net::TcpStream;
use socket2::{Domain, Protocol, SockAddr, Socket, Type};
use std::convert::TryInto;

let domain = Domain::for_address(*address);
let socket = Socket::new(domain, Type::STREAM, Some(Protocol::TCP))?;
let conf = socket2::TcpKeepalive::new().with_time(KEEPALIVE_TIME);
socket.set_tcp_keepalive(&conf)?;
let stream_future = async_std::net::TcpStream::connect(address);

let address: SockAddr = (*address).into();
if connect_timeout == Duration::from_secs(0) {
socket.connect(&address)?;
let stream = if connect_timeout.is_zero() {
stream_future.await?
} else {
socket.connect_timeout(&address, connect_timeout)?;
}

let stream: TcpStream = std::net::TcpStream::from(socket).into();
runtime::timeout(connect_timeout, stream_future).await??
};
stream.set_nodelay(true)?;

let std_stream: std::net::TcpStream = stream.try_into()?;

let socket = socket2::Socket::from(std_stream);
let conf = socket2::TcpKeepalive::new().with_time(KEEPALIVE_TIME);
socket.set_tcp_keepalive(&conf)?;
let std_stream = std::net::TcpStream::from(socket);
let stream = async_std::net::TcpStream::from(std_stream);

Ok(stream.into())
}

Expand Down