27
27
28
28
import java .io .Closeable ;
29
29
import java .io .IOException ;
30
- import java .net .URI ;
31
30
import java .nio .ByteBuffer ;
32
31
import java .time .Duration ;
33
32
import java .time .ZonedDateTime ;
@@ -152,7 +151,6 @@ private CompletableFuture<HttpResponse<AsyncBody>> consumeBytesOnce(StandardHttp
152
151
private <V > CompletableFuture <V > retryWithExponentialBackoff (
153
152
StandardHttpRequest request , Supplier <CompletableFuture <V >> action , java .util .function .Consumer <V > onCancel ,
154
153
Function <V , HttpResponse <?>> responseExtractor ) {
155
- final URI uri = request .uri ();
156
154
final RequestConfig requestConfig = getTag (RequestConfig .class );
157
155
final ExponentialBackoffIntervalCalculator retryIntervalCalculator = ExponentialBackoffIntervalCalculator
158
156
.from (requestConfig );
@@ -164,34 +162,39 @@ private <V> CompletableFuture<V> retryWithExponentialBackoff(
164
162
}
165
163
return AsyncUtils .retryWithExponentialBackoff (action , onCancel , timeout , retryIntervalCalculator ,
166
164
(response , throwable , retryInterval ) -> {
167
- if (response != null ) {
168
- HttpResponse <?> httpResponse = responseExtractor .apply (response );
169
- if (httpResponse != null ) {
170
- final int code = httpResponse .code ();
171
- if (code == 429 || code >= 500 ) {
172
- retryInterval = Math .max (retryAfterMillis (httpResponse ), retryInterval );
173
- LOG .debug (
174
- "HTTP operation on url: {} should be retried as the response code was {}, retrying after {} millis" ,
175
- uri , code , retryInterval );
176
- return true ;
177
- }
178
- }
179
- } else {
180
- final Throwable actualCause = unwrapCompletionException (throwable );
181
- builder .interceptors .forEach ((s , interceptor ) -> interceptor .afterConnectionFailure (request , actualCause ));
182
- if (actualCause instanceof IOException ) {
183
- // TODO: may not be specific enough - incorrect ssl settings for example will get caught here
184
- LOG .debug (
185
- String .format ("HTTP operation on url: %s should be retried after %d millis because of IOException" ,
186
- uri , retryInterval ),
187
- actualCause );
188
- return true ;
189
- }
190
- }
191
- return false ;
165
+ return shouldRetry (request , responseExtractor , response , throwable , retryInterval );
192
166
});
193
167
}
194
168
169
+ <V > long shouldRetry (StandardHttpRequest request , Function <V , HttpResponse <?>> responseExtractor , V response ,
170
+ Throwable throwable , long retryInterval ) {
171
+ if (response != null ) {
172
+ HttpResponse <?> httpResponse = responseExtractor .apply (response );
173
+ if (httpResponse != null ) {
174
+ final int code = httpResponse .code ();
175
+ if (code == 429 || code >= 500 ) {
176
+ retryInterval = Math .max (retryAfterMillis (httpResponse ), retryInterval );
177
+ LOG .debug (
178
+ "HTTP operation on url: {} should be retried as the response code was {}, retrying after {} millis" ,
179
+ request .uri (), code , retryInterval );
180
+ return retryInterval ;
181
+ }
182
+ }
183
+ } else {
184
+ final Throwable actualCause = unwrapCompletionException (throwable );
185
+ builder .interceptors .forEach ((s , interceptor ) -> interceptor .afterConnectionFailure (request , actualCause ));
186
+ if (actualCause instanceof IOException ) {
187
+ // TODO: may not be specific enough - incorrect ssl settings for example will get caught here
188
+ LOG .debug (
189
+ String .format ("HTTP operation on url: %s should be retried after %d millis because of IOException" ,
190
+ request .uri (), retryInterval ),
191
+ actualCause );
192
+ return retryInterval ;
193
+ }
194
+ }
195
+ return -1 ;
196
+ }
197
+
195
198
static Throwable unwrapCompletionException (Throwable throwable ) {
196
199
final Throwable actualCause ;
197
200
if (throwable instanceof CompletionException ) {
0 commit comments