Skip to content

Commit 352cce0

Browse files
Remove the backon dependency
1 parent e873b90 commit 352cce0

File tree

3 files changed

+47
-44
lines changed

3 files changed

+47
-44
lines changed

aggregation_mode/Cargo.lock

Lines changed: 1 addition & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aggregation_mode/proof_aggregator/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ reqwest = { version = "0.12" }
2121
ciborium = "=0.2.2"
2222
lambdaworks-crypto = { git = "https://github.com/lambdaclass/lambdaworks.git", rev = "5f8f2cfcc8a1a22f77e8dff2d581f1166eefb80b", features = ["serde"]}
2323
rayon = "1.10.0"
24-
backon = "1.2.0"
2524
sqlx = { version = "0.8", features = [ "runtime-tokio", "postgres", "uuid", "bigdecimal" ] }
2625

2726
# zkvms

aggregation_mode/proof_aggregator/src/backend/retry.rs

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use backon::ExponentialBuilder;
2-
use backon::Retryable;
31
use std::future::Future;
42
use std::time::Duration;
53

@@ -29,28 +27,58 @@ impl<E> RetryError<E> {
2927

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

32-
/// Supports retries only on async functions. See: https://docs.rs/backon/latest/backon/#retry-an-async-function
33-
/// Runs with `jitter: false`.
3430
pub async fn retry_function<FutureFn, Fut, T, E>(
35-
function: FutureFn,
36-
min_delay: u64,
31+
mut function: FutureFn,
32+
min_delay_ms: u64,
3733
factor: f32,
3834
max_times: usize,
39-
max_delay: u64,
35+
max_delay_seconds: u64,
4036
) -> Result<T, RetryError<E>>
4137
where
4238
Fut: Future<Output = Result<T, RetryError<E>>>,
4339
FutureFn: FnMut() -> Fut,
4440
{
45-
let backoff = ExponentialBuilder::default()
46-
.with_min_delay(Duration::from_millis(min_delay))
47-
.with_max_times(max_times)
48-
.with_factor(factor)
49-
.with_max_delay(Duration::from_secs(max_delay));
50-
51-
function
52-
.retry(backoff)
53-
.sleep(tokio::time::sleep)
54-
.when(|e| matches!(e, RetryError::Transient(_)))
55-
.await
41+
let mut delay = Duration::from_millis(min_delay_ms);
42+
43+
let factor = (factor as f64).max(1.0);
44+
45+
let mut attempt: usize = 0;
46+
47+
loop {
48+
match function().await {
49+
Ok(v) => return Ok(v),
50+
Err(RetryError::Permanent(e)) => return Err(RetryError::Permanent(e)),
51+
Err(RetryError::Transient(e)) => {
52+
if attempt >= max_times {
53+
return Err(RetryError::Transient(e));
54+
}
55+
56+
tokio::time::sleep(delay).await;
57+
58+
delay = next_backoff_delay(delay, max_delay_seconds, factor);
59+
60+
attempt += 1;
61+
}
62+
}
63+
}
64+
}
65+
66+
/// TODO: Replace with the one in aggregation_mode/db/src/orchestrator.rs, or use a common method.
67+
fn next_backoff_delay(current_delay: Duration, max_delay_seconds: u64, factor: f64) -> Duration {
68+
let max: Duration = Duration::from_secs(max_delay_seconds);
69+
// Defensive: factor should be >= 1.0 for backoff, we clamp it to avoid shrinking/NaN.
70+
71+
let scaled_secs = current_delay.as_secs_f64() * factor;
72+
let scaled_secs = if scaled_secs.is_finite() {
73+
scaled_secs
74+
} else {
75+
max.as_secs_f64()
76+
};
77+
78+
let scaled = Duration::from_secs_f64(scaled_secs);
79+
if scaled > max {
80+
max
81+
} else {
82+
scaled
83+
}
5684
}

0 commit comments

Comments
 (0)