From 751db88ae4e4e1d492cc4956cc36df4370c758ac Mon Sep 17 00:00:00 2001 From: lbl <1791778603@qq.com> Date: Fri, 4 Sep 2026 22:32:29 +0800 Subject: [PATCH] fix(p2p): limit concurrent punch tasks --- .../src/tunnel_core/p2p/transport/punch.rs | 84 ++++++++++++++++++- 1 file changed, 80 insertions(+), 4 deletions(-) diff --git a/vnt-core/src/tunnel_core/p2p/transport/punch.rs b/vnt-core/src/tunnel_core/p2p/transport/punch.rs index e54c0f0..c7f34d9 100644 --- a/vnt-core/src/tunnel_core/p2p/transport/punch.rs +++ b/vnt-core/src/tunnel_core/p2p/transport/punch.rs @@ -14,6 +14,28 @@ use rustp2p_core::punch::{PunchModel, Puncher}; use std::net::Ipv4Addr; use std::sync::Arc; use std::time::Duration; +use tokio::sync::{OwnedSemaphorePermit, Semaphore}; + +const MAX_CONCURRENT_PUNCHES: usize = 4; + +#[derive(Clone)] +struct PunchLimiter { + semaphore: Arc, +} + +impl Default for PunchLimiter { + fn default() -> Self { + Self { + semaphore: Arc::new(Semaphore::new(MAX_CONCURRENT_PUNCHES)), + } + } +} + +impl PunchLimiter { + fn try_acquire(&self) -> Option { + self.semaphore.clone().try_acquire_owned().ok() + } +} pub struct PunchTaskContext { pub network: SharedNetworkAddr, @@ -80,6 +102,7 @@ pub struct NatPuncher { punch_backoff: PunchBackoff, puncher: Option, packet_crypto: PacketCrypto, + limiter: PunchLimiter, } impl NatPuncher { @@ -94,22 +117,33 @@ impl NatPuncher { punch_backoff, puncher, packet_crypto, + limiter: PunchLimiter::default(), } } pub fn punch(&self, dest_ip: Ipv4Addr, punch_info: PunchInfo) -> anyhow::Result { - if self.puncher.is_none() { + let Some(puncher) = self.puncher.clone() else { return Ok(false); - } + }; + let Some(permit) = self.limiter.try_acquire() else { + log::debug!("skip punch to {dest_ip}: concurrent punch limit reached"); + return Ok(false); + }; if !self.punch_backoff.try_begin(dest_ip) { return Ok(false); } - self.punch_uncheck_delay(dest_ip, punch_info, Some(Duration::from_millis(50)))?; + self.spawn_punch( + puncher, + dest_ip, + punch_info, + Some(Duration::from_millis(50)), + permit, + )?; Ok(true) } pub fn punch_uncheck(&self, dest_ip: Ipv4Addr, punch_info: PunchInfo) -> anyhow::Result<()> { self.punch_uncheck_delay(dest_ip, punch_info, None) } - pub fn punch_uncheck_delay( + fn punch_uncheck_delay( &self, dest_ip: Ipv4Addr, punch_info: PunchInfo, @@ -118,11 +152,26 @@ impl NatPuncher { let Some(puncher) = self.puncher.clone() else { return Ok(()); }; + let Some(permit) = self.limiter.try_acquire() else { + log::debug!("skip punch to {dest_ip}: concurrent punch limit reached"); + return Ok(()); + }; + self.spawn_punch(puncher, dest_ip, punch_info, time, permit) + } + fn spawn_punch( + &self, + puncher: Puncher, + dest_ip: Ipv4Addr, + punch_info: PunchInfo, + time: Option, + permit: OwnedSemaphorePermit, + ) -> anyhow::Result<()> { let Some(src_ip) = self.network.ip() else { bail!("not ip"); }; let packet_crypto = self.packet_crypto.clone(); tokio::spawn(async move { + let _permit = permit; if let Some(time) = time { tokio::time::sleep(time).await; } @@ -157,3 +206,30 @@ async fn punch_now( .await?; Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn punch_limiter_is_shared_and_releases_capacity() { + let limiter = PunchLimiter::default(); + let clone = limiter.clone(); + let mut permits = Vec::new(); + + for _ in 0..MAX_CONCURRENT_PUNCHES { + permits.push(limiter.try_acquire().expect("前四个任务应获得许可")); + } + assert!( + clone.try_acquire().is_none(), + "克隆必须共享同一个全局并发上限" + ); + + permits.pop(); + + assert!( + clone.try_acquire().is_some(), + "任务结束释放许可后应能立即开始下一轮" + ); + } +}