Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set timeout and keep alive options #261

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions xmtp_networking/src/grpc_api_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn tls_config() -> ClientConfig {
.with_no_client_auth()
}

fn get_tls_connector() -> HttpsConnector<HttpConnector> {
fn get_tls_connector() -> HttpsConnector<tower::timeout::Timeout<HttpConnector>> {
let tls = tls_config();

let mut http = HttpConnector::new();
Expand All @@ -46,14 +46,18 @@ fn get_tls_connector() -> HttpsConnector<HttpConnector> {
.enable_http2()
.wrap_connector(s)
})
.timeout(Duration::from_secs(5))
.service(http)
}

pub enum InnerApiClient {
Plain(MessageApiClient<Channel>),
Tls(
MessageApiClient<
hyper::Client<HttpsConnector<HttpConnector>, UnsyncBoxBody<hyper::body::Bytes, Status>>,
hyper::Client<
HttpsConnector<tower::timeout::Timeout<HttpConnector>>,
UnsyncBoxBody<hyper::body::Bytes, Status>,
>,
>,
),
}
Expand All @@ -69,7 +73,11 @@ impl Client {
if is_secure {
let connector = get_tls_connector();

let tls_conn = hyper::Client::builder().build(connector);
let tls_conn = hyper::Client::builder()
.pool_idle_timeout(Duration::from_secs(30))
.http2_keep_alive_interval(Duration::from_secs(10))
.http2_keep_alive_timeout(Duration::from_secs(5))
.build(connector);

let uri =
Uri::from_str(&host).map_err(|e| Error::new(ErrorKind::SetupError).with(e))?;
Expand All @@ -83,6 +91,11 @@ impl Client {
} else {
let channel = Channel::from_shared(host)
.map_err(|e| Error::new(ErrorKind::SetupError).with(e))?
.timeout(Duration::from_secs(5))
.connect_timeout(Duration::from_secs(5))
.tcp_keepalive(Some(Duration::from_secs(10)))
.http2_keep_alive_interval(Duration::from_secs(10))
.keep_alive_timeout(Duration::from_secs(5))
.connect()
.await
.map_err(|e| Error::new(ErrorKind::SetupError).with(e))?;
Expand Down Expand Up @@ -123,7 +136,6 @@ impl XmtpApiClient for Client {
.map_err(|e| Error::new(ErrorKind::PublishError).with(e))?;

let mut tonic_request = Request::new(request);
tonic_request.set_timeout(Duration::from_secs(5));
tonic_request.metadata_mut().insert("authorization", token);
tonic_request
.metadata_mut()
Expand Down Expand Up @@ -171,7 +183,6 @@ impl XmtpApiClient for Client {

async fn query(&self, request: QueryRequest) -> Result<QueryResponse, Error> {
let mut tonic_request = Request::new(request);
tonic_request.set_timeout(Duration::from_secs(5));
tonic_request
.metadata_mut()
.insert("x-app-version", self.app_version.clone());
Expand All @@ -193,7 +204,6 @@ impl Client {
request: BatchQueryRequest,
) -> Result<BatchQueryResponse, Error> {
let mut tonic_request = Request::new(request);
tonic_request.set_timeout(Duration::from_secs(5));
tonic_request
.metadata_mut()
.insert("x-app-version", self.app_version.clone());
Expand Down