Skip to content

Commit e3f2965

Browse files
authored
chore(tonic): Move ConnectError type from transport (#1828)
* chore(tonic): Move TimeoutExpired out of transport By moving the TimeoutExpired type from the transport module to the the tonic engine proper, the type is made available to other transport implementations and tonic can be built without the "server" feature while still returning a Status indicating the timeout. Alternative transport implementations can use the type, now that it is moved, when they want the tonic Status error handling to recognize the error as having been triggered by a timeout in the transport logic. The tonic Status error will have a code of Cancelled with a message of "Timeout expired". There is already a test for this: cargo test picks_server_timeout_if_thats_sorter which worked the original way and contines to work; but now a new transport implementation can get the same behavior. Addresses #1825. * chore(tonic): Move ConnectError out of transport
1 parent c17d438 commit e3f2965

File tree

4 files changed

+22
-23
lines changed

4 files changed

+22
-23
lines changed

tonic/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ pub use extensions::GrpcMethod;
120120
pub use http::Extensions;
121121
pub use request::{IntoRequest, IntoStreamingRequest, Request};
122122
pub use response::Response;
123-
pub use status::{Code, Status, TimeoutExpired};
123+
pub use status::{Code, ConnectError, Status, TimeoutExpired};
124124

125125
pub(crate) type Error = Box<dyn std::error::Error + Send + Sync>;
126126

tonic/src/status.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -615,10 +615,7 @@ fn find_status_in_source_chain(err: &(dyn Error + 'static)) -> Option<Status> {
615615
// matches the spec of:
616616
// > The service is currently unavailable. This is most likely a transient condition that
617617
// > can be corrected if retried with a backoff.
618-
#[cfg(feature = "channel")]
619-
if let Some(connect) =
620-
err.downcast_ref::<crate::transport::channel::service::ConnectError>()
621-
{
618+
if let Some(connect) = err.downcast_ref::<ConnectError>() {
622619
return Some(Status::unavailable(connect.to_string()));
623620
}
624621

@@ -1032,3 +1029,20 @@ impl fmt::Display for TimeoutExpired {
10321029

10331030
// std::error::Error only requires a type to impl Debug and Display
10341031
impl std::error::Error for TimeoutExpired {}
1032+
1033+
/// Wrapper type to indicate that an error occurs during the connection
1034+
/// process, so that the appropriate gRPC Status can be inferred.
1035+
#[derive(Debug)]
1036+
pub struct ConnectError(pub Box<dyn std::error::Error + Send + Sync>);
1037+
1038+
impl fmt::Display for ConnectError {
1039+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1040+
fmt::Display::fmt(&self.0, f)
1041+
}
1042+
}
1043+
1044+
impl std::error::Error for ConnectError {
1045+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
1046+
Some(self.0.as_ref())
1047+
}
1048+
}

tonic/src/transport/channel/service/connector.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use super::BoxedIo;
22
#[cfg(feature = "tls")]
33
use super::TlsConnector;
44
use crate::transport::channel::BoxFuture;
5+
use crate::ConnectError;
56
use http::Uri;
7+
#[cfg(feature = "tls")]
68
use std::fmt;
79
use std::task::{Context, Poll};
810

@@ -12,23 +14,6 @@ use hyper::rt;
1214
use hyper_util::rt::TokioIo;
1315
use tower_service::Service;
1416

15-
/// Wrapper type to indicate that an error occurs during the connection
16-
/// process, so that the appropriate gRPC Status can be inferred.
17-
#[derive(Debug)]
18-
pub(crate) struct ConnectError(pub(crate) crate::Error);
19-
20-
impl fmt::Display for ConnectError {
21-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22-
fmt::Display::fmt(&self.0, f)
23-
}
24-
}
25-
26-
impl std::error::Error for ConnectError {
27-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
28-
Some(self.0.as_ref())
29-
}
30-
}
31-
3217
pub(crate) struct Connector<C> {
3318
inner: C,
3419
#[cfg(feature = "tls")]

tonic/src/transport/channel/service/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod io;
1717
use self::io::BoxedIo;
1818

1919
mod connector;
20-
pub(crate) use self::connector::{ConnectError, Connector};
20+
pub(crate) use self::connector::Connector;
2121

2222
mod executor;
2323
pub(super) use self::executor::{Executor, SharedExec};

0 commit comments

Comments
 (0)