From 727d2211c66b4fe455994540962d26c379f54fd3 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Thu, 3 Oct 2024 13:49:00 +0300 Subject: [PATCH] gateway proxy trait - elapsed time --- sdk/core/src/gateway.rs | 7 +++++++ sdk/core/src/retrieve_tx_on_network.rs | 10 +++------- sdk/http/src/gateway_http_proxy.rs | 10 ++++++++++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/sdk/core/src/gateway.rs b/sdk/core/src/gateway.rs index 44d8a22d52..b158a0ef2b 100644 --- a/sdk/core/src/gateway.rs +++ b/sdk/core/src/gateway.rs @@ -87,6 +87,9 @@ pub trait GatewayRequest: Send { } pub trait GatewayAsyncService: Send { + /// Keeps track of elapsed time. + type Instant; + fn request( &self, request: G, @@ -95,4 +98,8 @@ pub trait GatewayAsyncService: Send { G: GatewayRequest; fn sleep(&self, millis: u64) -> impl std::future::Future + Send; + + fn now(&self) -> Self::Instant; + + fn elapsed_seconds(&self, instant: &Self::Instant) -> f32; } diff --git a/sdk/core/src/retrieve_tx_on_network.rs b/sdk/core/src/retrieve_tx_on_network.rs index 7bf873cfe0..c68a649d9c 100644 --- a/sdk/core/src/retrieve_tx_on_network.rs +++ b/sdk/core/src/retrieve_tx_on_network.rs @@ -3,7 +3,6 @@ use crate::{ gateway::{GetTxInfo, GetTxProcessStatus}, }; use log::info; -use std::time::Instant; use crate::gateway::GatewayAsyncService; @@ -18,7 +17,7 @@ pub async fn retrieve_tx_on_network( ) -> TransactionOnNetwork { let mut retries = 0; let mut backoff_delay = INITIAL_BACKOFF_DELAY; - let start_time = Instant::now(); + let start_time = proxy.now(); loop { match proxy.request(GetTxProcessStatus::new(&tx_hash)).await { @@ -30,7 +29,6 @@ pub async fn retrieve_tx_on_network( let transaction_info_with_results = proxy .request(GetTxInfo::new(&tx_hash).with_results()) .await - // .get_transaction_info_with_results(&tx_hash) .unwrap(); info!( @@ -74,17 +72,15 @@ pub async fn retrieve_tx_on_network( let backoff_time = backoff_delay.min(MAX_BACKOFF_DELAY); proxy.sleep(backoff_time).await; - // tokio::time::sleep(backoff_time).await; backoff_delay *= 2; // exponential backoff }, } } // retries have been exhausted - let elapsed_time = start_time.elapsed(); println!( - "Fetching transaction failed and retries exhausted, returning default transaction. Total elapsed time: {:?}", - elapsed_time + "Fetching transaction failed and retries exhausted, returning default transaction. Total elapsed time: {:?}s", + proxy.elapsed_seconds(&start_time) ); TransactionOnNetwork::default() } diff --git a/sdk/http/src/gateway_http_proxy.rs b/sdk/http/src/gateway_http_proxy.rs index ec8d625e73..1417db3db5 100644 --- a/sdk/http/src/gateway_http_proxy.rs +++ b/sdk/http/src/gateway_http_proxy.rs @@ -55,6 +55,8 @@ impl GatewayHttpProxy { } impl GatewayAsyncService for GatewayHttpProxy { + type Instant = std::time::Instant; + fn request( &self, request: G, @@ -68,4 +70,12 @@ impl GatewayAsyncService for GatewayHttpProxy { fn sleep(&self, millis: u64) -> impl std::future::Future + Send { tokio::time::sleep(Duration::from_millis(millis)) } + + fn now(&self) -> Self::Instant { + std::time::Instant::now() + } + + fn elapsed_seconds(&self, instant: &Self::Instant) -> f32 { + instant.elapsed().as_secs_f32() + } }