Skip to content

Commit

Permalink
Retry with 429 status code when refreshing token
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Nov 6, 2021
1 parent 718eb99 commit 6616b26
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/drive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,13 @@ impl AliyunDrive {
return Ok(res);
}
Err(err) => {
let mut should_warn = true;
let mut should_retry = match err.downcast_ref::<reqwest::Error>() {
Some(e) => e.is_connect() || e.is_timeout(),
Some(e) => {
e.is_connect()
|| e.is_timeout()
|| matches!(e.status(), Some(StatusCode::TOO_MANY_REQUESTS))
}
None => false,
};
// retry if command line refresh_token is invalid but we also have
Expand All @@ -186,10 +191,14 @@ impl AliyunDrive {
if !should_retry && &refresh_token != refresh_token_from_file {
refresh_token = refresh_token_from_file.trim().to_string();
should_retry = true;
// don't warn if we are gonna try refresh_token from file
should_warn = false;
}
}
if should_retry {
warn!(error = %err, "refresh token failed, will wait and retry");
if should_warn {
warn!(error = %err, "refresh token failed, will wait and retry");
}
last_err = Some(err);
time::sleep(Duration::from_secs(1)).await;
continue;
Expand Down

0 comments on commit 6616b26

Please sign in to comment.