Skip to content

Commit

Permalink
add timeout to channel builder
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrii-ubskii committed Nov 8, 2024
1 parent 9a61b0a commit b879753
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions rust/src/common/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ error_messages! { ConnectionError
23: "Invalid URL '{address}': missing port.",
AddressTranslationMismatch { unknown: HashSet<Address>, unmapped: HashSet<Address> } =
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:?}.",
ConnectionTimedOut =
25: "Connection to the server timed out."
}

error_messages! { InternalError
Expand Down Expand Up @@ -196,6 +198,8 @@ impl From<Status> for Error {
Self::Connection(ConnectionError::ServerConnectionFailedStatusError { error: status.message().to_owned() })
} else if status.code() == Code::Unimplemented {
Self::Connection(ConnectionError::RPCMethodUnavailable { message: status.message().to_owned() })
} else if status.code() == Code::Cancelled && status.message() == "Timeout expired" {
Self::Connection(ConnectionError::ConnectionTimedOut)
} else {
Self::from_message(status.message())
}
Expand Down
14 changes: 11 additions & 3 deletions rust/src/connection/network/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
* under the License.
*/

use std::sync::{Arc, RwLock};
use std::{
sync::{Arc, RwLock},
time::Duration,
};

use tonic::{
body::BoxBody,
Expand Down Expand Up @@ -49,8 +52,13 @@ impl GRPCChannel for PlainTextChannel {}

impl GRPCChannel for CallCredChannel {}

const TIMEOUT: Duration = Duration::from_secs(60);

pub(super) fn open_plaintext_channel(address: Address) -> PlainTextChannel {
PlainTextChannel::new(Channel::builder(address.into_uri()).connect_lazy(), PlainTextFacade)
PlainTextChannel::new(
Channel::builder(address.into_uri()).timeout(TIMEOUT).connect_lazy(),
PlainTextFacade,
)
}

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

0 comments on commit b879753

Please sign in to comment.