From 05aa17c0c77799d8663e8a4b210cf8f022294680 Mon Sep 17 00:00:00 2001 From: Itay Tsabary Date: Sat, 16 Nov 2024 22:34:21 +0200 Subject: [PATCH] chore(sequencer_infra): add component client trait for remote client commit-id:5b800b4d --- .../remote_component_client.rs | 51 ++++++++++++------- .../remote_component_client_server_test.rs | 1 + 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/crates/starknet_sequencer_infra/src/component_client/remote_component_client.rs b/crates/starknet_sequencer_infra/src/component_client/remote_component_client.rs index 395c467e74..87e021f87c 100644 --- a/crates/starknet_sequencer_infra/src/component_client/remote_component_client.rs +++ b/crates/starknet_sequencer_infra/src/component_client/remote_component_client.rs @@ -4,6 +4,7 @@ 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}; @@ -11,7 +12,7 @@ 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 @@ -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)] @@ -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 where @@ -106,22 +110,6 @@ where Self { uri, client, config, _req: PhantomData, _res: PhantomData } } - pub async fn send(&self, component_request: Request) -> ClientResult { - // 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 { HyperRequest::post(self.uri.clone()) .header(CONTENT_TYPE, APPLICATION_OCTET_STREAM) @@ -150,6 +138,31 @@ where } } +// TODO(Tsabary): remove clone trait from request and response, throughout. +#[async_trait] +impl ComponentClient + for RemoteComponentClient +where + Request: Send + Sync + Serialize + DeserializeOwned + Clone + Debug, + Response: Send + Sync + Serialize + DeserializeOwned + Debug, +{ + async fn send(&self, component_request: Request) -> ClientResult { + // 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: HyperResponse) -> Result where Response: Serialize + DeserializeOwned + Debug, diff --git a/crates/starknet_sequencer_infra/src/tests/remote_component_client_server_test.rs b/crates/starknet_sequencer_infra/src/tests/remote_component_client_server_test.rs index 00b61c0c03..ba6c1c884f 100644 --- a/crates/starknet_sequencer_infra/src/tests/remote_component_client_server_test.rs +++ b/crates/starknet_sequencer_infra/src/tests/remote_component_client_server_test.rs @@ -22,6 +22,7 @@ use crate::component_client::{ RemoteComponentClient, }; use crate::component_definitions::{ + ComponentClient, ComponentRequestAndResponseSender, RemoteClientConfig, RemoteServerConfig,