diff options
| author | Jesse Wilson <jwilson@squareup.com> | 2014-04-03 00:18:59 -0400 |
|---|---|---|
| committer | Neil Fuller <nfuller@google.com> | 2014-10-24 17:09:50 +0100 |
| commit | 87ed7244fb53ae2bac9f23c033bbd5f23ac269f8 (patch) | |
| tree | bab8314a797842e78895d5df69386c234c20815d /okhttp | |
| parent | fe5706946b0c01b1370419c83b71b3b7f104c19e (diff) | |
| download | android_external_okhttp-87ed7244fb53ae2bac9f23c033bbd5f23ac269f8.tar.gz android_external_okhttp-87ed7244fb53ae2bac9f23c033bbd5f23ac269f8.tar.bz2 android_external_okhttp-87ed7244fb53ae2bac9f23c033bbd5f23ac269f8.zip | |
New disconnect strategy.
Support asynchronous disconnects by breaking the socket only, which
should cause the thread using that socket to trigger clean-up.
Upstream commit: https://github.com/square/okhttp/commit/9c302131491d05a4ca0209ef21770592c01f76fa
Bug: 18083851
Change-Id: I5f5eb648f4a5f2022c63acd7c903aac88e178d9a
Diffstat (limited to 'okhttp')
6 files changed, 37 insertions, 2 deletions
diff --git a/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpConnection.java b/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpConnection.java index b12b12d..718d471 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpConnection.java +++ b/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpConnection.java @@ -122,6 +122,10 @@ public final class HttpConnection { return state == STATE_CLOSED; } + public void closeIfOwnedBy(Object owner) throws IOException { + connection.closeIfOwnedBy(owner); + } + public void flush() throws IOException { sink.flush(); } diff --git a/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpEngine.java b/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpEngine.java index f00fbe7..d796a6c 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpEngine.java +++ b/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpEngine.java @@ -411,6 +411,18 @@ public class HttpEngine { } /** + * Immediately closes the socket connection if it's currently held by this + * engine. Use this to interrupt an in-flight request from any thread. It's + * the caller's responsibility to close the request body and response body + * streams; otherwise resources may be leaked. + */ + public final void disconnect() throws IOException { + if (transport != null) { + transport.disconnect(this); + } + } + + /** * Release any resources held by this engine. If a connection is still held by * this engine, it is returned. */ diff --git a/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpTransport.java b/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpTransport.java index a1b367f..2ffe039 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpTransport.java +++ b/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpTransport.java @@ -150,4 +150,8 @@ public final class HttpTransport implements Transport { // reference escapes. return httpConnection.newUnknownLengthSource(cacheRequest); } + + @Override public void disconnect(HttpEngine engine) throws IOException { + httpConnection.closeIfOwnedBy(engine); + } } diff --git a/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpURLConnectionImpl.java b/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpURLConnectionImpl.java index 899d914..32be0be 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpURLConnectionImpl.java +++ b/okhttp/src/main/java/com/squareup/okhttp/internal/http/HttpURLConnectionImpl.java @@ -109,9 +109,18 @@ public class HttpURLConnectionImpl extends HttpURLConnection { @Override public final void disconnect() { // Calling disconnect() before a connection exists should have no effect. - if (httpEngine != null) { - httpEngine.close(); + if (httpEngine == null) return; + + try { + httpEngine.disconnect(); + } catch (IOException ignored) { } + + // This doesn't close the stream because doing so would require all stream + // access to be synchronized. It's expected that the thread using the + // connection will close its streams directly. If it doesn't, the worst + // case is that the GzipSource's Inflater won't be released until it's + // finalized. (This logs a warning on Android.) } /** diff --git a/okhttp/src/main/java/com/squareup/okhttp/internal/http/SpdyTransport.java b/okhttp/src/main/java/com/squareup/okhttp/internal/http/SpdyTransport.java index e775d34..9db9643 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/internal/http/SpdyTransport.java +++ b/okhttp/src/main/java/com/squareup/okhttp/internal/http/SpdyTransport.java @@ -219,6 +219,10 @@ public final class SpdyTransport implements Transport { @Override public void releaseConnectionOnIdle() { } + @Override public void disconnect(HttpEngine engine) throws IOException { + stream.close(ErrorCode.CANCEL); + } + @Override public boolean canReuseConnection() { return true; // TODO: spdyConnection.isClosed() ? } diff --git a/okhttp/src/main/java/com/squareup/okhttp/internal/http/Transport.java b/okhttp/src/main/java/com/squareup/okhttp/internal/http/Transport.java index 94c90d4..852a15b 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/internal/http/Transport.java +++ b/okhttp/src/main/java/com/squareup/okhttp/internal/http/Transport.java @@ -76,6 +76,8 @@ interface Transport { */ void releaseConnectionOnIdle() throws IOException; + void disconnect(HttpEngine engine) throws IOException; + /** * Returns true if the socket connection held by this transport can be reused * for a follow-up exchange. |
