Skip to content

Commit

Permalink
fix: max wait interval in back off retry (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
Azure99 authored Mar 29, 2024
1 parent 6c0ab92 commit 26467cd
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions java/src/main/java/com/baidubce/qianfan/QianfanClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ private <T extends BaseRequest<T>, U, V, E extends Exception> V request(
BaseRequest<T> request,
ThrowingFunction<HttpRequest, HttpResponse<U>, E> reqProcessor,
ThrowingFunction<HttpResponse<U>, V, E> respProcessor) {
long startTime = System.currentTimeMillis();
for (int i = 0; i < retryConfig.getRetryCount(); i++) {
try {
return innerRequest(request, reqProcessor, respProcessor);
Expand All @@ -119,7 +118,7 @@ private <T extends BaseRequest<T>, U, V, E extends Exception> V request(
if (i == retryConfig.getRetryCount() - 1) {
throw ex;
}
backoffSleep(i, retryConfig.getBackoffFactor(), startTime, retryConfig.getMaxWaitInterval());
backoffSleep(i, retryConfig.getBackoffFactor(), retryConfig.getMaxWaitInterval());
}
}
throw new IllegalStateException("Request failed with unknown error");
Expand Down Expand Up @@ -151,12 +150,10 @@ private <T extends BaseRequest<T>, U, V, E extends Exception> V innerRequest(
}
}

private void backoffSleep(int retryCount, double backoffFactor, long startTime, int totalRetryTimeout) throws RequestException {
private void backoffSleep(int retryCount, double backoffFactor, int maxWaitInterval) throws RequestException {
try {
long baseDelay = (long) (Math.pow(2, retryCount) * backoffFactor * 1000);
long elapsedTime = System.currentTimeMillis() - startTime;
long adjustedDelay = Math.min(baseDelay, (long) totalRetryTimeout * 1000 - elapsedTime);
Thread.sleep(adjustedDelay);
long delay = (long) (Math.pow(2, retryCount) * backoffFactor * 1000);
Thread.sleep(Math.min(delay, maxWaitInterval * 1000L));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RequestException("Request failed: retry delay interrupted", e);
Expand Down

0 comments on commit 26467cd

Please sign in to comment.