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

[http server]: use tokio::spawn internally in HttpServer::start and return StopHandle #402

Merged
merged 15 commits into from
Oct 11, 2021

Conversation

niklasad1
Copy link
Member

@niklasad1 niklasad1 commented Jul 1, 2021

Changes the API to return the StopHandle after HttpServer::start instead of having to call a separate method handle....

@niklasad1 niklasad1 changed the title [WIP]: refactor start API for the servers to use tokio spawn [http server]: use tokio::spawn internally in HttpServer::start and return StopHandle Aug 3, 2021
@dvdplm dvdplm mentioned this pull request Sep 8, 2021
17 tasks
@niklasad1 niklasad1 marked this pull request as ready for review October 8, 2021 13:50
/// Blocks indefinitely until the server is stopped.
pub async fn wait_for_stop(&self) {
self.stop_handle.lock().await;
pub fn stop(mut self) -> Result<tokio::task::JoinHandle<()>, Error> {
Copy link
Member Author

@niklasad1 niklasad1 Oct 11, 2021

Choose a reason for hiding this comment

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

//cc @maciejhirsz I tried to unify the APIs but the underlying impl is different and fine by me at least.

I guess it would be nice to replace the stop_sender with your AtomicUsize waker because https://docs.rs/hyper/0.14.13/hyper/server/struct.Server.html#method.with_graceful_shutdown requires at future to be resolved but didn't work out of the box.....

any suggestions?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's fine for now, I need to debug the ws one first, might have some suggestions for it afterwards.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is fine too; this flavour is a bit easier to read imo.

/// Configure a custom [`tokio::runtime::Handle`] to run the server on.
///
/// Default: [`tokio::spawn`]
pub fn custom_tokio_runtime(mut self, rt: tokio::runtime::Handle) {
Copy link
Member Author

Choose a reason for hiding this comment

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

we might want to re-export Handle but I decided not because the library is tightly-coupled to tokio and if the wrong tokio version is used a compile error is more likely than a runtime error.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! I was thinking about adding something like this for ws for testing, but that ended ultimately not being necessary so I didn't bother. Should we add it there too?

Copy link
Member Author

@niklasad1 niklasad1 Oct 11, 2021

Choose a reason for hiding this comment

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

I have already added it to the WS server if that's ok? :)

http-server/src/server.rs Outdated Show resolved Hide resolved
/// Configure a custom [`tokio::runtime::Handle`] to run the server on.
///
/// Default: [`tokio::spawn`]
pub fn custom_tokio_runtime(mut self, rt: tokio::runtime::Handle) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! I was thinking about adding something like this for ws for testing, but that ended ultimately not being necessary so I didn't bother. Should we add it there too?

Co-authored-by: Maciej Hirsz <1096222+maciejhirsz@users.noreply.github.com>
///
/// Default: [`tokio::spawn`]
pub fn custom_tokio_runtime(mut self, rt: tokio::runtime::Handle) {
self.settings.tokio_runtime = Some(rt);
Copy link
Member Author

Choose a reason for hiding this comment

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

I added this to the WS Server too...

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, right, nice! :D

Copy link
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

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

lgtm

/// Blocks indefinitely until the server is stopped.
pub async fn wait_for_stop(&self) {
self.stop_handle.lock().await;
pub fn stop(mut self) -> Result<tokio::task::JoinHandle<()>, Error> {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is fine too; this flavour is a bit easier to read imo.

@dvdplm dvdplm merged commit 6fb61dc into master Oct 11, 2021
@dvdplm dvdplm deleted the na-servers-tokio-spawn branch October 11, 2021 18:13
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.

3 participants