-
Notifications
You must be signed in to change notification settings - Fork 361
Retry 429s after waiting a specified time #4689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pandaninjas
wants to merge
3
commits into
modrinth:main
Choose a base branch
from
pandaninjas:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -5,14 +5,15 @@ use crate::LAUNCHER_USER_AGENT; | |
| use crate::event::LoadingBarId; | ||
| use crate::event::emit::emit_loading; | ||
| use bytes::Bytes; | ||
| use chrono::DateTime; | ||
| use reqwest::Method; | ||
| use serde::de::DeserializeOwned; | ||
| use std::ffi::OsStr; | ||
| use std::path::{Path, PathBuf}; | ||
| use std::sync::LazyLock; | ||
| use std::time::{self}; | ||
| use std::time::{self, Duration}; | ||
| use tokio::sync::Semaphore; | ||
| use tokio::{fs::File, io::AsyncWriteExt}; | ||
| use tokio::{fs::File, io::AsyncWriteExt, time::sleep}; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct IoSemaphore(pub Semaphore); | ||
|
|
@@ -77,13 +78,14 @@ pub async fn fetch_advanced( | |
| exec: impl sqlx::Executor<'_, Database = sqlx::Sqlite>, | ||
| ) -> crate::Result<Bytes> { | ||
| let _permit = semaphore.0.acquire().await?; | ||
| let is_modrinth = url.starts_with("https://cdn.modrinth.com") | ||
| || url.starts_with(env!("MODRINTH_API_URL")) | ||
| || url.starts_with(env!("MODRINTH_API_URL_V3")); | ||
|
|
||
| let creds = if header | ||
| .as_ref() | ||
| .is_none_or(|x| &*x.0.to_lowercase() != "authorization") | ||
| && (url.starts_with("https://cdn.modrinth.com") | ||
| || url.starts_with(env!("MODRINTH_API_URL")) | ||
| || url.starts_with(env!("MODRINTH_API_URL_V3"))) | ||
| && is_modrinth | ||
| { | ||
| crate::state::ModrinthCredentials::get_active(exec).await? | ||
| } else { | ||
|
|
@@ -116,6 +118,40 @@ pub async fn fetch_advanced( | |
| || resp.status().is_server_error() | ||
| { | ||
| let backup_error = resp.error_for_status_ref().unwrap_err(); | ||
| if resp.status() == 429 && attempt <= FETCH_ATTEMPTS { | ||
| if is_modrinth // x-ratelimit-reset is not portable across different servers | ||
| && let Some(reset_header) = | ||
| resp.headers().get("X-Ratelimit-Reset") | ||
| && let Ok(seconds) = reset_header.to_str() | ||
| && let Ok(seconds) = seconds.parse::<u64>() | ||
| { | ||
| sleep(Duration::from_secs(seconds)).await; | ||
| continue; | ||
| } else if let Some(retry_header) = | ||
| resp.headers().get("Retry-After") | ||
| && let Ok(retry) = retry_header.to_str() | ||
| { | ||
| if let Ok(seconds) = retry.parse::<u64>() { | ||
| // when retry-after retruns a delay in seconds | ||
| sleep(Duration::from_secs(seconds)).await; | ||
| continue; | ||
| } else if let Ok(date) = | ||
| DateTime::parse_from_rfc2822(retry) | ||
| // when retry-after returns an http date | ||
| { | ||
| let now = chrono::Utc::now(); | ||
| // Convert now to the same timezone as date | ||
| let now_fixed = | ||
| now.with_timezone(date.offset()); | ||
| let wait_duration = date - now_fixed; | ||
| sleep(Duration::from_secs( | ||
| wait_duration.num_seconds() as u64, | ||
| )) | ||
|
Comment on lines
+147
to
+149
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use |
||
| .await; | ||
| continue; | ||
| } | ||
| } | ||
| } | ||
| if let Ok(error) = resp.json().await { | ||
| return Err(ErrorKind::LabrinthError(error).into()); | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make the HTTP status code explicit