@@ -79,7 +79,7 @@ public final class ServiceInvokerBuilder {
7979 /** The auth client factory. */
8080 private Function <ServiceInvoker , AuthClient > authClientFactory ;
8181 /** Times to retry */
82- private int retries = 1 ;
82+ private int retries ;
8383
8484 //-------------------------------------------------------------------------
8585 /**
@@ -131,6 +131,9 @@ public ServiceInvokerBuilder httpClient(OkHttpClient httpClient) {
131131 * Sets the number of retries for HTTP requests that failed due to system/network issues.
132132 * <p>
133133 * This allows the client to cope with intermittent network failures, such as timeouts.
134+ * <p>
135+ * Retries are primarily handled by the underlying OkHttp library.
136+ * At this level, retries are off by default, and this is the recommended setting.
134137 *
135138 * @param retries how many times to retry
136139 * @return this builder, for method chaining
@@ -217,12 +220,20 @@ public ServiceInvoker build() {
217220 }
218221 // setup HttpClient
219222 TokenInterceptor tokenInterceptor = new TokenInterceptor ();
220- RetryInterceptor retryInterceptor = new RetryInterceptor (retries );
221- httpClient = httpClient .newBuilder ()
222- .addInterceptor (tokenInterceptor )
223- .addInterceptor (new UserAgentHeaderInterceptor ())
224- .addInterceptor (retryInterceptor )
225- .build ();
223+ UserAgentHeaderInterceptor userAgentInterceptor = new UserAgentHeaderInterceptor ();
224+ if (retries > 0 ) {
225+ RetryInterceptor retryInterceptor = new RetryInterceptor (retries );
226+ httpClient = httpClient .newBuilder ()
227+ .addInterceptor (tokenInterceptor )
228+ .addInterceptor (userAgentInterceptor )
229+ .addInterceptor (retryInterceptor )
230+ .build ();
231+ } else {
232+ httpClient = httpClient .newBuilder ()
233+ .addInterceptor (tokenInterceptor )
234+ .addInterceptor (userAgentInterceptor )
235+ .build ();
236+ }
226237 // setup instance, creating a pure immutable ServiceInvoker, then using it
227238 // care should be taken when altering this code to ensure Java Memory Model semantics are considered
228239 ServiceInvoker invoker = new ServiceInvoker (serviceUrl , httpClient , executorService );
@@ -287,21 +298,19 @@ private RetryInterceptor(int retryCount) {
287298 @ Override
288299 public Response intercept (Chain chain ) throws IOException {
289300 Request request = chain .request ();
290- Response response = null ;
291301 Exception exception = null ;
292- int retries = 0 ;
293- while (response == null && retries < retryCount ) {
294- retries ++;
302+ for (int retries = 0 ; retries <= retryCount ; retries ++) {
295303 try {
296- response = chain .proceed (request );
297- } catch (IOException | UncheckedIOException e ) {
298- exception = e ;
304+ Response response = chain .proceed (request );
305+ if (response != null ) {
306+ return response ;
307+ }
308+ } catch (IOException | UncheckedIOException ex ) {
309+ exception = ex ;
299310 }
300311 }
301- if (response == null ) {
302- throw new IOException ("Failed to perform " + request .method () + " request to given URL after " + retries + " retries: " + request .url ().toString (), exception );
303- }
304- return response ;
312+ throw new IOException ("Failed to perform " + request .method () + " request to given URL after " + retryCount +
313+ " retries: " + request .url ().toString (), exception );
305314 }
306315 }
307316
@@ -341,6 +350,8 @@ public Response intercept(Chain chain) throws IOException {
341350 if (response .code () != 401 ) {
342351 return response ;
343352 }
353+ // response must be closed before calling chain.proceed() again
354+ response .close ();
344355 }
345356
346357 // try to get a new token
0 commit comments