-
Notifications
You must be signed in to change notification settings - Fork 352
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
Added wait and retries for rate limit response code from Bitbucket server #433
Closed
shankarinece
wants to merge
6
commits into
jenkinsci:master
from
shankarinece:enhancement/bitbucket-server-ratelimit-fix
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e7ffc97
Added wait and retries for ratelimit response code
5c6d025
Fixed linting - removal of whitespaces
3d852de
Fixed linting issues
e9031b3
Merge branch 'master' into enhancement/bitbucket-server-ratelimit-fix
bitwiseman 4d5fb12
Merge branch 'master' into enhancement/bitbucket-server-ratelimit-fix
lifeofguenter 4c742b7
Merge branch 'master' into enhancement/bitbucket-server-ratelimit-fix
lifeofguenter 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 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 |
---|---|---|
|
@@ -151,6 +151,9 @@ public class BitbucketServerAPIClient implements BitbucketApi { | |
private static final String API_COMMIT_STATUS_PATH = "/rest/build-status/1.0/commits{/hash}"; | ||
private static final Integer DEFAULT_PAGE_LIMIT = 200; | ||
|
||
private static final int API_RATE_LIMIT_CODE = 429; | ||
private static final int API_RATE_LIMIT_WAIT_TIME_DEFAULT = 5000; | ||
|
||
/** | ||
* Repository owner. | ||
*/ | ||
|
@@ -529,6 +532,15 @@ public boolean checkPathExists(@NonNull String branchOrHash, @NonNull String pat | |
// https://support.atlassian.com/bitbucket-cloud/docs/use-bitbucket-rest-api-version-1/ | ||
} else if (HttpStatus.SC_NOT_FOUND == status || HttpStatus.SC_UNAUTHORIZED == status) { | ||
return false; | ||
} else if (API_RATE_LIMIT_CODE == status) { | ||
LOGGER.fine("Bitbucket API rate limit reached, sleeping for 5 sec then retry..."); | ||
try { | ||
Thread.sleep(API_RATE_LIMIT_WAIT_TIME_DEFAULT); | ||
} catch (InterruptedException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
return checkPathExists(branchOrHash, path); | ||
} else { | ||
throw new IOException("Communication error for url: " + path + " status code: " + status); | ||
} | ||
|
@@ -871,9 +883,23 @@ protected String getRequest(String path) throws IOException { | |
throw new FileNotFoundException("URL: " + path); | ||
} | ||
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { | ||
throw new BitbucketRequestException(response.getStatusLine().getStatusCode(), | ||
"HTTP request error. Status: " + response.getStatusLine().getStatusCode() | ||
+ ": " + response.getStatusLine().getReasonPhrase() + ".\n" + response); | ||
|
||
if(response.getStatusLine().getStatusCode() == API_RATE_LIMIT_CODE) { | ||
response.close(); | ||
httpget.releaseConnection(); | ||
LOGGER.fine("Bitbucket API rate limit reached, sleeping for 5 sec then retry..."); | ||
try { | ||
Thread.sleep(API_RATE_LIMIT_WAIT_TIME_DEFAULT); | ||
} catch (InterruptedException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
Comment on lines
+890
to
+896
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. These two blocks are identical, convert to a method. |
||
return getRequest(path); | ||
} else { | ||
throw new BitbucketRequestException(response.getStatusLine().getStatusCode(), | ||
"HTTP request error. Status: " + response.getStatusLine().getStatusCode() | ||
+ ": " + response.getStatusLine().getReasonPhrase() + ".\n" + response); | ||
} | ||
} | ||
return content; | ||
} catch (BitbucketRequestException | FileNotFoundException e) { | ||
|
@@ -911,6 +937,11 @@ private BufferedImage getImageRequest(String path) throws IOException, Interrupt | |
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) { | ||
throw new FileNotFoundException("URL: " + path); | ||
} | ||
if(response.getStatusLine().getStatusCode() == API_RATE_LIMIT_CODE) { | ||
httpget.releaseConnection(); | ||
Thread.sleep(API_RATE_LIMIT_WAIT_TIME_DEFAULT); | ||
return getImageRequest(path); | ||
} | ||
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { | ||
throw new BitbucketRequestException(response.getStatusLine().getStatusCode(), | ||
"HTTP request error. Status: " + response.getStatusLine().getStatusCode() + ": " | ||
|
@@ -1084,6 +1115,17 @@ private String doRequest(HttpRequestBase request) throws IOException { | |
content = new String(buf.toByteArray(), StandardCharsets.UTF_8); | ||
} | ||
EntityUtils.consume(response.getEntity()); | ||
|
||
if(response.getStatusLine().getStatusCode() == API_RATE_LIMIT_CODE) { | ||
request.releaseConnection(); | ||
try { | ||
Thread.sleep(API_RATE_LIMIT_WAIT_TIME_DEFAULT); | ||
} catch (InterruptedException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
return doRequest(request); | ||
} | ||
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK && response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) { | ||
throw new BitbucketRequestException(response.getStatusLine().getStatusCode(), "HTTP request error. Status: " + response.getStatusLine().getStatusCode() + ": " + response.getStatusLine().getReasonPhrase() + ".\n" + response); | ||
} | ||
|
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.
Just waiting for 5 seconds will cause a lot of polling and spamming of the server. Please shift to some kind of progressive back off where each of retry waits longer (up to some reasonable limit.
Alternative: see if they the headers of the 429 provide information about when to the limit will reset and sleep for something like that long.