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

Graceful shutdown2 #746

Closed
wants to merge 4 commits into from
Closed

Graceful shutdown2 #746

wants to merge 4 commits into from

Conversation

GWBasic
Copy link

@GWBasic GWBasic commented Nov 18, 2020

Fixes #528

It works by awaiting on two futures and returns when the first completes: https://github.com/http-rs/tide/compare/main...GWBasic:GracefulShutdown2?w=1#diff-1f14b8b6953e2388b19efaf814862a89c0b57c45a6814f79ed373fde05d864d0R82

I then created a "CancelationToken" future that returns once it is canceled.

(For context, see #528 (comment))

@GWBasic GWBasic mentioned this pull request Nov 18, 2020
@jbr jbr self-requested a review November 18, 2020 16:30
@GWBasic
Copy link
Author

GWBasic commented Nov 18, 2020

Note: The syntax might be cleaner using the select macro: https://rust-lang.github.io/async-book/06_multiple_futures/03_select.html

@GWBasic
Copy link
Author

GWBasic commented Dec 3, 2020

Just an FYI that I created a crate with CancelationToken. Again, looking for some feedback!

Copy link
Member

@yoshuawuyts yoshuawuyts left a comment

Choose a reason for hiding this comment

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

Hey @GWBasic, thanks so much for kicking this off!

In broad strokes it seems what you're implementing is in the right direction; but we could probably use a round of design feedback on this. Usually I'd suggest opening an issue first, but given we're already discussing this in a PR, doing that here seems fine.

Specifically I want to get a better idea of how we expect end-users to register cancellation in Tide, and how that will work with the various listeners. cc/ @jbr I think you may have had a few ideas here?

@@ -41,6 +41,7 @@ async-sse = "4.0.1"
async-std = { version = "1.6.5", features = ["unstable"] }
async-trait = "0.1.41"
femme = { version = "2.1.1", optional = true }
futures = "0.3.7"
Copy link
Member

Choose a reason for hiding this comment

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

We probably don't want to depend on futures directly since it adds about a minute of compile time in certain setups. Instead it's preferable to use smaller crates and/or async-std directly.

Copy link
Author

Choose a reason for hiding this comment

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

Futures is required for "select": https://rust-lang.github.io/async-book/06_multiple_futures/03_select.html

Honestly, I don't see a way to do this without "select," otherwise you end up waiting for a final incoming http request before ending the loop accepting incoming requests. (See my comment about when I reviewed stop-token.)

Maybe it's worth trying to implement something like select manually, but, IMO, that might be a fools' errand.

Copy link
Member

Choose a reason for hiding this comment

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

I think what we would prefer is futures_lite::future::race.

Copy link
Author

Choose a reason for hiding this comment

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

Great advice!

Let me see what I can do. It'll probably be a few days before I can update this PR.

shared_state: self.shared_state.clone()
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

The stop-token does much the same, and we're looking to integrate it into async-std in the near future. If possible it'd be great to use that crate directly instead of defining our own here.

Copy link
Author

Choose a reason for hiding this comment

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

One thing I like about stop-token is that it supports streams.

However, the authors describe it as "experimental." Upon closer inspection:

I should point out that the library that I made with cancelation token has full documentation and tests: https://crates.io/crates/sync-tokens

@GWBasic
Copy link
Author

GWBasic commented Dec 7, 2020

@yoshuawuyts : Regarding how end users can register for cancelation, take a look at the example code I published here: https://docs.rs/sync-tokens/0.1.0/sync_tokens/

Pay attention to main:

  1. The caller knows when the server is listening
  2. The caller explicitly calls cancel
  3. The caller can await for the server to complete

@GWBasic
Copy link
Author

GWBasic commented Dec 16, 2020

Replaced with #766 due to excessive merge conflicts

@GWBasic GWBasic closed this Dec 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Graceful shutdown
3 participants