Skip to content

Commit

Permalink
Add try_bind_with_graceful_shutdown method to the TlsServer (#717)
Browse files Browse the repository at this point in the history
To have fallible semantics rather than panics when a TLS configuration fails.
  • Loading branch information
jnicholls committed May 5, 2023
1 parent 220be14 commit 4c1f7ba
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ where
<F::Future as TryFuture>::Error: IsReject,
{
/// Run this `Server` forever on the current thread.
///
/// # Panics
///
/// Panics if we are unable to bind to the provided address.
pub async fn run(self, addr: impl Into<SocketAddr>) {
let (addr, fut) = self.bind_ephemeral(addr);
let span = tracing::info_span!("Server::run", ?addr);
Expand Down Expand Up @@ -275,6 +279,10 @@ where
/// let _ = tx.send(());
/// # }
/// ```
///
/// # Panics
///
/// Panics if we are unable to bind to the provided address.
pub fn bind_with_graceful_shutdown(
self,
addr: impl Into<SocketAddr> + 'static,
Expand Down Expand Up @@ -516,6 +524,10 @@ where
/// executed on a runtime.
///
/// *This function requires the `"tls"` feature.*
///
/// # Panics
///
/// Panics if we are unable to bind to the provided address.
pub async fn bind(self, addr: impl Into<SocketAddr>) {
let (_, fut) = self.bind_ephemeral(addr);
fut.await;
Expand All @@ -527,6 +539,10 @@ where
/// the current runtime.
///
/// *This function requires the `"tls"` feature.*
///
/// # Panics
///
/// Panics if we are unable to bind to the provided address.
pub fn bind_ephemeral(
self,
addr: impl Into<SocketAddr>,
Expand All @@ -547,6 +563,10 @@ where
/// process.
///
/// *This function requires the `"tls"` feature.*
///
/// # Panics
///
/// Panics if we are unable to bind to the provided address.
pub fn bind_with_graceful_shutdown(
self,
addr: impl Into<SocketAddr> + 'static,
Expand All @@ -561,6 +581,28 @@ where
});
(addr, fut)
}

/// Create a server with graceful shutdown signal.
///
/// When the signal completes, the server will start the graceful shutdown
/// process.
///
/// *This function requires the `"tls"` feature.*
pub fn try_bind_with_graceful_shutdown(
self,
addr: impl Into<SocketAddr> + 'static,
signal: impl Future<Output = ()> + Send + 'static,
) -> Result<(SocketAddr, impl Future<Output = ()> + 'static), crate::Error> {
let addr = addr.into();
let (addr, srv) = try_bind!(tls: self, &addr).map_err(crate::Error::new)?;
let srv = srv.with_graceful_shutdown(signal).map(|result| {
if let Err(err) = result {
tracing::error!("server error: {}", err)
}
});

Ok((addr, srv))
}
}

#[cfg(feature = "tls")]
Expand Down

0 comments on commit 4c1f7ba

Please sign in to comment.