Skip to content
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

Do not assume server time is in sync with local machine time on rate limit path #1972

Merged
merged 2 commits into from
Oct 17, 2024

Conversation

holly-cummins
Copy link
Contributor

See discussion in #1909 and companion PR in #1971. On this test, I did try and cover the guard for missing dates with tests.

Description

Before submitting a PR:

  • Changes must not break binary backwards compatibility. If you are unclear on how to make the change you think is needed while maintaining backward compatibility, CONTRIBUTING.md for details.
  • Add JavaDocs and other comments explaining the behavior.
  • When adding or updating methods that fetch entities, add @link JavaDoc entries to the relevant documentation on https://docs.github.com/en/rest .
  • Add tests that cover any added or changed code. This generally requires capturing snapshot test data. See CONTRIBUTING.md for details.
  • Run mvn -D enable-ci clean install site locally. If this command doesn't succeed, your change will not pass CI.
  • Push your changes to a branch other than main. You will create your PR from that branch.

When creating a PR:

  • Fill in the "Description" above with clear summary of the changes. This includes:
    • If this PR fixes one or more issues, include "Fixes #" lines for each issue.
    • Provide links to relevant documentation on https://docs.github.com/en/rest where possible. If not including links, explain why not.
  • All lines of new code should be covered by tests as reported by code coverage. Any lines that are not covered must have PR comments explaining why they cannot be covered. For example, "Reaching this particular exception is hard and is not a particular common scenario."
  • Enable "Allow edits from maintainers".

@holly-cummins holly-cummins force-pushed the correct-rate-limit-wait-parsing branch 5 times, most recently from 0fd8887 to 2973e57 Compare October 9, 2024 20:57
@holly-cummins holly-cummins marked this pull request as draft October 10, 2024 11:16
@holly-cummins holly-cummins marked this pull request as ready for review October 10, 2024 17:27
Comment on lines +88 to +103
long parseWaitTime(GitHubConnectorResponse connectorResponse) {
String v = connectorResponse.header("X-RateLimit-Reset");
if (v == null)
return Duration.ofMinutes(1).toMillis(); // can't tell, return 1 min

return Math.max(1000, Long.parseLong(v) * 1000 - System.currentTimeMillis());
// Don't use ZonedDateTime.now(), because the local and remote server times may not be in sync
// Instead, we can take advantage of the Date field in the response to see what time the remote server
// thinks it is
String dateField = connectorResponse.header("Date");
ZonedDateTime now;
if (dateField != null) {
now = ZonedDateTime.parse(dateField, DateTimeFormatter.RFC_1123_DATE_TIME);
} else {
now = ZonedDateTime.now();
}
};
return Math.max(MINIMUM_RATE_LIMIT_RETRY_MILLIS, (Long.parseLong(v) - now.toInstant().getEpochSecond()) * 1000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now mostly the same as the abuse limit wait parse code you submitted. Would it make sense to extract this into a shared method? Perhaps even make it public so others could use it? Opened #1973 to track this.

Copy link

codecov bot commented Oct 14, 2024

Codecov Report

Attention: Patch coverage is 75.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 83.10%. Comparing base (0c9e195) to head (317f1cb).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...ava/org/kohsuke/github/GitHubRateLimitHandler.java 75.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##               main    #1972   +/-   ##
=========================================
  Coverage     83.09%   83.10%           
- Complexity     2329     2331    +2     
=========================================
  Files           231      231           
  Lines          7168     7172    +4     
  Branches        377      378    +1     
=========================================
+ Hits           5956     5960    +4     
  Misses          974      974           
  Partials        238      238           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bitwiseman bitwiseman merged commit 6ea075b into hub4j:main Oct 17, 2024
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants