Support for graceful exit from loop using shutdown flag
Added a shutdown flag which allows setting shutdown signal from outside loop() to exit gracefully.
:+1:
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.