-
Notifications
You must be signed in to change notification settings - Fork 9
feat: handle rate limiting #71
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
tziemek
wants to merge
5
commits into
scm-rs:main
Choose a base branch
from
tziemek:feature/respect-rate-limiting
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b1a2612
feat: gracefully handle rate limiting
tziemek 3a7b20b
fix: allow retry header to be a HTTP-date as defined in RFC7231
tziemek db0159b
fix: use rstest to simplify some fetcher tests
tziemek 900c683
fix: use adjustable retry delays when sending data
tziemek 56939e1
fix: bump minor version to 0.16.0
tziemek 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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| use std::time::Duration; | ||
|
|
||
| use reqwest::{Response, StatusCode, header}; | ||
|
|
||
| pub enum RetryAfter { | ||
| Duration(Duration), | ||
| After(std::time::SystemTime), | ||
| } | ||
|
|
||
| /// Parse Retry-After header value. | ||
| /// Supports both delay-seconds (numeric) and HTTP-date formats as per RFC7231 | ||
| fn parse_retry_after(value: &str) -> Option<RetryAfter> { | ||
| // Try parsing as seconds (numeric) | ||
| if let Ok(seconds) = value.parse::<u64>() { | ||
| return Some(RetryAfter::Duration(Duration::from_secs(seconds))); | ||
| } | ||
|
|
||
| // Try parsing as HTTP-date (RFC7231 format) | ||
| // Common formats: "Sun, 06 Nov 1994 08:49:37 GMT" (IMF-fixdate preferred) | ||
| if let Ok(datetime) = httpdate::parse_http_date(value) { | ||
| return Some(RetryAfter::After(datetime)); | ||
| } | ||
|
|
||
| None | ||
| } | ||
|
|
||
| pub fn calculate_retry_after_from_response_header( | ||
| response: &Response, | ||
| default_duration: Duration, | ||
| ) -> Option<Duration> { | ||
| if response.status() == StatusCode::TOO_MANY_REQUESTS { | ||
| let retry_after = response | ||
| .headers() | ||
| .get(header::RETRY_AFTER) | ||
| .and_then(|v| v.to_str().ok()) | ||
| .and_then(parse_retry_after) | ||
| .and_then(|retry| match retry { | ||
| RetryAfter::Duration(d) => Some(d), | ||
| RetryAfter::After(after) => { | ||
| // Calculate duration from now until the specified time | ||
| std::time::SystemTime::now() | ||
| .duration_since(std::time::UNIX_EPOCH) | ||
| .ok() | ||
| .and_then(|now| { | ||
| after | ||
| .duration_since(std::time::UNIX_EPOCH) | ||
| .ok() | ||
| .and_then(|target| target.checked_sub(now)) | ||
| }) | ||
| } | ||
| }) | ||
| .unwrap_or(default_duration); | ||
| return Some(retry_after); | ||
| } | ||
| None | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.