-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(install): retry downloads of registry info / tarballs (#26278)
Fixes #26085. Adds a basic retry utility with some defaults, starts off with a 100ms wait, then 250ms, then 500ms I've applied the retry in the http client, reusing an existing function, so this also applies to retrying downloads of deno binaries in `upgrade` and `compile`. I can make a separate function that doesn't retry so this doesn't affect `upgrade` and `compile`, but it seemed desirable to have retries there too, so I left it in.
- Loading branch information
1 parent
40b1c42
commit 7c3c13c
Showing
7 changed files
with
68 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
use std::future::Future; | ||
use std::time::Duration; | ||
|
||
pub fn retry< | ||
F: FnMut() -> Fut, | ||
T, | ||
E, | ||
Fut: Future<Output = Result<T, E>>, | ||
ShouldRetry: FnMut(&E) -> bool, | ||
>( | ||
mut f: F, | ||
mut should_retry: ShouldRetry, | ||
) -> impl Future<Output = Result<T, E>> { | ||
const WAITS: [Duration; 3] = [ | ||
Duration::from_millis(100), | ||
Duration::from_millis(250), | ||
Duration::from_millis(500), | ||
]; | ||
|
||
let mut waits = WAITS.into_iter(); | ||
async move { | ||
let mut first_result = None; | ||
loop { | ||
let result = f().await; | ||
match result { | ||
Ok(r) => return Ok(r), | ||
Err(e) if !should_retry(&e) => return Err(e), | ||
_ => {} | ||
} | ||
if first_result.is_none() { | ||
first_result = Some(result); | ||
} | ||
let Some(wait) = waits.next() else { | ||
return first_result.unwrap(); | ||
}; | ||
tokio::time::sleep(wait).await; | ||
} | ||
} | ||
} |