Skip to content

Commit

Permalink
More logging of HTTP errors. Improve throttling handling. Try fix bui…
Browse files Browse the repository at this point in the history
…ld notification errors by using a more unique key.
  • Loading branch information
Edgars Batna committed Mar 3, 2023
1 parent 6cb1559 commit 6c9ad6e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private static String getBuildKey(@NonNull Run<?, ?> build, String branch,
String folderName = build.getParent().getParent().getFullName();
key = String.format("%s/%s", folderName, branch);
} else {
key = build.getParent().getFullName(); // use the job full name as the key for the status
key = build.getUrl();
}

return key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,7 @@
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.*;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.SocketConfig;
import org.apache.http.entity.ContentType;
Expand Down Expand Up @@ -859,6 +853,7 @@ protected CloseableHttpResponse executeMethod(HttpHost host, HttpRequestBase htt
httpMethod.setConfig(requestConfig.build());

CloseableHttpResponse response = client.execute(host, httpMethod, requestContext);
int i = 5000;
while (response.getStatusLine().getStatusCode() == API_RATE_LIMIT_CODE) {
release(httpMethod);
if (Thread.interrupted()) {
Expand All @@ -868,8 +863,11 @@ protected CloseableHttpResponse executeMethod(HttpHost host, HttpRequestBase htt
TODO: When bitbucket starts supporting rate limit expiration time, remove 5 sec wait and put code
to wait till expiration time is over. It should also fix the wait for ever loop.
*/
LOGGER.fine("Bitbucket Cloud API rate limit reached, sleeping for 5 sec then retry...");
Thread.sleep(5000);
LOGGER.fine("Bitbucket Cloud API rate limit reached, sleeping for " + i + " ms before retrying...");
Thread.sleep(i);
if (i < 60000 * 5) {
i *= 2;
}
response = client.execute(host, httpMethod, requestContext);
}
return response;
Expand Down Expand Up @@ -975,7 +973,11 @@ private String doRequest(HttpRequestBase httppost) throws IOException, Interrupt
String content = getResponseContent(response);
EntityUtils.consume(response.getEntity());
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);
String reqContent = "";
if (httppost instanceof HttpEntityEnclosingRequestBase) {
reqContent = ((HttpEntityEnclosingRequestBase) httppost).getEntity().toString();
}
throw new BitbucketRequestException(response.getStatusLine().getStatusCode(), "HTTP request error. Status: " + response.getStatusLine().getStatusCode() + ": " + response.getStatusLine().getReasonPhrase() + ".\nRequest: " + reqContent + "\nResponse: " + content);
}
return content;
} catch (BitbucketRequestException e) {
Expand Down

0 comments on commit 6c9ad6e

Please sign in to comment.