Skip to content

Commit

Permalink
Merge pull request #68 from iamjpotts/20240407-server-example-keepalive
Browse files Browse the repository at this point in the history
  • Loading branch information
softprops authored Apr 7, 2024
2 parents 09f9806 + bf06e28 commit adf3588
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions examples/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hyper::{service::service_fn, Response};
use hyper_util::rt::TokioIo;
use std::{error::Error, fs, io::ErrorKind, path::Path};
use std::{error::Error, fs, path::Path};
use tokio::net::UnixListener;

const PHRASE: &str = "It's a Unix system. I know this.\n";
Expand Down Expand Up @@ -30,30 +30,18 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
Ok::<_, hyper::Error>(Response::new(body))
});

// On linux, serve_connection will return right away with Result::Ok.
//
// On OSX, serve_connection will block until the client disconnects,
// and return Result::Err(hyper::Error) with a source (inner/cause)
// socket error indicating the client connection is no longer open.
match hyper::server::conn::http1::Builder::new()
// On OSX, disabling keep alive prevents serve_connection from
// blocking and later returning an Err derived from E_NOTCONN.
.keep_alive(false)
.serve_connection(io, svc_fn)
.await
{
Ok(()) => {
println!("Accepted connection.");
}
Err(err) => {
let source: Option<&std::io::Error> =
err.source().and_then(|s| s.downcast_ref());

match source {
Some(io_err) if io_err.kind() == ErrorKind::NotConnected => {
println!("Client disconnected.");
}
_ => {
eprintln!("Failed to accept connection: {err:?}");
}
}
eprintln!("Failed to accept connection: {err:?}");
}
};
});
Expand Down

0 comments on commit adf3588

Please sign in to comment.