Skip to content

Commit 69640d6

Browse files
committed
chore(deps): remove tokio dependency and use backon for exponential backoff
1 parent 281696d commit 69640d6

File tree

4 files changed

+22
-35
lines changed

4 files changed

+22
-35
lines changed

core/Cargo.lock

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

core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ services-http = ["dep:opendal-service-http"]
147147
services-huggingface = ["dep:opendal-service-huggingface"]
148148
services-huggingface-xet = [
149149
"dep:opendal-service-huggingface",
150-
"opendal-service-huggingface?/xet",
150+
"opendal-service-huggingface/xet",
151151
]
152152
services-ipfs = ["dep:opendal-service-ipfs"]
153153
services-ipmfs = ["dep:opendal-service-ipmfs"]

core/services/huggingface/Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@ xet = [
3737
"dep:xet-data",
3838
"dep:cas_types",
3939
"dep:xet-utils",
40-
"tokio/sync",
41-
"tokio/rt",
4240
"dep:futures",
4341
"dep:async-trait",
4442
]
4543

4644
[dependencies]
45+
backon = "1.6"
4746
base64 = { workspace = true }
4847
bytes = { workspace = true }
4948
http = { workspace = true }
@@ -54,8 +53,6 @@ serde = { workspace = true, features = ["derive"] }
5453
serde_json = { workspace = true }
5554
sha2 = "0.10"
5655
tempfile = "3"
57-
tokio = { workspace = true, features = ["time"] }
58-
5956
# XET storage protocol support (optional)
6057
async-trait = { version = "0.1", optional = true }
6158
cas_types = { git = "https://github.com/kszucs/xet-core", branch = "download_bytes", optional = true }

core/services/huggingface/src/core.rs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use std::fmt::Debug;
1919
use std::sync::Arc;
2020

21+
use backon::ExponentialBuilder;
22+
use backon::Retryable;
2123
use bytes::Buf;
2224
use bytes::Bytes;
2325
use http::Request;
@@ -237,43 +239,30 @@ impl HfCore {
237239
self.repo.uri(&self.root, path)
238240
}
239241

240-
/// Exponential backoff: 200ms, 400ms, 800ms, … capped at ~6s.
241-
async fn backoff(attempt: usize) {
242-
let millis = 200u64 * (1u64 << attempt.min(5));
243-
tokio::time::sleep(std::time::Duration::from_millis(millis)).await;
244-
}
245-
246242
/// Send a request with retries, returning the successful response.
247243
///
248244
/// Retries on commit conflicts (HTTP 412) and transient server errors
249245
/// (HTTP 5xx) up to `self.max_retries` attempts with exponential backoff.
250246
pub(super) async fn send(&self, req: Request<Buffer>) -> Result<Response<Buffer>> {
247+
let backoff = ExponentialBuilder::default()
248+
.with_min_delay(std::time::Duration::from_millis(200))
249+
.with_max_delay(std::time::Duration::from_millis(6400))
250+
.with_max_times(self.max_retries.saturating_sub(1));
251251
let client = self.info.http_client();
252-
let mut attempt = 0;
253-
loop {
254-
match client.send(req.clone()).await {
255-
Ok(resp) if resp.status().is_success() => {
256-
return Ok(resp);
257-
}
258-
Ok(resp) => {
259-
attempt += 1;
260-
let err = parse_error(resp);
261-
let retryable =
262-
err.kind() == ErrorKind::ConditionNotMatch || err.is_temporary();
263-
if attempt >= self.max_retries || !retryable {
264-
return Err(err);
265-
}
266-
Self::backoff(attempt).await;
267-
}
268-
Err(err) => {
269-
attempt += 1;
270-
if attempt >= self.max_retries || !err.is_temporary() {
271-
return Err(err);
272-
}
273-
Self::backoff(attempt).await;
274-
}
252+
253+
let send_once = || async {
254+
let resp = client.send(req.clone()).await?;
255+
if resp.status().is_success() {
256+
Ok(resp)
257+
} else {
258+
Err(parse_error(resp))
275259
}
276-
}
260+
};
261+
262+
send_once
263+
.retry(backoff)
264+
.when(|e: &Error| e.kind() == ErrorKind::ConditionNotMatch || e.is_temporary())
265+
.await
277266
}
278267

279268
/// Send a request, check for success, and deserialize the JSON response.

0 commit comments

Comments
 (0)