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

Fix RetryInterceptor #1751

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,23 @@ public RetryInterceptor(int maxRetries, long retryInterval) {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = null;
IOException lastException = null;
Response response = chain.proceed(request);
int tryCount = 1;

for (int i = 0; i < maxRetries; i++) {
while (shouldRetry(response) && tryCount < maxRetries) {
long backoff = (long) Math.pow(2, (tryCount - 1)) * retryInterval;
response.close();
try {
response = chain.proceed(request);
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);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("Retry interrupted", e);
}
response = chain.proceed(request);
tryCount++;
}

if (response != null) {
return response;
} else {
throw lastException != null ? lastException : new IOException("Unknown error");
}
return response;
}

private boolean shouldRetry(Response response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ static class ServerErrorDispatcher extends Dispatcher {

@Override
public MockResponse dispatch(RecordedRequest request) {
return new MockResponse().setResponseCode(responseCode);
return new MockResponse().setResponseCode(responseCode).setBody(TEST_BODY);
}
}
}
Loading