Skip to content

Commit 567060b

Browse files
committed
chore(client) switch from net2 to socket2
net2 was recently deprecated; socket2 is the recommended alternative Closes #2205
1 parent ccd7ebd commit 567060b

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ want = "0.3"
3838

3939
# Optional
4040

41-
net2 = { version = "0.2.32", optional = true }
41+
socket2 = { version = "0.3", optional = true }
4242

4343
[dev-dependencies]
4444
futures-util = { version = "0.3", default-features = false, features = ["alloc"] }
@@ -65,7 +65,7 @@ runtime = [
6565
"tokio/rt-core",
6666
]
6767
tcp = [
68-
"net2",
68+
"socket2",
6969
"tokio/blocking",
7070
"tokio/tcp",
7171
"tokio/time",

src/client/connect/http.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use std::time::Duration;
1111

1212
use futures_util::future::Either;
1313
use http::uri::{Scheme, Uri};
14-
use net2::TcpBuilder;
1514
use pin_project::pin_project;
1615
use tokio::net::TcpStream;
1716
use tokio::time::Delay;
@@ -552,30 +551,32 @@ fn connect(
552551
reuse_address: bool,
553552
connect_timeout: Option<Duration>,
554553
) -> io::Result<impl Future<Output = io::Result<TcpStream>>> {
555-
let builder = match *addr {
556-
SocketAddr::V4(_) => TcpBuilder::new_v4()?,
557-
SocketAddr::V6(_) => TcpBuilder::new_v6()?,
554+
use socket2::{Domain, Protocol, Socket, Type};
555+
let domain = match *addr {
556+
SocketAddr::V4(_) => Domain::ipv4(),
557+
SocketAddr::V6(_) => Domain::ipv6(),
558558
};
559+
let socket = Socket::new(domain, Type::stream(), Some(Protocol::tcp()))?;
559560

560561
if reuse_address {
561-
builder.reuse_address(reuse_address)?;
562+
socket.set_reuse_address(true)?;
562563
}
563564

564565
if let Some(ref local_addr) = *local_addr {
565566
// Caller has requested this socket be bound before calling connect
566-
builder.bind(SocketAddr::new(local_addr.clone(), 0))?;
567+
socket.bind(&SocketAddr::new(local_addr.clone(), 0).into())?;
567568
} else if cfg!(windows) {
568569
// Windows requires a socket be bound before calling connect
569570
let any: SocketAddr = match *addr {
570571
SocketAddr::V4(_) => ([0, 0, 0, 0], 0).into(),
571572
SocketAddr::V6(_) => ([0, 0, 0, 0, 0, 0, 0, 0], 0).into(),
572573
};
573-
builder.bind(any)?;
574+
socket.bind(&any.into())?;
574575
}
575576

576577
let addr = *addr;
577578

578-
let std_tcp = builder.to_tcp_stream()?;
579+
let std_tcp = socket.into_tcp_stream();
579580

580581
Ok(async move {
581582
let connect = TcpStream::connect_std(std_tcp, &addr);

0 commit comments

Comments
 (0)