Skip to content
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

Merged
merged 7 commits into from
Nov 29, 2024
Merged

Conversation

dignifiedquire
Copy link
Contributor

@dignifiedquire dignifiedquire commented Nov 29, 2024

Description

  • `iroh::protocol::Router
    • 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
    • use Arc<Mutex<Option<JoinHandle>>> instead of Shared<JoinHandle> to more cleanly represent shutdown

Breaking Changes

  • iroh::protocol::Router::shutdown takes &self instead of self

Change checklist

  • Self-review.
  • Documentation updates following the style guide, if relevant.
  • Tests if relevant.
  • All breaking changes documented.

@dignifiedquire dignifiedquire changed the title Feat improve shutdown feat(iroh): improve Router shutdown Nov 29, 2024
Copy link

github-actions bot commented Nov 29, 2024

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

/// 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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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.

Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Some(Ok(Err(inner))) => {
debug!("Task errored: {inner:?}");
Some(Ok(Some(()))) => {
debug!("Task finished");
Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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!

Copy link
Contributor

@matheus23 matheus23 left a 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.

Comment on lines 306 to 309
debug!("Task finished");
}
Some(Ok(None)) => {
debug!("Task cancelled");
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switched to trace

Comment on lines 323 to 325
join_set.spawn(async move {
token.run_until_cancelled(handle_connection(incoming, protocols)).await
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't compile..

@dignifiedquire
Copy link
Contributor Author

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.

they all need protocols, which is why I haven't done it yet

@matheus23
Copy link
Contributor

You could copy the Echo protocol from the README (or the echo.rs example)

@dignifiedquire
Copy link
Contributor Author

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
Copy link

Netsim report & logs for this PR have been generated and is available at: LOGS
This report will remain available for 3 days.

Last updated for commit: a635592

@dignifiedquire dignifiedquire added this pull request to the merge queue Nov 29, 2024
Merged via the queue into main with commit fbcaaa5 Nov 29, 2024
26 checks passed
@dignifiedquire dignifiedquire deleted the feat-improve-shutdown branch November 29, 2024 11:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants