Remove duplication in serving with and without graceful shutdown

This commit is contained in:
David Pedersen 2023-12-31 11:51:48 +01:00
parent 71eedc6d6c
commit 5028d97efd

View file

@ -181,54 +181,15 @@ where
type IntoFuture = private::ServeFuture; type IntoFuture = private::ServeFuture;
fn into_future(self) -> Self::IntoFuture { fn into_future(self) -> Self::IntoFuture {
private::ServeFuture(Box::pin(async move { let Self {
let Self { tcp_listener,
tcp_listener, make_service,
mut make_service, _marker: _,
_marker: _, } = self;
} = self;
loop { serve(tcp_listener, make_service)
let (tcp_stream, remote_addr) = match tcp_accept(&tcp_listener).await { .with_graceful_shutdown(std::future::pending())
Some(conn) => conn, .into_future()
None => continue,
};
let tcp_stream = TokioIo::new(tcp_stream);
poll_fn(|cx| make_service.poll_ready(cx))
.await
.unwrap_or_else(|err| match err {});
let tower_service = make_service
.call(IncomingStream {
tcp_stream: &tcp_stream,
remote_addr,
})
.await
.unwrap_or_else(|err| match err {});
let hyper_service = TowerToHyperService {
service: tower_service,
};
tokio::spawn(async move {
match Builder::new(TokioExecutor::new())
// upgrades needed for websockets
.serve_connection_with_upgrades(tcp_stream, hyper_service)
.await
{
Ok(()) => {}
Err(_err) => {
// This error only appears when the client doesn't send a request and
// terminate the connection.
//
// If client sends one request then terminate connection whenever, it doesn't
// appear.
}
}
});
}
}))
} }
} }