Skip to content

Commit

Permalink
Fix per connection timeouts for XHR
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Dave Miller authored and facebook-github-bot-4 committed Jan 28, 2016
1 parent 3117a33 commit f7c48ee
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,15 @@ public void sendRequest(
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) {
Expand Down Expand Up @@ -193,7 +201,7 @@ public void sendRequest(
requestBuilder.method(method, null);
}

mClient.newCall(requestBuilder.build()).enqueue(
client.newCall(requestBuilder.build()).enqueue(
new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Expand Down

0 comments on commit f7c48ee

Please sign in to comment.