Skip to content

Commit 7ecf327

Browse files
committed
add retry exception handler
1 parent b95d48c commit 7ecf327

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

client/src/main/java/io/avaje/http/client/DHttpClientRequestWithRetry.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,34 @@ final class DHttpClientRequestWithRetry extends DHttpClientRequest {
2222
@Override
2323
protected <T> HttpResponse<T> performSend(HttpResponse.BodyHandler<T> responseHandler) {
2424
HttpResponse<T> res;
25-
res = super.performSend(responseHandler);
26-
if (res.statusCode() < 300) {
27-
return res;
28-
}
29-
while (retryHandler.isRetry(retryCount++, res)) {
30-
res = super.performSend(responseHandler);
25+
HttpException ex;
26+
27+
do {
28+
try {
29+
res = super.performSend(responseHandler);
30+
ex = null;
31+
} catch (final HttpException e) {
32+
ex = e;
33+
res = null;
34+
}
35+
if (res != null && res.statusCode() < 300) {
36+
return res;
37+
}
38+
retryCount++;
39+
} while (retry(res, ex));
40+
41+
if (res == null && ex != null) {
42+
throw ex;
3143
}
44+
3245
return res;
3346
}
3447

48+
protected boolean retry(HttpResponse<?> res, HttpException ex) {
49+
50+
if (res != null) {
51+
return retryHandler.isRetry(retryCount, res);
52+
}
53+
return retryHandler.isExceptionRetry(retryCount, ex);
54+
}
3555
}

client/src/main/java/io/avaje/http/client/RetryHandler.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ public interface RetryHandler {
1111
* Return true if the request should be retried.
1212
*
1313
* @param retryCount The number of retry attempts already executed
14-
* @param response The HTTP response
14+
* @param response The HTTP response
1515
* @return True if the request should be retried or false if not
1616
*/
1717
boolean isRetry(int retryCount, HttpResponse<?> response);
1818

19+
/**
20+
* Return true if the request should be retried.
21+
*
22+
* @param retryCount The number of retry attempts already executed
23+
* @param exception The Wrapped Error thrown by the underlying Http Client
24+
* @return True if the request should be retried or false if not
25+
*/
26+
default boolean isExceptionRetry(int retryCount, HttpException exception) {
27+
throw exception;
28+
}
1929
}

0 commit comments

Comments
 (0)