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

Support for graceful exit from loop using shutdown flag #118

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

rohitjoshi
Copy link

Added a shutdown flag which allows setting shutdown signal from outside loop() to exit gracefully.

@dhontecillas
Copy link

dhontecillas commented Sep 11, 2019

👍

It would be nice to have the ability to shutdown the server.

Example of use case: A command line tool that wants to run a server locally to receive the result of a third party authorization process. The tool launches a server to give the third party the url for the redirection (e.g: http://localhost:8000/). And once the user gives the authorization, we can receive the token and shutdown the server.

However one of the issues of the current implementation is that we are using an iterator to receive the next incoming connection (see the iterator https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next ), so, once we enter the loop, if we do not receive any connection we would be blocking on that line, and we would never reach the point to check the shutdown flag again.

            // check if shutdown received
            if self.shutdown.load(Ordering::SeqCst) {
                return;
            }
            // Incoming is an endless iterator, so it's okay to unwrap on it.
            let stream = incoming.next().unwrap();
            let stream = stream.expect("Error handling TCP stream.");

I am not sure if set_nonblocking and polling the socket for incoming connections would work.

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.

2 participants