-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(iroh): improve Router shutdown #2978
Conversation
Documentation for this PR has been generated and is available at: https://n0-computer.github.io/iroh/pr/2978/docs/iroh/ Last updated: 2024-11-29T10:39:27Z |
iroh/src/protocol.rs
Outdated
/// Shuts down the accept loop cleanly. | ||
/// | ||
/// If some [`ProtocolHandler`] panicked in the accept loop, this will propagate | ||
/// that panic into the result here. | ||
pub async fn shutdown(self) -> Result<()> { | ||
/// | ||
/// If already shutdown, it just returns `Ok`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// If already shutdown, it just returns `Ok`. | |
/// If already shutdown, it returns `Ok`. |
Text is almost always better off without words like "just" or "only" etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably say something about what it is waiting on. Is it fully shut down when it returns?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
iroh/src/protocol.rs
Outdated
Some(Ok(Err(inner))) => { | ||
debug!("Task errored: {inner:?}"); | ||
Some(Ok(Some(()))) => { | ||
debug!("Task finished"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the granularity of tasks here? Basically I'm wondering whether this is too nosy to be on debug level and should rather be on trace level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One task per incoming connection
let protocols = protocols.clone(); | ||
let token = cancel_token.child_token(); | ||
join_set.spawn(async move { | ||
token.run_until_cancelled(handle_connection(incoming, protocols)).await |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL about this function, I would have built this myself!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. I thought about the change to Mutex
, but yeah I agree that's fine, probably even better.
I'm not sure where I stand w.r.t. tests. Always better to have them :) Maybe it's possible to dig the old Node
tests up and see what can be adapted to here.
iroh/src/protocol.rs
Outdated
debug!("Task finished"); | ||
} | ||
Some(Ok(None)) => { | ||
debug!("Task cancelled"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if we should log this... I mean, it doesn't cost much, but I do think there's potential for needless log pollution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
switched to trace
iroh/src/protocol.rs
Outdated
join_set.spawn(async move { | ||
token.run_until_cancelled(handle_connection(incoming, protocols)).await | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
join_set.spawn(async move { | |
token.run_until_cancelled(handle_connection(incoming, protocols)).await | |
}); | |
join_set.spawn(token.run_until_cancelled(handle_connection(incoming, protocols))); |
I'm a sucker for pointfree style. That said, this might not compile. And it might not actually read better to others.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't compile..
they all need protocols, which is why I haven't done it yet |
You could copy the |
lets to the test adding in a different PR, as they don't directly test shutdown |
- `shutdown` now takes `&self` - `shutdown` is idempotent, and will immediately return if already shutdown - there is now a `is_shutdown` method to check if the router is already closed - spawned tasks are cancelled through the cancel token as well, to improve shutdown speed
adc16ff
to
0eaed7d
Compare
Description
shutdown
now takes&self
shutdown
is idempotent, and will immediately return if already shutdownis_shutdown
method to check if the router is already closedArc<Mutex<Option<JoinHandle>>>
instead ofShared<JoinHandle>
to more cleanly represent shutdownBreaking Changes
iroh::protocol::Router::shutdown
takes&self
instead ofself
Change checklist