Bug 734521 - ensure response entities are always consumed. r=rnewman

This commit is contained in:
Nick Alexander
2012-03-12 19:17:56 -07:00
parent 6e28a6aaad
commit ac8c3812f2
14 changed files with 255 additions and 239 deletions

View File

@@ -4,14 +4,8 @@
package org.mozilla.gecko.sync.net;
import java.io.BufferedReader;
import java.io.IOException;
import ch.boye.httpclientandroidlib.HttpEntity;
import ch.boye.httpclientandroidlib.HttpResponse;
import ch.boye.httpclientandroidlib.client.methods.HttpRequestBase;
import ch.boye.httpclientandroidlib.impl.client.DefaultHttpClient;
import ch.boye.httpclientandroidlib.util.EntityUtils;
/**
* Shared abstract class for resource delegate that use the same timeouts
@@ -21,6 +15,8 @@ import ch.boye.httpclientandroidlib.util.EntityUtils;
*
*/
public abstract class SyncResourceDelegate implements ResourceDelegate {
public static int connectionTimeoutInMillis = 1000 * 30; // Wait 30s for a connection to open.
public static int socketTimeoutInMillis = 1000 * 5 * 60; // Wait 5 minutes for data.
protected Resource resource;
public SyncResourceDelegate(Resource resource) {
@@ -29,11 +25,12 @@ public abstract class SyncResourceDelegate implements ResourceDelegate {
@Override
public int connectionTimeout() {
return 30 * 1000; // Wait 30s for a connection to open.
return connectionTimeoutInMillis;
}
@Override
public int socketTimeout() {
return 5 * 60 * 1000; // Wait 5 minutes for data.
return socketTimeoutInMillis;
}
@Override
@@ -44,47 +41,4 @@ public abstract class SyncResourceDelegate implements ResourceDelegate {
@Override
public void addHeaders(HttpRequestBase request, DefaultHttpClient client) {
}
/**
* Best-effort attempt to ensure that the entity has been fully consumed and
* that the underlying stream has been closed.
*
* This releases the connection back to the connection pool.
*
* @param entity The HttpEntity to be consumed.
*/
public static void consumeEntity(HttpEntity entity) {
try {
EntityUtils.consume(entity);
} catch (Exception e) {
// Doesn't matter.
}
}
public static void consumeEntity(HttpResponse response) {
consumeEntity(response.getEntity());
}
public static void consumeEntity(SyncStorageResponse response) {
if (response.httpResponse() != null) {
consumeEntity(response.httpResponse());
}
}
/**
* Best-effort attempt to ensure that the reader has been fully consumed, so
* that the underlying stream will be closed.
*
* This should allow the connection to be released back to the connection pool.
*
* @param reader The BufferedReader to be consumed.
*/
public static void consumeReader(BufferedReader reader) {
try {
while ((reader.readLine()) != null) {
}
} catch (IOException e) {
return;
}
}
}