Skip to content

Commit

Permalink
refactor: move retryable functions to batcher_retry
Browse files Browse the repository at this point in the history
  • Loading branch information
avilagaston9 committed Oct 23, 2024
1 parent c9c6e70 commit bb2d464
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 54 deletions.
5 changes: 3 additions & 2 deletions batcher/aligned-batcher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use dotenvy::dotenv;
use eth::service_manager::ServiceManager;
use ethers::contract::ContractError;
use ethers::signers::Signer;
use retry::{
use retry::batcher_retry::{
get_gas_price_retryable, get_user_balance_retryable, get_user_nonce_from_ethereum_retryable,
retry_function, user_balance_is_unlocked_retryable, RetryError,
user_balance_is_unlocked_retryable,
};
use retry::{retry_function, RetryError};
use types::batch_state::BatchState;
use types::user_state::UserState;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,7 @@
use backon::ExponentialBuilder;
use backon::Retryable;
use ethers::prelude::*;
use log::warn;
use std::{future::Future, time::Duration};

use crate::eth::payment_service::BatcherPaymentService;

#[derive(Debug)]
pub enum RetryError<E> {
Transient(E),
Permanent(E),
}

impl<E: std::fmt::Display> std::fmt::Display for RetryError<E> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
RetryError::Transient(e) => write!(f, "{}", e),
RetryError::Permanent(e) => write!(f, "{}", e),
}
}
}

impl<E> RetryError<E> {
pub fn inner(self) -> E {
match self {
RetryError::Transient(e) => e,
RetryError::Permanent(e) => e,
}
}
}

impl<E: std::fmt::Display> std::error::Error for RetryError<E> where E: std::fmt::Debug {}

pub async fn retry_function<FutureFn, Fut, T, E>(
function: FutureFn,
min_delay: u64,
factor: f32,
max_times: usize,
) -> Result<T, RetryError<E>>
where
Fut: Future<Output = Result<T, RetryError<E>>>,
FutureFn: FnMut() -> Fut,
{
let backoff = ExponentialBuilder::default()
.with_min_delay(Duration::from_millis(min_delay))
.with_max_times(max_times)
.with_factor(factor);

function
.retry(backoff)
.sleep(tokio::time::sleep)
.when(|e| matches!(e, RetryError::Transient(_)))
.await
}
use crate::{eth::payment_service::BatcherPaymentService, retry::RetryError};

pub async fn get_user_balance_retryable(
payment_service: &BatcherPaymentService,
Expand Down
53 changes: 53 additions & 0 deletions batcher/aligned-batcher/src/retry/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
pub mod batcher_retry;

use backon::ExponentialBuilder;
use backon::Retryable;
use std::{future::Future, time::Duration};

#[derive(Debug)]
pub enum RetryError<E> {
Transient(E),
Permanent(E),
}

impl<E: std::fmt::Display> std::fmt::Display for RetryError<E> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
RetryError::Transient(e) => write!(f, "{}", e),
RetryError::Permanent(e) => write!(f, "{}", e),
}
}
}

impl<E> RetryError<E> {
pub fn inner(self) -> E {
match self {
RetryError::Transient(e) => e,
RetryError::Permanent(e) => e,
}
}
}

impl<E: std::fmt::Display> std::error::Error for RetryError<E> where E: std::fmt::Debug {}

pub async fn retry_function<FutureFn, Fut, T, E>(
function: FutureFn,
min_delay: u64,
factor: f32,
max_times: usize,
) -> Result<T, RetryError<E>>
where
Fut: Future<Output = Result<T, RetryError<E>>>,
FutureFn: FnMut() -> Fut,
{
let backoff = ExponentialBuilder::default()
.with_min_delay(Duration::from_millis(min_delay))
.with_max_times(max_times)
.with_factor(factor);

function
.retry(backoff)
.sleep(tokio::time::sleep)
.when(|e| matches!(e, RetryError::Transient(_)))
.await
}

0 comments on commit bb2d464

Please sign in to comment.