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

chore(sequencer_infra): add component client trait for remote client #2085

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use std::net::IpAddr;
use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use hyper::body::to_bytes;
use hyper::header::CONTENT_TYPE;
use hyper::{Body, Client, Request as HyperRequest, Response as HyperResponse, StatusCode, Uri};
use serde::de::DeserializeOwned;
use serde::Serialize;

use super::definitions::{ClientError, ClientResult};
use crate::component_definitions::{RemoteClientConfig, APPLICATION_OCTET_STREAM};
use crate::component_definitions::{ComponentClient, RemoteClientConfig, APPLICATION_OCTET_STREAM};
use crate::serde_utils::BincodeSerdeWrapper;

/// The `RemoteComponentClient` struct is a generic client for sending component requests and
Expand All @@ -34,7 +35,10 @@ use crate::serde_utils::BincodeSerdeWrapper;
/// use serde::{Deserialize, Serialize};
///
/// use crate::starknet_sequencer_infra::component_client::RemoteComponentClient;
/// use crate::starknet_sequencer_infra::component_definitions::RemoteClientConfig;
/// use crate::starknet_sequencer_infra::component_definitions::{
/// ComponentClient,
/// RemoteClientConfig,
/// };
///
/// // Define your request and response types
/// #[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -72,7 +76,7 @@ use crate::serde_utils::BincodeSerdeWrapper;
///
/// # Notes
/// - The `RemoteComponentClient` struct is designed to work in an asynchronous environment,
/// utilizing Tokio's async runtime and hyper framwork to send HTTP requests and receive HTTP
/// utilizing Tokio's async runtime and hyper framework to send HTTP requests and receive HTTP
/// responses.
pub struct RemoteComponentClient<Request, Response>
where
Expand Down Expand Up @@ -106,22 +110,6 @@ where
Self { uri, client, config, _req: PhantomData, _res: PhantomData }
}

pub async fn send(&self, component_request: Request) -> ClientResult<Response> {
// Construct and request, and send it up to 'max_retries' times. Return if received a
// successful response.
for _ in 0..self.config.retries {
let http_request = self.construct_http_request(component_request.clone());
let res = self.try_send(http_request).await;
if res.is_ok() {
return res;
}
}
// Construct and send the request, return the received response regardless whether it
// successful or not.
let http_request = self.construct_http_request(component_request);
self.try_send(http_request).await
}

fn construct_http_request(&self, component_request: Request) -> HyperRequest<Body> {
HyperRequest::post(self.uri.clone())
.header(CONTENT_TYPE, APPLICATION_OCTET_STREAM)
Expand Down Expand Up @@ -150,6 +138,31 @@ where
}
}

// TODO(Tsabary): remove clone trait from request and response, throughout.
#[async_trait]
impl<Request, Response> ComponentClient<Request, Response>
for RemoteComponentClient<Request, Response>
where
Request: Send + Sync + Serialize + DeserializeOwned + Clone + Debug,
Response: Send + Sync + Serialize + DeserializeOwned + Debug,
{
async fn send(&self, component_request: Request) -> ClientResult<Response> {
// Construct and request, and send it up to 'max_retries' times. Return if received a
// successful response.
for _ in 0..self.config.retries {
let http_request = self.construct_http_request(component_request.clone());
let res = self.try_send(http_request).await;
if res.is_ok() {
return res;
}
}
// Construct and send the request, return the received response regardless whether it
// successful or not.
let http_request = self.construct_http_request(component_request);
self.try_send(http_request).await
}
}

async fn get_response_body<Response>(response: HyperResponse<Body>) -> Result<Response, ClientError>
where
Response: Serialize + DeserializeOwned + Debug,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::component_client::{
RemoteComponentClient,
};
use crate::component_definitions::{
ComponentClient,
ComponentRequestAndResponseSender,
RemoteClientConfig,
RemoteServerConfig,
Expand Down
Loading