fix(transport): Propagate errors in tls_config instead of unwrap/panic (#385)
* Propagate errors in tls_config instead of unwrap Ran into `tls_connector` failing and causing our app to panic and shutdown as it seems there wasn't any way to avoid panicking in `tls_config`. So after talking to @LucioFranco briefly `tls_config` now returns a `Result` instead and propagates errors to the caller, where they can be handled. * Fix compile warning when tls feature is disabled
This commit is contained in:
@@ -155,11 +155,15 @@ impl Endpoint {
|
||||
/// Configures TLS for the endpoint.
|
||||
#[cfg(feature = "tls")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
|
||||
pub fn tls_config(self, tls_config: ClientTlsConfig) -> Self {
|
||||
Endpoint {
|
||||
tls: Some(tls_config.tls_connector(self.uri.clone()).unwrap()),
|
||||
pub fn tls_config(self, tls_config: ClientTlsConfig) -> Result<Self, Error> {
|
||||
Ok(Endpoint {
|
||||
tls: Some(
|
||||
tls_config
|
||||
.tls_connector(self.uri.clone())
|
||||
.map_err(|e| Error::from_source(e))?,
|
||||
),
|
||||
..self
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Set the value of `TCP_NODELAY` option for accepted connections. Enabled by default.
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
//! let mut channel = Channel::from_static("https://example.com")
|
||||
//! .tls_config(ClientTlsConfig::new()
|
||||
//! .ca_certificate(Certificate::from_pem(&cert))
|
||||
//! .domain_name("example.com".to_string()))
|
||||
//! .domain_name("example.com".to_string()))?
|
||||
//! .timeout(Duration::from_secs(5))
|
||||
//! .rate_limit(5, Duration::from_secs(1))
|
||||
//! .concurrency_limit(256)
|
||||
@@ -74,7 +74,7 @@
|
||||
//!
|
||||
//! Server::builder()
|
||||
//! .tls_config(ServerTlsConfig::new()
|
||||
//! .identity(Identity::from_pem(&cert, &key)))
|
||||
//! .identity(Identity::from_pem(&cert, &key)))?
|
||||
//! .concurrency_limit_per_connection(256)
|
||||
//! .add_service(my_svc)
|
||||
//! .serve(addr)
|
||||
|
||||
@@ -18,6 +18,9 @@ use incoming::TcpIncoming;
|
||||
#[cfg(feature = "tls")]
|
||||
pub(crate) use incoming::TlsStream;
|
||||
|
||||
#[cfg(feature = "tls")]
|
||||
use crate::transport::Error;
|
||||
|
||||
use super::service::{Or, Routes, ServerIo, ServiceBuilderExt};
|
||||
use crate::{body::BoxBody, request::ConnectionInfo};
|
||||
use futures_core::Stream;
|
||||
@@ -97,11 +100,15 @@ impl Server {
|
||||
/// Configure TLS for this server.
|
||||
#[cfg(feature = "tls")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
|
||||
pub fn tls_config(self, tls_config: ServerTlsConfig) -> Self {
|
||||
Server {
|
||||
tls: Some(tls_config.tls_acceptor().unwrap()),
|
||||
pub fn tls_config(self, tls_config: ServerTlsConfig) -> Result<Self, Error> {
|
||||
Ok(Server {
|
||||
tls: Some(
|
||||
tls_config
|
||||
.tls_acceptor()
|
||||
.map_err(|e| Error::from_source(e))?,
|
||||
),
|
||||
..self
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Set the concurrency limit applied to on requests inbound per connection.
|
||||
|
||||
Reference in New Issue
Block a user