mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-05-10 16:05:37 +00:00
refactor: remove ScopedTask (#2125)
* replace ScopedTask with AbortOnDropHandle
This commit is contained in:
@@ -6,9 +6,10 @@ use std::{
|
||||
|
||||
use anyhow::Context;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio_util::task::AbortOnDropHandle;
|
||||
|
||||
use crate::{
|
||||
common::{PeerId, scoped_task::ScopedTask, stun::StunInfoCollectorTrait},
|
||||
common::{PeerId, stun::StunInfoCollectorTrait},
|
||||
connector::udp_hole_punch::common::{
|
||||
HOLE_PUNCH_PACKET_BODY_LEN, UdpHolePunchListener, try_connect_with_socket,
|
||||
},
|
||||
@@ -32,7 +33,7 @@ const REMOTE_WAIT_TIME_MS: u64 = 5000;
|
||||
|
||||
pub(crate) struct PunchBothEasySymHoleServer {
|
||||
common: Arc<PunchHoleServerCommon>,
|
||||
task: Mutex<Option<ScopedTask<()>>>,
|
||||
task: Mutex<Option<AbortOnDropHandle<()>>>,
|
||||
}
|
||||
|
||||
impl PunchBothEasySymHoleServer {
|
||||
@@ -161,7 +162,7 @@ impl PunchBothEasySymHoleServer {
|
||||
}
|
||||
});
|
||||
|
||||
*locked_task = Some(task.into());
|
||||
*locked_task = Some(AbortOnDropHandle::new(task));
|
||||
return Ok(SendPunchPacketBothEasySymResponse {
|
||||
is_busy: false,
|
||||
base_mapped_addr: Some(cur_mapped_addr.into()),
|
||||
|
||||
@@ -5,9 +5,10 @@ use std::{
|
||||
|
||||
use anyhow::Context;
|
||||
use tokio::net::UdpSocket;
|
||||
use tokio_util::task::AbortOnDropHandle;
|
||||
|
||||
use crate::{
|
||||
common::{PeerId, scoped_task::ScopedTask, upnp},
|
||||
common::{PeerId, upnp},
|
||||
connector::udp_hole_punch::common::{
|
||||
HOLE_PUNCH_PACKET_BODY_LEN, UdpSocketArray, try_connect_with_socket,
|
||||
},
|
||||
@@ -178,7 +179,7 @@ impl PunchConeHoleClient {
|
||||
|
||||
send_from_local().await?;
|
||||
|
||||
let scoped_punch_task: ScopedTask<()> = tokio::spawn(async move {
|
||||
let punch_task = AbortOnDropHandle::new(tokio::spawn(async move {
|
||||
if let Err(e) = rpc_stub
|
||||
.send_punch_packet_cone(
|
||||
BaseController {
|
||||
@@ -198,8 +199,7 @@ impl PunchConeHoleClient {
|
||||
{
|
||||
tracing::error!(?e, "failed to call remote send punch packet");
|
||||
}
|
||||
})
|
||||
.into();
|
||||
}));
|
||||
|
||||
// server: will send some punching resps, total 10 packets.
|
||||
// client: use the socket to create UdpTunnel with UdpTunnelConnector
|
||||
@@ -208,7 +208,7 @@ impl PunchConeHoleClient {
|
||||
while finish_time.is_none() || finish_time.as_ref().unwrap().elapsed().as_millis() < 1000 {
|
||||
tokio::time::sleep(Duration::from_millis(200)).await;
|
||||
|
||||
if finish_time.is_none() && (*scoped_punch_task).is_finished() {
|
||||
if finish_time.is_none() && punch_task.is_finished() {
|
||||
finish_time = Some(Instant::now());
|
||||
}
|
||||
|
||||
|
||||
@@ -11,12 +11,11 @@ use std::{
|
||||
use anyhow::Context;
|
||||
use rand::{Rng, seq::SliceRandom};
|
||||
use tokio::{net::UdpSocket, sync::RwLock};
|
||||
use tokio_util::task::AbortOnDropHandle;
|
||||
use tracing::Level;
|
||||
|
||||
use crate::{
|
||||
common::{
|
||||
PeerId, global_ctx::ArcGlobalCtx, scoped_task::ScopedTask, stun::StunInfoCollectorTrait,
|
||||
},
|
||||
common::{PeerId, global_ctx::ArcGlobalCtx, stun::StunInfoCollectorTrait},
|
||||
connector::udp_hole_punch::{
|
||||
common::{
|
||||
HOLE_PUNCH_PACKET_BODY_LEN, send_symmetric_hole_punch_packet, try_connect_with_socket,
|
||||
@@ -360,7 +359,7 @@ impl PunchSymToConeHoleClient {
|
||||
packet: &[u8],
|
||||
tid: u32,
|
||||
remote_mapped_addr: crate::proto::common::SocketAddr,
|
||||
scoped_punch_task: &ScopedTask<T>,
|
||||
punch_task: &AbortOnDropHandle<T>,
|
||||
) -> Result<Option<Box<dyn Tunnel>>, anyhow::Error> {
|
||||
// no matter what the result is, we should check if we received any hole punching packet
|
||||
let mut ret_tunnel: Option<Box<dyn Tunnel>> = None;
|
||||
@@ -372,7 +371,7 @@ impl PunchSymToConeHoleClient {
|
||||
|
||||
tokio::time::sleep(Duration::from_millis(200)).await;
|
||||
|
||||
if finish_time.is_none() && (*scoped_punch_task).is_finished() {
|
||||
if finish_time.is_none() && punch_task.is_finished() {
|
||||
finish_time = Some(Instant::now());
|
||||
}
|
||||
|
||||
@@ -482,27 +481,27 @@ impl PunchSymToConeHoleClient {
|
||||
|
||||
if self.punch_predicablely.load(Ordering::Relaxed) && base_port_for_easy_sym.is_some() {
|
||||
let rpc_stub = self.get_rpc_stub(dst_peer_id).await;
|
||||
let scoped_punch_task: ScopedTask<()> =
|
||||
tokio::spawn(Self::remote_send_hole_punch_packet_predicable(
|
||||
let punch_task = AbortOnDropHandle::new(tokio::spawn(
|
||||
Self::remote_send_hole_punch_packet_predicable(
|
||||
rpc_stub,
|
||||
base_port_for_easy_sym,
|
||||
my_nat_info,
|
||||
remote_mapped_addr,
|
||||
public_ips.clone(),
|
||||
tid,
|
||||
))
|
||||
.into();
|
||||
),
|
||||
));
|
||||
let ret_tunnel = Self::check_hole_punch_result(
|
||||
global_ctx.clone(),
|
||||
&udp_array,
|
||||
&packet,
|
||||
tid,
|
||||
remote_mapped_addr,
|
||||
&scoped_punch_task,
|
||||
&punch_task,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let task_ret = scoped_punch_task.await;
|
||||
let task_ret = punch_task.await;
|
||||
tracing::debug!(?ret_tunnel, ?task_ret, "predictable punch task got result");
|
||||
if let Some(tunnel) = ret_tunnel {
|
||||
return Ok(Some(tunnel));
|
||||
@@ -510,27 +509,26 @@ impl PunchSymToConeHoleClient {
|
||||
}
|
||||
|
||||
let rpc_stub = self.get_rpc_stub(dst_peer_id).await;
|
||||
let scoped_punch_task: ScopedTask<Option<u32>> =
|
||||
tokio::spawn(Self::remote_send_hole_punch_packet_random(
|
||||
let punch_task =
|
||||
AbortOnDropHandle::new(tokio::spawn(Self::remote_send_hole_punch_packet_random(
|
||||
rpc_stub,
|
||||
remote_mapped_addr,
|
||||
public_ips.clone(),
|
||||
tid,
|
||||
round,
|
||||
port_index,
|
||||
))
|
||||
.into();
|
||||
)));
|
||||
let ret_tunnel = Self::check_hole_punch_result(
|
||||
global_ctx,
|
||||
&udp_array,
|
||||
&packet,
|
||||
tid,
|
||||
remote_mapped_addr,
|
||||
&scoped_punch_task,
|
||||
&punch_task,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let punch_task_result = scoped_punch_task.await;
|
||||
let punch_task_result = punch_task.await;
|
||||
tracing::debug!(?punch_task_result, ?ret_tunnel, "punch task got result");
|
||||
|
||||
if let Ok(Some(next_port_idx)) = punch_task_result {
|
||||
@@ -644,7 +642,7 @@ pub mod tests {
|
||||
#[tokio::test]
|
||||
#[serial_test::serial(hole_punch)]
|
||||
async fn hole_punching_symmetric_only_predict(#[values("true", "false")] is_inc: bool) {
|
||||
use crate::common::scoped_task::ScopedTask;
|
||||
use tokio_util::task::AbortOnDropHandle;
|
||||
|
||||
RUN_TESTING.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
|
||||
@@ -694,12 +692,12 @@ pub mod tests {
|
||||
|
||||
let counter = Arc::new(AtomicU32::new(0));
|
||||
|
||||
let mut tasks: Vec<ScopedTask<()>> = vec![];
|
||||
let mut tasks: Vec<AbortOnDropHandle<()>> = vec![];
|
||||
|
||||
// all these sockets should receive hole punching packet
|
||||
for udp in udps.iter().map(Arc::clone) {
|
||||
let counter = counter.clone();
|
||||
tasks.push(ScopedTask::from(tokio::spawn(async move {
|
||||
tasks.push(AbortOnDropHandle::new(tokio::spawn(async move {
|
||||
let mut buf = [0u8; 1024];
|
||||
let (len, addr) = udp.recv_from(&mut buf).await.unwrap();
|
||||
println!(
|
||||
|
||||
Reference in New Issue
Block a user