Skip to content

Commit 43e9805

Browse files
authored
refactor(iroh): Replace timer module with AbortOnDropHandle and sleep (#3141)
## Description I find this easier to reason about. Better this than having to understand an additional concept (the golang timer). ## Notes & open questions This is opinionated. I'm not married to this, so feel free to critique. This also makes it such that when the MagicSocket is dropped (/the NodeMap is dropped), it'll abort all these tasks. ## Change checklist - [x] Self-review.
1 parent eadc76b commit 43e9805

File tree

3 files changed

+7
-110
lines changed

3 files changed

+7
-110
lines changed

iroh/src/magicsock.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,10 @@ use crate::{
7575
mod metrics;
7676
mod node_map;
7777
mod relay_actor;
78-
mod timer;
7978
mod udp_conn;
8079

8180
pub use node_map::Source;
8281

83-
pub(super) use self::timer::Timer;
8482
pub use self::{
8583
metrics::Metrics,
8684
node_map::{ConnectionType, ControlMsg, DirectAddrInfo, RemoteInfo},

iroh/src/magicsock/node_map/node_state.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use iroh_relay::protos::stun;
1212
use netwatch::ip::is_unicast_link_local;
1313
use serde::{Deserialize, Serialize};
1414
use tokio::sync::mpsc;
15+
use tokio_util::task::AbortOnDropHandle;
1516
use tracing::{debug, event, info, instrument, trace, warn, Level};
1617

1718
use super::{
@@ -24,7 +25,7 @@ use super::{
2425
use crate::endpoint::PathSelection;
2526
use crate::{
2627
disco::{self, SendAddr},
27-
magicsock::{ActorMessage, MagicsockMetrics, QuicMappedAddr, Timer, HEARTBEAT_INTERVAL},
28+
magicsock::{ActorMessage, MagicsockMetrics, QuicMappedAddr, HEARTBEAT_INTERVAL},
2829
watchable::{Watchable, Watcher},
2930
};
3031

@@ -526,19 +527,20 @@ impl NodeState {
526527
}
527528

528529
let id = self.id;
529-
let timer = Timer::after(PING_TIMEOUT_DURATION, async move {
530+
let _expiry_task = AbortOnDropHandle::new(tokio::spawn(async move {
531+
tokio::time::sleep(PING_TIMEOUT_DURATION).await;
530532
sender
531533
.send(ActorMessage::EndpointPingExpired(id, tx_id))
532534
.await
533535
.ok();
534-
});
536+
}));
535537
self.sent_pings.insert(
536538
tx_id,
537539
SentPing {
538540
to,
539541
at: now,
540542
purpose,
541-
timer,
543+
_expiry_task,
542544
},
543545
);
544546
}
@@ -887,8 +889,6 @@ impl NodeState {
887889
None
888890
}
889891
Some(sp) => {
890-
sp.timer.abort();
891-
892892
let mut node_map_insert = None;
893893

894894
let now = Instant::now();
@@ -1230,7 +1230,7 @@ pub(super) struct SentPing {
12301230
pub(super) at: Instant,
12311231
#[allow(dead_code)]
12321232
pub(super) purpose: DiscoPingPurpose,
1233-
pub(super) timer: Timer,
1233+
pub(super) _expiry_task: AbortOnDropHandle<()>,
12341234
}
12351235

12361236
/// The reason why a discovery ping message was sent.

iroh/src/magicsock/timer.rs

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)