diff --git a/crates/sdk/src/network/client.rs b/crates/sdk/src/network/client.rs index ae0b405c6..eaa62b63c 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)?.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..2d68f1cbd --- /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: &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) + .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;