Skip to content

Commit

Permalink
[grpc_clients] add connect_timeout to shared grpc client
Browse files Browse the repository at this point in the history
Summary: by default, the shared grpc client will try to connect indefinitely to the endpoint url. we should time out after 1 second if we're unable to connect.

Test Plan: repro'd [ENG-6052](https://linear.app/comm/issue/ENG-6052/rust-node-addon-crashes-when-identity-service-config-is-missing) and then added the time out and confirmed that the client only waited 1 second before giving up

Reviewers: ashoat, bartek

Reviewed By: bartek

Subscribers: tomek

Differential Revision: https://phab.comm.dev/D10329
  • Loading branch information
vdhanan committed Dec 15, 2023
1 parent e18fe51 commit d4f399f
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion shared/grpc_clients/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub use tonic;

use error::Error;
use std::path::Path;
use std::time::Duration;
use tonic::transport::{Certificate, Channel, ClientTlsConfig};
use tracing::info;

Expand All @@ -17,6 +18,7 @@ const CERT_PATHS: &[&str] = &[
"/etc/ssl/certs/ca-bundle.crt",
"/etc/ssl/certs/ca-certificates.crt",
];
const CONNECT_TIMEOUT_DURATION: Duration = Duration::from_secs(5);

pub(crate) fn get_ca_cert_contents() -> Option<String> {
CERT_PATHS
Expand All @@ -32,7 +34,8 @@ pub(crate) async fn get_grpc_service_channel(
let ca_cert = crate::get_ca_cert_contents().expect("Unable to get CA bundle");

info!("Connecting to gRPC service at {}", url);
let mut channel = Channel::from_shared(url.to_string())?;
let mut channel = Channel::from_shared(url.to_string())?
.connect_timeout(CONNECT_TIMEOUT_DURATION);

// tls_config will fail if the underlying URI is only http://
if url.starts_with("https:") {
Expand Down

0 comments on commit d4f399f

Please sign in to comment.