From afff49e61acd0316059606e30256787855eb919f Mon Sep 17 00:00:00 2001 From: lbl8603 <49143209+lbl8603@users.noreply.github.com> Date: Tue, 6 Aug 2024 22:03:18 +0800 Subject: [PATCH] set_reuse_port --- vnt/src/channel/mod.rs | 4 +++- vnt/src/channel/socket/mod.rs | 2 ++ vnt/src/channel/tcp_channel.rs | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/vnt/src/channel/mod.rs b/vnt/src/channel/mod.rs index 19b1525..6fec685 100644 --- a/vnt/src/channel/mod.rs +++ b/vnt/src/channel/mod.rs @@ -262,7 +262,9 @@ pub(crate) fn init_context( let socket = socket2::Socket::new(socket2::Domain::IPV4, socket2::Type::STREAM, None)?; (socket, address) }; - let _ = socket.set_reuse_address(true); + socket.set_reuse_address(true)?; + #[cfg(unix)] + socket.set_reuse_port(true)?; if let Err(e) = socket.bind(&address.into()) { if ports[0] == 0 { //端口可能冲突,则使用任意端口 diff --git a/vnt/src/channel/socket/mod.rs b/vnt/src/channel/socket/mod.rs index 338702d..87abb14 100644 --- a/vnt/src/channel/socket/mod.rs +++ b/vnt/src/channel/socket/mod.rs @@ -62,6 +62,8 @@ pub fn create_tcp0( } if bind_port != 0 { socket.set_reuse_address(true)?; + #[cfg(unix)] + socket.set_reuse_port(true)?; if v4 { let addr: SocketAddr = format!("0.0.0.0:{}", bind_port).parse().unwrap(); socket.bind(&addr.into())?; diff --git a/vnt/src/channel/tcp_channel.rs b/vnt/src/channel/tcp_channel.rs index 679616c..2e8e163 100644 --- a/vnt/src/channel/tcp_channel.rs +++ b/vnt/src/channel/tcp_channel.rs @@ -83,7 +83,7 @@ async fn connect_tcp_handle( }; tokio::spawn(async move { if let Err(e) = connect_tcp0(data, addr, recv_handler, context, bind_port).await { - log::warn!("发送失败,链接终止:{:?},{:?}", addr, e); + log::warn!("连接失败,链接终止:{:?},{:?}", addr, e); } }); }