From f7c48ee0cef0bd8cb5fe94ce2e7d1e028c553a83 Mon Sep 17 00:00:00 2001 From: Dave Miller Date: Wed, 27 Jan 2016 18:59:56 -0800 Subject: [PATCH] Fix per connection timeouts for XHR Summary: public When initially implemented, the timeout for a request was actually set for all requests that the OKHttpClient handled, not just that request. The fix is to clone the client, set the timeout for the client which will be used for that request. Reviewed By: andreicoman11 Differential Revision: D2873220 fb-gh-sync-id: c8c102a6eb9dd0ac57d5a7f53c3ba3b7d6db5ef9 --- .../react/modules/network/NetworkingModule.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java index 9374683b9..50cd58a08 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java @@ -132,7 +132,15 @@ public final class NetworkingModule extends ReactContextBaseJavaModule { requestBuilder.tag(requestId); } - mClient.setConnectTimeout(timeout, TimeUnit.MILLISECONDS); + OkHttpClient client = mClient; + // If the current timeout does not equal the passed in timeout, we need to clone the existing + // client and set the timeout explicitly on the clone. This is cheap as everything else is + // shared under the hood. + // See https://github.com/square/okhttp/wiki/Recipes#per-call-configuration for more information + if (timeout != mClient.getConnectTimeout()) { + client = mClient.clone(); + client.setReadTimeout(timeout, TimeUnit.MILLISECONDS); + } Headers requestHeaders = extractHeaders(headers, data); if (requestHeaders == null) { @@ -193,7 +201,7 @@ public final class NetworkingModule extends ReactContextBaseJavaModule { requestBuilder.method(method, null); } - mClient.newCall(requestBuilder.build()).enqueue( + client.newCall(requestBuilder.build()).enqueue( new Callback() { @Override public void onFailure(Request request, IOException e) {