From 36757ff1730b99f3c3d101f666fb49431e74be5d Mon Sep 17 00:00:00 2001 From: mattstam Date: Thu, 9 Jan 2025 12:14:38 -0800 Subject: [PATCH 1/3] feat(sdk): configure grpc settings --- crates/sdk/src/network/client.rs | 12 ++---------- crates/sdk/src/network/grpc.rs | 24 ++++++++++++++++++++++++ crates/sdk/src/network/mod.rs | 1 + 3 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 crates/sdk/src/network/grpc.rs diff --git a/crates/sdk/src/network/client.rs b/crates/sdk/src/network/client.rs index ae0b405c6..ecd53e323 100644 --- a/crates/sdk/src/network/client.rs +++ b/crates/sdk/src/network/client.rs @@ -19,6 +19,7 @@ use tonic::{ Code, }; +use super::grpc; use super::utils::Signable; use crate::network::proto::artifact::{ artifact_store_client::ArtifactStoreClient, ArtifactType, CreateArtifactRequest, @@ -258,16 +259,7 @@ impl NetworkClient { } pub(crate) async fn prover_network_client(&self) -> Result> { - let rpc_url = self.rpc_url.clone(); - let mut endpoint = Channel::from_shared(rpc_url.clone())?; - - // Check if the URL scheme is HTTPS and configure TLS. - if rpc_url.starts_with("https://") { - let tls_config = ClientTlsConfig::new().with_enabled_roots(); - endpoint = endpoint.tls_config(tls_config)?; - } - - let channel = endpoint.connect().await?; + let channel = grpc::configure_endpoint(self.rpc_url.clone())?.connect().await?; Ok(ProverNetworkClient::new(channel)) } diff --git a/crates/sdk/src/network/grpc.rs b/crates/sdk/src/network/grpc.rs new file mode 100644 index 000000000..afe45e29b --- /dev/null +++ b/crates/sdk/src/network/grpc.rs @@ -0,0 +1,24 @@ +use std::time::Duration; +use tonic::transport::{ClientTlsConfig, Endpoint, Error}; + +/// Configures the endpoint for the gRPC client. +/// +/// Sets reasonable settings to handle timeouts and keep-alive. +pub fn configure_endpoint(addr: String) -> Result { + let mut endpoint = Endpoint::new(addr.clone())? + .timeout(Duration::from_secs(60)) + .connect_timeout(Duration::from_secs(15)) + .keep_alive_while_idle(true) + .http2_keep_alive_interval(Duration::from_secs(15)) + .keep_alive_timeout(Duration::from_secs(15)) + .tcp_keepalive(Some(Duration::from_secs(60))) + .tcp_nodelay(true); + + // Configure TLS if using HTTPS. + if addr.starts_with("https://") { + let tls_config = ClientTlsConfig::new().with_enabled_roots(); + endpoint = endpoint.tls_config(tls_config)?; + } + + Ok(endpoint) +} diff --git a/crates/sdk/src/network/mod.rs b/crates/sdk/src/network/mod.rs index bcec52ab1..77fcbfa36 100644 --- a/crates/sdk/src/network/mod.rs +++ b/crates/sdk/src/network/mod.rs @@ -11,6 +11,7 @@ pub mod prover; pub mod proto; pub mod builder; mod error; +mod grpc; pub mod prove; pub mod utils; From b1f1f5f088e43578330e5ac3a8eca04a7d7bf59f Mon Sep 17 00:00:00 2001 From: mattstam Date: Fri, 10 Jan 2025 10:51:27 -0800 Subject: [PATCH 2/3] fix --- crates/sdk/src/network/grpc.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/sdk/src/network/grpc.rs b/crates/sdk/src/network/grpc.rs index afe45e29b..2d68f1cbd 100644 --- a/crates/sdk/src/network/grpc.rs +++ b/crates/sdk/src/network/grpc.rs @@ -4,8 +4,8 @@ use tonic::transport::{ClientTlsConfig, Endpoint, Error}; /// Configures the endpoint for the gRPC client. /// /// Sets reasonable settings to handle timeouts and keep-alive. -pub fn configure_endpoint(addr: String) -> Result { - let mut endpoint = Endpoint::new(addr.clone())? +pub fn configure_endpoint(addr: &str) -> Result { + let mut endpoint = Endpoint::new(addr.to_string())? .timeout(Duration::from_secs(60)) .connect_timeout(Duration::from_secs(15)) .keep_alive_while_idle(true) From 25b9e03d63b4e6c57a3517f7ce9461a6541b8bbd Mon Sep 17 00:00:00 2001 From: mattstam Date: Fri, 10 Jan 2025 10:58:18 -0800 Subject: [PATCH 3/3] fix --- crates/sdk/src/network/client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/sdk/src/network/client.rs b/crates/sdk/src/network/client.rs index ecd53e323..eaa62b63c 100644 --- a/crates/sdk/src/network/client.rs +++ b/crates/sdk/src/network/client.rs @@ -259,7 +259,7 @@ impl NetworkClient { } pub(crate) async fn prover_network_client(&self) -> Result> { - let channel = grpc::configure_endpoint(self.rpc_url.clone())?.connect().await?; + let channel = grpc::configure_endpoint(&self.rpc_url)?.connect().await?; Ok(ProverNetworkClient::new(channel)) }