Skip to content

Commit

Permalink
gateway proxy trait - elapsed time
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Oct 3, 2024
1 parent a8f382a commit 727d221
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
7 changes: 7 additions & 0 deletions sdk/core/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ pub trait GatewayRequest: Send {
}

pub trait GatewayAsyncService: Send {
/// Keeps track of elapsed time.
type Instant;

fn request<G>(
&self,
request: G,
Expand All @@ -95,4 +98,8 @@ pub trait GatewayAsyncService: Send {
G: GatewayRequest;

fn sleep(&self, millis: u64) -> impl std::future::Future<Output = ()> + Send;

fn now(&self) -> Self::Instant;

fn elapsed_seconds(&self, instant: &Self::Instant) -> f32;
}
10 changes: 3 additions & 7 deletions sdk/core/src/retrieve_tx_on_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::{
gateway::{GetTxInfo, GetTxProcessStatus},
};
use log::info;
use std::time::Instant;

use crate::gateway::GatewayAsyncService;

Expand All @@ -18,7 +17,7 @@ pub async fn retrieve_tx_on_network<GatewayProxy: GatewayAsyncService>(
) -> 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 {
Expand All @@ -30,7 +29,6 @@ pub async fn retrieve_tx_on_network<GatewayProxy: GatewayAsyncService>(
let transaction_info_with_results = proxy
.request(GetTxInfo::new(&tx_hash).with_results())
.await
// .get_transaction_info_with_results(&tx_hash)
.unwrap();

info!(
Expand Down Expand Up @@ -74,17 +72,15 @@ pub async fn retrieve_tx_on_network<GatewayProxy: GatewayAsyncService>(

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()
}
Expand Down
10 changes: 10 additions & 0 deletions sdk/http/src/gateway_http_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ impl GatewayHttpProxy {
}

impl GatewayAsyncService for GatewayHttpProxy {
type Instant = std::time::Instant;

fn request<G>(
&self,
request: G,
Expand All @@ -68,4 +70,12 @@ impl GatewayAsyncService for GatewayHttpProxy {
fn sleep(&self, millis: u64) -> impl std::future::Future<Output = ()> + 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()
}
}

0 comments on commit 727d221

Please sign in to comment.