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
This commit is contained in:
Dave Miller 2016-01-27 18:59:56 -08:00 committed by facebook-github-bot-4
parent 3117a334b1
commit f7c48ee0ce
1 changed files with 10 additions and 2 deletions

View File

@ -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) {