|
22 | 22 | import org.apache.http.client.protocol.ClientContext; |
23 | 23 | import org.apache.http.conn.ClientConnectionManager; |
24 | 24 | import org.apache.http.conn.ConnectionKeepAliveStrategy; |
| 25 | +import org.apache.http.conn.params.ConnManagerPNames; |
| 26 | +import org.apache.http.conn.params.ConnManagerParams; |
| 27 | +import org.apache.http.conn.params.ConnPerRoute; |
| 28 | +import org.apache.http.conn.params.ConnPerRouteBean; |
| 29 | +import org.apache.http.conn.routing.HttpRoute; |
25 | 30 | import org.apache.http.conn.routing.HttpRoutePlanner; |
26 | 31 | import org.apache.http.conn.scheme.PlainSocketFactory; |
27 | 32 | import org.apache.http.conn.scheme.Scheme; |
|
32 | 37 | import org.apache.http.impl.client.DefaultRequestDirector; |
33 | 38 | import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; |
34 | 39 | import org.apache.http.message.BasicHeader; |
| 40 | +import org.apache.http.params.BasicHttpParams; |
| 41 | +import org.apache.http.params.HttpConnectionParams; |
35 | 42 | import org.apache.http.params.HttpParams; |
36 | 43 | import org.apache.http.params.HttpProtocolParams; |
37 | 44 | import org.apache.http.protocol.BasicHttpProcessor; |
@@ -83,11 +90,18 @@ public class ApiWrapper implements CloudAPI, Serializable { |
83 | 90 | transient private HttpClient httpClient; |
84 | 91 | transient private TokenListener listener; |
85 | 92 |
|
86 | | - /** debug request details to stderr */ |
87 | | - public boolean debugRequests; |
| 93 | + private String mDefaultContentType; |
88 | 94 |
|
| 95 | + public static final int BUFFER_SIZE = 8192; |
| 96 | + /** Connection timeout */ |
| 97 | + public static final int TIMEOUT = 20 * 1000; |
| 98 | + /** Keepalive timeout */ |
| 99 | + public static final long KEEPALIVE_TIMEOUT = 20 * 1000; |
| 100 | + /* maximum number of connections allowed */ |
| 101 | + public static final int MAX_TOTAL_CONNECTIONS = 20; |
89 | 102 |
|
90 | | - private String mDefaultContentType; |
| 103 | + /** debug request details to stderr */ |
| 104 | + public boolean debugRequests; |
91 | 105 |
|
92 | 106 | /** |
93 | 107 | * Constructs a new ApiWrapper instance. |
@@ -251,13 +265,36 @@ protected Token requestToken(Request request) throws IOException { |
251 | 265 | } |
252 | 266 | } |
253 | 267 |
|
254 | | - |
255 | | - |
256 | 268 | /** |
257 | | - * @return parameters used by the underlying HttpClient |
| 269 | + * @return the default HttpParams |
| 270 | + * @see <a href="http://developer.android.com/reference/android/net/http/AndroidHttpClient.html#newInstance(java.lang.String, android.content.Context)"> |
| 271 | + * android.net.http.AndroidHttpClient#newInstance(String, Context)</a> |
258 | 272 | */ |
259 | 273 | protected HttpParams getParams() { |
260 | | - return Http.defaultParams(); |
| 274 | + final HttpParams params = new BasicHttpParams(); |
| 275 | + HttpConnectionParams.setConnectionTimeout(params, TIMEOUT); |
| 276 | + HttpConnectionParams.setSoTimeout(params, TIMEOUT); |
| 277 | + HttpConnectionParams.setSocketBufferSize(params, BUFFER_SIZE); |
| 278 | + ConnManagerParams.setMaxTotalConnections(params, MAX_TOTAL_CONNECTIONS); |
| 279 | + |
| 280 | + // Turn off stale checking. Our connections break all the time anyway, |
| 281 | + // and it's not worth it to pay the penalty of checking every time. |
| 282 | + HttpConnectionParams.setStaleCheckingEnabled(params, false); |
| 283 | + |
| 284 | + // fix contributed by Bjorn Roche XXX check if still needed |
| 285 | + params.setBooleanParameter("http.protocol.expect-continue", false); |
| 286 | + params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRoute() { |
| 287 | + @Override |
| 288 | + public int getMaxForRoute(HttpRoute httpRoute) { |
| 289 | + if (env.isApiHost(httpRoute.getTargetHost())) { |
| 290 | + // there will be a lot of concurrent request to the API host |
| 291 | + return MAX_TOTAL_CONNECTIONS; |
| 292 | + } else { |
| 293 | + return ConnPerRouteBean.DEFAULT_MAX_CONNECTIONS_PER_ROUTE; |
| 294 | + } |
| 295 | + } |
| 296 | + }); |
| 297 | + return params; |
261 | 298 | } |
262 | 299 |
|
263 | 300 | /** |
@@ -305,7 +342,7 @@ public HttpClient getHttpClient() { |
305 | 342 | setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { |
306 | 343 | @Override |
307 | 344 | public long getKeepAliveDuration(HttpResponse httpResponse, HttpContext httpContext) { |
308 | | - return 20 * 1000; // milliseconds |
| 345 | + return KEEPALIVE_TIMEOUT; |
309 | 346 | } |
310 | 347 | }); |
311 | 348 |
|
|
0 commit comments