Skip to content

Commit b879753

Browse files
committedNov 8, 2024
add timeout to channel builder
1 parent 9a61b0a commit b879753

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed
 

‎rust/src/common/error.rs

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ error_messages! { ConnectionError
7474
23: "Invalid URL '{address}': missing port.",
7575
AddressTranslationMismatch { unknown: HashSet<Address>, unmapped: HashSet<Address> } =
7676
24: "Address translation map does not match the server's advertised address list. User-provided servers not in the advertised list: {unknown:?}. Advertised servers not mapped by user: {unmapped:?}.",
77+
ConnectionTimedOut =
78+
25: "Connection to the server timed out."
7779
}
7880

7981
error_messages! { InternalError
@@ -196,6 +198,8 @@ impl From<Status> for Error {
196198
Self::Connection(ConnectionError::ServerConnectionFailedStatusError { error: status.message().to_owned() })
197199
} else if status.code() == Code::Unimplemented {
198200
Self::Connection(ConnectionError::RPCMethodUnavailable { message: status.message().to_owned() })
201+
} else if status.code() == Code::Cancelled && status.message() == "Timeout expired" {
202+
Self::Connection(ConnectionError::ConnectionTimedOut)
199203
} else {
200204
Self::from_message(status.message())
201205
}

‎rust/src/connection/network/channel.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
* under the License.
1818
*/
1919

20-
use std::sync::{Arc, RwLock};
20+
use std::{
21+
sync::{Arc, RwLock},
22+
time::Duration,
23+
};
2124

2225
use tonic::{
2326
body::BoxBody,
@@ -49,8 +52,13 @@ impl GRPCChannel for PlainTextChannel {}
4952

5053
impl GRPCChannel for CallCredChannel {}
5154

55+
const TIMEOUT: Duration = Duration::from_secs(60);
56+
5257
pub(super) fn open_plaintext_channel(address: Address) -> PlainTextChannel {
53-
PlainTextChannel::new(Channel::builder(address.into_uri()).connect_lazy(), PlainTextFacade)
58+
PlainTextChannel::new(
59+
Channel::builder(address.into_uri()).timeout(TIMEOUT).connect_lazy(),
60+
PlainTextFacade,
61+
)
5462
}
5563

5664
#[derive(Clone, Debug)]
@@ -66,7 +74,7 @@ pub(super) fn open_callcred_channel(
6674
address: Address,
6775
credential: Credential,
6876
) -> Result<(CallCredChannel, Arc<CallCredentials>)> {
69-
let mut builder = Channel::builder(address.into_uri());
77+
let mut builder = Channel::builder(address.into_uri()).timeout(TIMEOUT);
7078
if credential.is_tls_enabled() {
7179
builder = builder.tls_config(credential.tls_config().clone().unwrap())?;
7280
}

0 commit comments

Comments
 (0)