1414import java .net .UnknownHostException ;
1515
1616/**
17- * OkHttp interceptor that retries requests on certain connection failures,
18- * which can be helpful when MarkLogic is temporarily unavailable during restarts.
17+ * Experimental interceptor added in 8.0.0 for retrying requests that fail due to connection issues. These issues are
18+ * not handled by the application-level retry support in OkHttpServices, which only handles retries based on certain
19+ * HTTP status codes. The main limitation of this approach is that it cannot retry a request that has a one-shot body,
20+ * such as a streaming body. But for requests that don't have one-shot bodies, this interceptor can be helpful for
21+ * retrying requests that fail due to temporary network issues or MarkLogic restarts.
1922 */
20- public class RetryInterceptor implements Interceptor {
23+ public class RetryIOExceptionInterceptor implements Interceptor {
2124
22- private final static Logger logger = org .slf4j .LoggerFactory .getLogger (RetryInterceptor .class );
25+ private final static Logger logger = org .slf4j .LoggerFactory .getLogger (RetryIOExceptionInterceptor .class );
2326
2427 private final int maxRetries ;
2528 private final long initialDelayMs ;
2629 private final double backoffMultiplier ;
2730 private final long maxDelayMs ;
2831
29- public RetryInterceptor (int maxRetries , long initialDelayMs , double backoffMultiplier , long maxDelayMs ) {
32+ public RetryIOExceptionInterceptor (int maxRetries , long initialDelayMs , double backoffMultiplier , long maxDelayMs ) {
3033 this .maxRetries = maxRetries ;
3134 this .initialDelayMs = initialDelayMs ;
3235 this .backoffMultiplier = backoffMultiplier ;
@@ -37,11 +40,15 @@ public RetryInterceptor(int maxRetries, long initialDelayMs, double backoffMulti
3740 public Response intercept (Chain chain ) throws IOException {
3841 Request request = chain .request ();
3942
43+ if (request .body () instanceof RetryableRequestBody body && !body .isRetryable ()) {
44+ return chain .proceed (request );
45+ }
46+
4047 for (int attempt = 0 ; attempt <= maxRetries ; attempt ++) {
4148 try {
4249 return chain .proceed (request );
4350 } catch (IOException e ) {
44- if (attempt == maxRetries || !isRetryableException (e )) {
51+ if (attempt == maxRetries || !isRetryableIOException (e )) {
4552 logger .warn ("Not retryable: {}; {}" , e .getClass (), e .getMessage ());
4653 throw e ;
4754 }
@@ -58,7 +65,7 @@ public Response intercept(Chain chain) throws IOException {
5865 throw new IllegalStateException ("Unexpected end of retry loop" );
5966 }
6067
61- private boolean isRetryableException (IOException e ) {
68+ private boolean isRetryableIOException (IOException e ) {
6269 return e instanceof ConnectException ||
6370 e instanceof SocketTimeoutException ||
6471 e instanceof UnknownHostException ||
0 commit comments