From 8b4b9602f98d2a71b015ac28f67358433817fe7d Mon Sep 17 00:00:00 2001 From: Itay Tsabary Date: Sat, 16 Nov 2024 22:59:14 +0200 Subject: [PATCH] chore(sequencer_infra): restructure code to avoid clone trait bound commit-id:bdbb68f7 --- .../remote_component_client.rs | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 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 748f66eb04..96c29f7579 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 @@ -89,7 +89,7 @@ where impl RemoteComponentClient where - Request: Serialize + DeserializeOwned + Debug + Clone, + Request: Serialize + DeserializeOwned + Debug, Response: Serialize + DeserializeOwned + Debug, { pub fn new(config: RemoteClientConfig) -> Self { @@ -107,14 +107,10 @@ where Self { uri, client, config, _req: PhantomData, _res: PhantomData } } - fn construct_http_request(&self, component_request: Request) -> HyperRequest { + fn construct_http_request(&self, serialized_request: Vec) -> HyperRequest { HyperRequest::post(self.uri.clone()) .header(CONTENT_TYPE, APPLICATION_OCTET_STREAM) - .body(Body::from( - BincodeSerdeWrapper::new(component_request) - .to_bincode() - .expect("Request serialization should succeed"), - )) + .body(Body::from(serialized_request)) .expect("Request building should succeed") } @@ -139,23 +135,29 @@ where impl ComponentClient for RemoteComponentClient where - Request: Send + Sync + Serialize + DeserializeOwned + Clone + Debug, - Response: Send + Sync + Serialize + DeserializeOwned + Clone + Debug, + Request: Send + Sync + Serialize + DeserializeOwned + 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()); + // Serialize the request. + let serialized_request = BincodeSerdeWrapper::new(component_request) + .to_bincode() + .expect("Request serialization should succeed"); + + // Construct the request, and send it up to 'max_retries + 1' times. Return if received a + // successful response, or the last response if all attempts failed. + let max_attempts = self.config.retries + 1; + for attempt in 0..max_attempts { + let http_request = self.construct_http_request(serialized_request.clone()); let res = self.try_send(http_request).await; if res.is_ok() { return res; } + if attempt == max_attempts - 1 { + 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 + unreachable!("Guaranteed to return a response before reaching this point."); } }