Skip to content

Commit 41f9957

Browse files
refactor(dependencies): update to socket2 v0.4.0 (#2472)
1 parent 48fdaf1 commit 41f9957

File tree

3 files changed

+9
-46
lines changed

3 files changed

+9
-46
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ want = "0.3"
4242
# Optional
4343

4444
libc = { version = "0.2", optional = true }
45-
socket2 = { version = "0.3.16", optional = true }
45+
socket2 = { version = "0.4", optional = true }
4646

4747
[dev-dependencies]
4848
futures-util = { version = "0.3", default-features = false, features = ["alloc"] }

src/client/connect/http.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -584,14 +584,11 @@ fn connect(
584584
// TODO(eliza): if Tokio's `TcpSocket` gains support for setting the
585585
// keepalive timeout, it would be nice to use that instead of socket2,
586586
// and avoid the unsafe `into_raw_fd`/`from_raw_fd` dance...
587-
use socket2::{Domain, Protocol, Socket, Type};
587+
use socket2::{Domain, Protocol, Socket, TcpKeepalive, Type};
588588
use std::convert::TryInto;
589589

590-
let domain = match *addr {
591-
SocketAddr::V4(_) => Domain::ipv4(),
592-
SocketAddr::V6(_) => Domain::ipv6(),
593-
};
594-
let socket = Socket::new(domain, Type::stream(), Some(Protocol::tcp()))
590+
let domain = Domain::for_address(*addr);
591+
let socket = Socket::new(domain, Type::STREAM, Some(Protocol::TCP))
595592
.map_err(ConnectError::m("tcp open error"))?;
596593

597594
// When constructing a Tokio `TcpSocket` from a raw fd/socket, the user is
@@ -601,7 +598,8 @@ fn connect(
601598
.map_err(ConnectError::m("tcp set_nonblocking error"))?;
602599

603600
if let Some(dur) = config.keep_alive_timeout {
604-
if let Err(e) = socket.set_keepalive(Some(dur)) {
601+
let conf = TcpKeepalive::new().with_time(dur);
602+
if let Err(e) = socket.set_tcp_keepalive(&conf) {
605603
warn!("tcp set_keepalive error: {}", e);
606604
}
607605
}

src/server/tcp.rs

+3-38
Original file line numberDiff line numberDiff line change
@@ -108,46 +108,11 @@ impl AddrIncoming {
108108
match ready!(self.listener.poll_accept(cx)) {
109109
Ok((socket, addr)) => {
110110
if let Some(dur) = self.tcp_keepalive_timeout {
111-
// Convert the Tokio `TcpStream` into a `socket2` socket
112-
// so we can call `set_keepalive`.
113-
// TODO(eliza): if Tokio's `TcpSocket` API grows a few
114-
// more methods in the future, hopefully we shouldn't
115-
// have to do the `from_raw_fd` dance any longer...
116-
#[cfg(unix)]
117-
let socket = unsafe {
118-
// Safety: `socket2`'s socket will try to close the
119-
// underlying fd when it's dropped. However, we
120-
// can't take ownership of the fd from the tokio
121-
// TcpStream, so instead we will call `into_raw_fd`
122-
// on the socket2 socket before dropping it. This
123-
// prevents it from trying to close the fd.
124-
use std::os::unix::io::{AsRawFd, FromRawFd};
125-
socket2::Socket::from_raw_fd(socket.as_raw_fd())
126-
};
127-
#[cfg(windows)]
128-
let socket = unsafe {
129-
// Safety: `socket2`'s socket will try to close the
130-
// underlying SOCKET when it's dropped. However, we
131-
// can't take ownership of the SOCKET from the tokio
132-
// TcpStream, so instead we will call `into_raw_socket`
133-
// on the socket2 socket before dropping it. This
134-
// prevents it from trying to close the SOCKET.
135-
use std::os::windows::io::{AsRawSocket, FromRawSocket};
136-
socket2::Socket::from_raw_socket(socket.as_raw_socket())
137-
};
138-
139-
// Actually set the TCP keepalive timeout.
140-
if let Err(e) = socket.set_keepalive(Some(dur)) {
111+
let socket = socket2::SockRef::from(&socket);
112+
let conf = socket2::TcpKeepalive::new().with_time(dur);
113+
if let Err(e) = socket.set_tcp_keepalive(&conf) {
141114
trace!("error trying to set TCP keepalive: {}", e);
142115
}
143-
144-
// Take ownershop of the fd/socket back from the socket2
145-
// `Socket`, so that socket2 doesn't try to close it
146-
// when it's dropped.
147-
#[cfg(unix)]
148-
drop(std::os::unix::io::IntoRawFd::into_raw_fd(socket));
149-
#[cfg(windows)]
150-
drop(std::os::windows::io::IntoRawSocket::into_raw_socket(socket));
151116
}
152117
if let Err(e) = socket.set_nodelay(self.tcp_nodelay) {
153118
trace!("error trying to set TCP nodelay: {}", e);

0 commit comments

Comments
 (0)