Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gg2001 committed Feb 13, 2025
1 parent f4f9fb4 commit 8ab122b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
15 changes: 11 additions & 4 deletions crates/sdk/src/network/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use sp1_core_machine::io::SP1Stdin;
use sp1_prover::{HashableKey, SP1VerifyingKey};
use tonic::{transport::Channel, Code};

use super::grpc;
use super::grpc::GrpcClient;
use super::retry::{self, RetryableRpc, DEFAULT_RETRY_TIMEOUT};
use super::utils::Signable;
use crate::network::proto::artifact::{
Expand All @@ -36,6 +36,7 @@ pub struct NetworkClient {
pub(crate) signer: PrivateKeySigner,
pub(crate) http: HttpClientWithMiddleware,
pub(crate) rpc_url: String,
pub(crate) rpc_client: GrpcClient,
}

#[async_trait]
Expand Down Expand Up @@ -75,7 +76,13 @@ impl NetworkClient {
.pool_idle_timeout(Duration::from_secs(240))
.build()
.unwrap();
Self { signer, http: client.into(), rpc_url: rpc_url.into() }
let rpc_url: String = rpc_url.into();
Self {
signer,
http: client.into(),
rpc_url: rpc_url.clone(),
rpc_client: GrpcClient::new(rpc_url),
}
}

/// Get the latest nonce for this account's address.
Expand Down Expand Up @@ -333,12 +340,12 @@ impl NetworkClient {
}

pub(crate) async fn prover_network_client(&self) -> Result<ProverNetworkClient<Channel>> {
let channel = grpc::configure_endpoint(&self.rpc_url)?.connect().await?;
let channel = self.rpc_client.get_channel().await?;
Ok(ProverNetworkClient::new(channel))
}

pub(crate) async fn artifact_store_client(&self) -> Result<ArtifactStoreClient<Channel>> {
let channel = grpc::configure_endpoint(&self.rpc_url)?.connect().await?;
let channel = self.rpc_client.get_channel().await?;
Ok(ArtifactStoreClient::new(channel))
}

Expand Down
30 changes: 29 additions & 1 deletion crates/sdk/src/network/grpc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::time::Duration;
use tonic::transport::{ClientTlsConfig, Endpoint, Error};
use tokio::sync::OnceCell;
use tonic::transport::{Channel, ClientTlsConfig, Endpoint, Error};

/// Configures the endpoint for the gRPC client.
///
Expand All @@ -22,3 +23,30 @@ pub fn configure_endpoint(addr: &str) -> Result<Endpoint, Error> {

Ok(endpoint)
}

/// Creates a reusable channel for gRPC connections
pub async fn create_persistent_channel(addr: &str) -> Result<Channel, Error> {
let endpoint = configure_endpoint(addr)?;
endpoint.connect().await
}

/// A thread-safe wrapper for managing a persistent gRPC channel
pub struct GrpcClient {
channel: OnceCell<Channel>,
addr: String,
}

impl GrpcClient {
pub fn new(addr: String) -> Self {
Self { channel: OnceCell::new(), addr }
}

/// Gets or initializes the channel
pub async fn get_channel(&self) -> Result<Channel, Error> {
let channel = self
.channel
.get_or_try_init(|| async { create_persistent_channel(&self.addr).await })
.await?;
Ok(channel.clone())
}
}

0 comments on commit 8ab122b

Please sign in to comment.