Skip to content

Commit

Permalink
chore: replace backoff by backon library (#4984)
Browse files Browse the repository at this point in the history
Co-authored-by: hanabi1224 <harlowmoo@gmail.com>
  • Loading branch information
ruseinov and hanabi1224 authored Nov 27, 2024
1 parent e806ecd commit 7ea29fa
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 38 deletions.
15 changes: 6 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async-fs = "2"
async-trait = "0.1"
asynchronous-codec = "0.7"
axum = "0.7"
backoff = { version = "0.4", features = ['tokio'] }
backon = "1.2.0"
base64 = "0.22"
bigdecimal = "=0.4.2" # TODO(forest): https://github.com/ChainSafe/forest/issues/4035
blake2b_simd = "1.0"
Expand Down
4 changes: 0 additions & 4 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ ignore = [
# Instant is unmaintained, but there is a replacement in
# the pipeline - see
# https://github.com/libp2p/rust-libp2p/pull/5674
# We will likele need to get rid of `backoff` dependency
# in favour of something that is maintained too - see
# https://github.com/ihrwein/backoff/pull/70. Last commit
# to master was 3 years ago.
"RUSTSEC-2024-0384",
]
# If this is true, then cargo deny will use the git executable to fetch advisory database.
Expand Down
15 changes: 10 additions & 5 deletions src/beacon/drand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ use crate::shim::version::NetworkVersion;
use crate::utils::net::global_http_client;
use anyhow::Context as _;
use async_trait::async_trait;
use backon::{ExponentialBuilder, Retryable};
use bls_signatures::Serialize as _;
use itertools::Itertools as _;
use lru::LruCache;
use parking_lot::RwLock;
use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize};
use tracing::debug;
use url::Url;

/// Environmental Variable to ignore `Drand`. Lotus parallel is
Expand Down Expand Up @@ -378,12 +380,15 @@ impl Beacon for DrandBeacon {
anyhow::Ok(server.join(&format!("{}/public/{round}", self.hash))?)
})
.try_collect()?;
Ok(
backoff::future::retry(backoff::ExponentialBackoff::default(), || async {
Ok(fetch_entry(urls.iter().cloned()).await?)
Ok((|| fetch_entry(urls.iter().cloned()))
.retry(ExponentialBuilder::default())
.notify(|err, dur| {
debug!(
"retrying fetch_entry {err} after {}",
humantime::format_duration(dur)
);
})
.await?,
)
.await?)
}
}
}
Expand Down
39 changes: 20 additions & 19 deletions src/utils/proofs_api/paramfetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::{
io::{self, ErrorKind},
path::{Path, PathBuf},
sync::Arc,
time::Duration,
};

use crate::{
Expand All @@ -22,7 +21,7 @@ use crate::{
},
};
use anyhow::{bail, Context};
use backoff::{future::retry, ExponentialBackoffBuilder};
use backon::{ExponentialBuilder, Retryable};
use futures::{stream::FuturesUnordered, AsyncWriteExt, TryStreamExt};
use tokio::fs::{self};
use tracing::{debug, info, warn};
Expand Down Expand Up @@ -156,16 +155,16 @@ async fn fetch_params_ipfs_gateway(path: &Path, info: &ParameterData) -> anyhow:
"Fetching param file {path} from {gateway}",
path = path.display()
);
let backoff = ExponentialBackoffBuilder::default()
// Up to 30 minutes for downloading the file. This may be drastic,
// but the gateway proved to be unreliable at times and we
// don't want to get stuck here. Better to fail fast and retry.
.with_max_elapsed_time(Some(Duration::from_secs(60 * 30)))
.build();
let result = retry(backoff, || async {
Ok(download_ipfs_file_trustlessly(&info.cid, &gateway, path).await?)
})
.await;
let result = (|| download_ipfs_file_trustlessly(&info.cid, &gateway, path))
.retry(ExponentialBuilder::default())
.notify(|err, dur| {
debug!(
"retrying download_ipfs_file_trustlessly {err} after {}",
humantime::format_duration(dur)
);
})
.await;

debug!(
"Done fetching param file {path} from {gateway}",
path = path.display(),
Expand All @@ -176,13 +175,15 @@ async fn fetch_params_ipfs_gateway(path: &Path, info: &ParameterData) -> anyhow:
/// Downloads the parameter file from Cloudflare R2 to the given path. It wraps the [`download_from_cloudflare`] function with a retry and timeout mechanisms.
async fn fetch_params_cloudflare(name: &str, path: &Path) -> anyhow::Result<()> {
info!("Fetching param file {name} from Cloudflare R2 {CLOUDFLARE_PROOF_PARAMETER_DOMAIN}");
let backoff = ExponentialBackoffBuilder::default()
.with_max_elapsed_time(Some(Duration::from_secs(60 * 30)))
.build();
let result = retry(backoff, || async {
Ok(download_from_cloudflare(name, path).await?)
})
.await;
let result = (|| download_from_cloudflare(name, path))
.retry(ExponentialBuilder::default())
.notify(|err, dur| {
debug!(
"retrying download_from_cloudflare {err} after {}",
humantime::format_duration(dur)
);
})
.await;
debug!(
"Done fetching param file {} from Cloudflare",
path.display()
Expand Down

0 comments on commit 7ea29fa

Please sign in to comment.