Skip to content

Commit

Permalink
Addressed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerwowen committed Nov 13, 2024
1 parent e3b3f28 commit fd5a76b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/** Wrapper for Jenkins API calls */
public class Jenkins {
private static final Logger LOG = LoggerFactory.getLogger(Jenkins.class);
private static final HttpClient httpClient = HttpClient.builder().maxRetries(3).build();
private static final HttpClient httpClient = HttpClient.builder().build();
private String jenkinsUrl;
private String jenkinsRemoteToken;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
*/
package com.pinterest.teletraan.universal.http;

import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class RetryInterceptor implements Interceptor {
private static final ImmutableList<Integer> RETRY_CODES =
ImmutableList.of(429, 500, 502, 503, 504);
private final int maxRetries;
private final long retryInterval;

Expand All @@ -39,13 +42,17 @@ public Response intercept(Chain chain) throws IOException {
for (int i = 0; i < maxRetries; i++) {
try {
response = chain.proceed(request);
if (response.isSuccessful() || !shouldRetry(response)) {
if (response.isSuccessful()) {
return response;
}
} catch (IOException e) {
lastException = e;
}

if (!shouldRetry(response) || i == maxRetries - 1) {
break;
}

try {
long backoff = (long) Math.pow(2, i) * retryInterval;
TimeUnit.MILLISECONDS.sleep(backoff);
Expand All @@ -63,7 +70,6 @@ public Response intercept(Chain chain) throws IOException {
}

private boolean shouldRetry(Response response) {
int code = response.code();
return code == 429 || code == 500 || code == 502 || code == 503 || code == 504;
return RETRY_CODES.contains(response.code());
}
}

0 comments on commit fd5a76b

Please sign in to comment.