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:
parent
3117a334b1
commit
f7c48ee0ce
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue