Skip to content

Commit

Permalink
refactor(iroh): Replace timer module with AbortOnDropHandle and s…
Browse files Browse the repository at this point in the history
…leep (#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.
  • Loading branch information
matheus23 authored Jan 22, 2025
1 parent eadc76b commit 43e9805
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 110 deletions.
2 changes: 0 additions & 2 deletions iroh/src/magicsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,10 @@ use crate::{
mod metrics;
mod node_map;
mod relay_actor;
mod timer;
mod udp_conn;

pub use node_map::Source;

pub(super) use self::timer::Timer;
pub use self::{
metrics::Metrics,
node_map::{ConnectionType, ControlMsg, DirectAddrInfo, RemoteInfo},
Expand Down
14 changes: 7 additions & 7 deletions iroh/src/magicsock/node_map/node_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use iroh_relay::protos::stun;
use netwatch::ip::is_unicast_link_local;
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc;
use tokio_util::task::AbortOnDropHandle;
use tracing::{debug, event, info, instrument, trace, warn, Level};

use super::{
Expand All @@ -24,7 +25,7 @@ use super::{
use crate::endpoint::PathSelection;
use crate::{
disco::{self, SendAddr},
magicsock::{ActorMessage, MagicsockMetrics, QuicMappedAddr, Timer, HEARTBEAT_INTERVAL},
magicsock::{ActorMessage, MagicsockMetrics, QuicMappedAddr, HEARTBEAT_INTERVAL},
watchable::{Watchable, Watcher},
};

Expand Down Expand Up @@ -526,19 +527,20 @@ impl NodeState {
}

let id = self.id;
let timer = Timer::after(PING_TIMEOUT_DURATION, async move {
let _expiry_task = AbortOnDropHandle::new(tokio::spawn(async move {
tokio::time::sleep(PING_TIMEOUT_DURATION).await;
sender
.send(ActorMessage::EndpointPingExpired(id, tx_id))
.await
.ok();
});
}));
self.sent_pings.insert(
tx_id,
SentPing {
to,
at: now,
purpose,
timer,
_expiry_task,
},
);
}
Expand Down Expand Up @@ -887,8 +889,6 @@ impl NodeState {
None
}
Some(sp) => {
sp.timer.abort();

let mut node_map_insert = None;

let now = Instant::now();
Expand Down Expand Up @@ -1230,7 +1230,7 @@ pub(super) struct SentPing {
pub(super) at: Instant,
#[allow(dead_code)]
pub(super) purpose: DiscoPingPurpose,
pub(super) timer: Timer,
pub(super) _expiry_task: AbortOnDropHandle<()>,
}

/// The reason why a discovery ping message was sent.
Expand Down
101 changes: 0 additions & 101 deletions iroh/src/magicsock/timer.rs

This file was deleted.

0 comments on commit 43e9805

Please sign in to comment.