Skip to content

Commit 0fd8887

Browse files
committed
Do not assume server time is in sync with local machine time on rate limit path
1 parent 768c715 commit 0fd8887

File tree

8 files changed

+390
-10
lines changed

8 files changed

+390
-10
lines changed

src/main/java/org/kohsuke/github/GitHubRateLimitHandler.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
import java.io.IOException;
77
import java.io.InterruptedIOException;
8+
import java.time.Duration;
9+
import java.time.ZonedDateTime;
10+
import java.time.format.DateTimeFormatter;
811

912
import javax.annotation.Nonnull;
1013

@@ -21,6 +24,11 @@
2124
*/
2225
public abstract class GitHubRateLimitHandler extends GitHubConnectorResponseErrorHandler {
2326

27+
/**
28+
* On a wait, even if the response suggests a very short wait, wait for a minimum duration.
29+
*/
30+
private static final int MINIMUM_RATE_LIMIT_RETRY_MILLIS = 1000;
31+
2432
/**
2533
* Create default GitHubRateLimitHandler instance
2634
*/
@@ -71,15 +79,29 @@ public void onError(GitHubConnectorResponse connectorResponse) throws IOExceptio
7179
throw (InterruptedIOException) new InterruptedIOException().initCause(ex);
7280
}
7381
}
82+
};
7483

75-
private long parseWaitTime(GitHubConnectorResponse connectorResponse) {
76-
String v = connectorResponse.header("X-RateLimit-Reset");
77-
if (v == null)
78-
return 60 * 1000; // can't tell, return 1 min
84+
/*
85+
* Exposed for testability. Given an http response, find the rate limit reset header field and parse it. If no
86+
* header is found, wait for a reasonably amount of time.
87+
*/
88+
long parseWaitTime(GitHubConnectorResponse connectorResponse) {
89+
String v = connectorResponse.header("X-RateLimit-Reset");
90+
if (v == null)
91+
return Duration.ofMinutes(1).toMillis(); // can't tell, return 1 min
7992

80-
return Math.max(1000, Long.parseLong(v) * 1000 - System.currentTimeMillis());
93+
// Don't use ZonedDateTime.now(), because the local and remote server times may not be in sync
94+
// Instead, we can take advantage of the Date field in the response to see what time the remote server
95+
// thinks it is
96+
String dateField = connectorResponse.header("Date");
97+
ZonedDateTime now;
98+
if (dateField != null) {
99+
now = ZonedDateTime.parse(dateField, DateTimeFormatter.RFC_1123_DATE_TIME);
100+
} else {
101+
now = ZonedDateTime.now();
81102
}
82-
};
103+
return Math.max(MINIMUM_RATE_LIMIT_RETRY_MILLIS, (Long.parseLong(v) - now.toInstant().getEpochSecond()) * 1000);
104+
}
83105

84106
/**
85107
* Fail immediately.

src/test/java/org/kohsuke/github/RateLimitHandlerTest.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.kohsuke.github;
22

33
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
4+
import org.jetbrains.annotations.NotNull;
45
import org.junit.Test;
56
import org.kohsuke.github.connector.GitHubConnectorResponse;
67

@@ -117,16 +118,55 @@ public void testHandler_HttpStatus_Fail() throws Exception {
117118
/**
118119
* Test handler wait.
119120
*
120-
* @throws Exception
121+
* @throws IOException
121122
* the exception
122123
*/
123124
@Test
124-
public void testHandler_Wait() throws Exception {
125+
public void testHandler_Wait() throws IOException {
126+
// Customized response that templates the date to keep things working
127+
snapshotNotAllowed();
128+
129+
gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl())
130+
.withRateLimitHandler(new GitHubRateLimitHandler() {
131+
132+
@Override
133+
public void onError(@NotNull GitHubConnectorResponse connectorResponse) throws IOException {
134+
long waitTime = GitHubRateLimitHandler.WAIT.parseWaitTime(connectorResponse);
135+
assertThat(waitTime, equalTo(3 * 1000l));
136+
137+
GitHubAbuseLimitHandler.WAIT.onError(connectorResponse);
138+
}
139+
})
140+
.build();
141+
142+
gitHub.getMyself();
143+
assertThat(mockGitHub.getRequestCount(), equalTo(1));
144+
145+
getTempRepository();
146+
assertThat(mockGitHub.getRequestCount(), equalTo(3));
147+
}
148+
149+
/**
150+
* Test the wait logic in the case where the "Date" header field is missing from the response.
151+
*
152+
* @throws IOException if the code under test throws that exception
153+
*/
154+
@Test
155+
public void testHandler_Wait_Missing_Date_Header() throws IOException {
125156
// Customized response that templates the date to keep things working
126157
snapshotNotAllowed();
127158

128159
gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl())
129-
.withRateLimitHandler(GitHubRateLimitHandler.WAIT)
160+
.withRateLimitHandler(new GitHubRateLimitHandler() {
161+
162+
@Override
163+
public void onError(@NotNull GitHubConnectorResponse connectorResponse) throws IOException {
164+
long waitTime = GitHubRateLimitHandler.WAIT.parseWaitTime(connectorResponse);
165+
assertThat(waitTime, equalTo(3 * 1000l));
166+
167+
GitHubAbuseLimitHandler.WAIT.onError(connectorResponse);
168+
}
169+
})
130170
.build();
131171

132172
gitHub.getMyself();

src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait/mappings/2-r_h_t_Wait.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"Status": "403 Forbidden",
2121
"X-RateLimit-Limit": "5000",
2222
"X-RateLimit-Remaining": "0",
23-
"X-RateLimit-Reset": "{{testStartDate offset='3 seconds' format='unix'}}",
23+
"X-RateLimit-Reset": "{{now offset='3 seconds' format='unix'}}",
2424
"Cache-Control": "private, max-age=60, s-maxage=60",
2525
"Vary": [
2626
"Accept, Authorization, Cookie, X-GitHub-OTP",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"login": "bitwiseman",
3+
"id": 1958953,
4+
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
5+
"avatar_url": "https://avatars3.githubusercontent.com/u/1958953?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/bitwiseman",
8+
"html_url": "https://github.com/bitwiseman",
9+
"followers_url": "https://api.github.com/users/bitwiseman/followers",
10+
"following_url": "https://api.github.com/users/bitwiseman/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/bitwiseman/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/bitwiseman/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/bitwiseman/subscriptions",
14+
"organizations_url": "https://api.github.com/users/bitwiseman/orgs",
15+
"repos_url": "https://api.github.com/users/bitwiseman/repos",
16+
"events_url": "https://api.github.com/users/bitwiseman/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/bitwiseman/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "Liam Newman",
21+
"company": "Cloudbees, Inc.",
22+
"blog": "",
23+
"location": "Seattle, WA, USA",
24+
"email": "[email protected]",
25+
"hireable": null,
26+
"bio": "https://twitter.com/bitwiseman",
27+
"public_repos": 181,
28+
"public_gists": 7,
29+
"followers": 146,
30+
"following": 9,
31+
"created_at": "2012-07-11T20:38:33Z",
32+
"updated_at": "2020-02-06T17:29:39Z",
33+
"private_gists": 8,
34+
"total_private_repos": 10,
35+
"owned_private_repos": 0,
36+
"disk_usage": 33697,
37+
"collaborators": 0,
38+
"two_factor_authentication": true,
39+
"plan": {
40+
"name": "free",
41+
"space": 976562499,
42+
"collaborators": 0,
43+
"private_repos": 10000
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{
2+
"id": 238757196,
3+
"node_id": "MDEwOlJlcG9zaXRvcnkyMzg3NTcxOTY=",
4+
"name": "temp-testHandler_Wait",
5+
"full_name": "hub4j-test-org/temp-testHandler_Wait",
6+
"private": false,
7+
"owner": {
8+
"login": "hub4j-test-org",
9+
"id": 7544739,
10+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
11+
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
12+
"gravatar_id": "",
13+
"url": "https://api.github.com/users/hub4j-test-org",
14+
"html_url": "https://github.com/hub4j-test-org",
15+
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
16+
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
17+
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
18+
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
19+
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
20+
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
21+
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
22+
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
23+
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
24+
"type": "Organization",
25+
"site_admin": false
26+
},
27+
"html_url": "https://github.com/hub4j-test-org/temp-testHandler_Wait",
28+
"description": "A test repository for testing the github-api project: temp-testHandler_Wait",
29+
"fork": false,
30+
"url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait",
31+
"forks_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/forks",
32+
"keys_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/keys{/key_id}",
33+
"collaborators_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/collaborators{/collaborator}",
34+
"teams_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/teams",
35+
"hooks_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/hooks",
36+
"issue_events_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/issues/events{/number}",
37+
"events_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/events",
38+
"assignees_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/assignees{/user}",
39+
"branches_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/branches{/branch}",
40+
"tags_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/tags",
41+
"blobs_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/git/blobs{/sha}",
42+
"git_tags_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/git/tags{/sha}",
43+
"git_refs_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/git/refs{/sha}",
44+
"trees_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/git/trees{/sha}",
45+
"statuses_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/statuses/{sha}",
46+
"languages_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/languages",
47+
"stargazers_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/stargazers",
48+
"contributors_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/contributors",
49+
"subscribers_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/subscribers",
50+
"subscription_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/subscription",
51+
"commits_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/commits{/sha}",
52+
"git_commits_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/git/commits{/sha}",
53+
"comments_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/comments{/number}",
54+
"issue_comment_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/issues/comments{/number}",
55+
"contents_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/contents/{+path}",
56+
"compare_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/compare/{base}...{head}",
57+
"merges_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/merges",
58+
"archive_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/{archive_format}{/ref}",
59+
"downloads_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/downloads",
60+
"issues_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/issues{/number}",
61+
"pulls_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/pulls{/number}",
62+
"milestones_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/milestones{/number}",
63+
"notifications_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/notifications{?since,all,participating}",
64+
"labels_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/labels{/name}",
65+
"releases_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/releases{/id}",
66+
"deployments_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/deployments",
67+
"created_at": "2020-02-06T18:33:39Z",
68+
"updated_at": "2020-02-06T18:33:43Z",
69+
"pushed_at": "2020-02-06T18:33:41Z",
70+
"git_url": "git://github.com/hub4j-test-org/temp-testHandler_Wait.git",
71+
"ssh_url": "[email protected]:hub4j-test-org/temp-testHandler_Wait.git",
72+
"clone_url": "https://github.com/hub4j-test-org/temp-testHandler_Wait.git",
73+
"svn_url": "https://github.com/hub4j-test-org/temp-testHandler_Wait",
74+
"homepage": "http://github-api.kohsuke.org/",
75+
"size": 0,
76+
"stargazers_count": 0,
77+
"watchers_count": 0,
78+
"language": null,
79+
"has_issues": true,
80+
"has_projects": true,
81+
"has_downloads": true,
82+
"has_wiki": true,
83+
"has_pages": false,
84+
"forks_count": 0,
85+
"mirror_url": null,
86+
"archived": false,
87+
"disabled": false,
88+
"open_issues_count": 0,
89+
"license": null,
90+
"forks": 0,
91+
"open_issues": 0,
92+
"watchers": 0,
93+
"default_branch": "main",
94+
"permissions": {
95+
"admin": true,
96+
"push": true,
97+
"pull": true
98+
},
99+
"temp_clone_token": "",
100+
"allow_squash_merge": true,
101+
"allow_merge_commit": true,
102+
"allow_rebase_merge": true,
103+
"delete_branch_on_merge": false,
104+
"organization": {
105+
"login": "hub4j-test-org",
106+
"id": 7544739,
107+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
108+
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
109+
"gravatar_id": "",
110+
"url": "https://api.github.com/users/hub4j-test-org",
111+
"html_url": "https://github.com/hub4j-test-org",
112+
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
113+
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
114+
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
115+
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
116+
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
117+
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
118+
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
119+
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
120+
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
121+
"type": "Organization",
122+
"site_admin": false
123+
},
124+
"network_count": 0,
125+
"subscribers_count": 6
126+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"id": "a60baf84-5b5c-4f86-af3d-cab0d609c7b2",
3+
"name": "user",
4+
"request": {
5+
"url": "/user",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "application/vnd.github+json"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "1-user.json",
16+
"headers": {
17+
"Date": "Thu, 06 Feb 2020 18:33:32 GMT",
18+
"Content-Type": "application/json; charset=utf-8",
19+
"Server": "GitHub.com",
20+
"Status": "200 OK",
21+
"X-RateLimit-Limit": "5000",
22+
"X-RateLimit-Remaining": "4930",
23+
"X-RateLimit-Reset": "{{now offset='3 seconds' format='unix'}}",
24+
"Cache-Control": "private, max-age=60, s-maxage=60",
25+
"Vary": [
26+
"Accept, Authorization, Cookie, X-GitHub-OTP",
27+
"Accept-Encoding"
28+
],
29+
"ETag": "W/\"1cb30f031c67c499473b3aad01c7f7a5\"",
30+
"Last-Modified": "Thu, 06 Feb 2020 17:29:39 GMT",
31+
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
32+
"X-Accepted-OAuth-Scopes": "",
33+
"X-GitHub-Media-Type": "unknown, github.v3",
34+
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
35+
"Access-Control-Allow-Origin": "*",
36+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
37+
"X-Frame-Options": "deny",
38+
"X-Content-Type-Options": "nosniff",
39+
"X-XSS-Protection": "1; mode=block",
40+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
41+
"Content-Security-Policy": "default-src 'none'",
42+
"X-GitHub-Request-Id": "CC37:2605:3F884:4E941:5E3C5BFC"
43+
}
44+
},
45+
"uuid": "a60baf84-5b5c-4f86-af3d-cab0d609c7b2",
46+
"persistent": true,
47+
"insertionIndex": 1
48+
}

0 commit comments

Comments
 (0)