public final class StreamAllocation
extends java.lang.Object
Instances of this class act on behalf of the call, using one or more streams over one or more connections. This class has APIs to release each of the above resources:
noNewStreams()
prevents the connection from being used for new streams in the
future. Use this after a Connection: close
header, or when the connection may be
inconsistent.
streamFinished()
releases the active stream from this allocation.
Note that only one stream may be active at a given time, so it is necessary to call
streamFinished()
before creating a subsequent stream with newStream()
.
release()
removes the call's hold on the connection. Note that this won't
immediately free the connection if there is a stream still lingering. That happens when a
call is complete but its response body has yet to be fully consumed.
This class supports asynchronous canceling. This is intended to have the smallest blast radius possible. If an HTTP/2 stream is active, canceling will cancel that stream but not the other streams sharing its connection. But if the TLS handshake is still in progress then canceling may break the entire connection.
Modifier and Type | Class and Description |
---|---|
static class |
StreamAllocation.StreamAllocationReference |
Modifier and Type | Field and Description |
---|---|
Address |
address |
Call |
call |
private java.lang.Object |
callStackTrace |
private boolean |
canceled |
private HttpCodec |
codec |
private RealConnection |
connection |
private ConnectionPool |
connectionPool |
EventListener |
eventListener |
private int |
refusedStreamCount |
private boolean |
released |
private boolean |
reportedAcquired |
private Route |
route |
private RouteSelector.Selection |
routeSelection |
private RouteSelector |
routeSelector |
Constructor and Description |
---|
StreamAllocation(ConnectionPool connectionPool,
Address address,
Call call,
EventListener eventListener,
java.lang.Object callStackTrace) |
Modifier and Type | Method and Description |
---|---|
void |
acquire(RealConnection connection,
boolean reportedAcquired)
Use this allocation to hold
connection . |
void |
cancel() |
HttpCodec |
codec() |
RealConnection |
connection() |
private java.net.Socket |
deallocate(boolean noNewStreams,
boolean released,
boolean streamFinished)
Releases resources held by this allocation.
|
private RealConnection |
findConnection(int connectTimeout,
int readTimeout,
int writeTimeout,
int pingIntervalMillis,
boolean connectionRetryEnabled)
Returns a connection to host a new stream.
|
private RealConnection |
findHealthyConnection(int connectTimeout,
int readTimeout,
int writeTimeout,
int pingIntervalMillis,
boolean connectionRetryEnabled,
boolean doExtensiveHealthChecks)
Finds a connection and returns it if it is healthy.
|
boolean |
hasMoreRoutes() |
HttpCodec |
newStream(OkHttpClient client,
Interceptor.Chain chain,
boolean doExtensiveHealthChecks) |
void |
noNewStreams()
Forbid new streams from being created on the connection that hosts this allocation.
|
void |
release() |
private void |
release(RealConnection connection)
Remove this allocation from the connection's list of allocations.
|
java.net.Socket |
releaseAndAcquire(RealConnection newConnection)
Release the connection held by this connection and acquire
newConnection instead. |
private java.net.Socket |
releaseIfNoNewStreams()
Releases the currently held connection and returns a socket to close if the held connection
restricts new streams from being created.
|
Route |
route() |
private RouteDatabase |
routeDatabase() |
void |
streamFailed(java.io.IOException e) |
void |
streamFinished(boolean noNewStreams,
HttpCodec codec,
long bytesRead,
java.io.IOException e) |
java.lang.String |
toString() |
public final Address address
private RouteSelector.Selection routeSelection
private Route route
private final ConnectionPool connectionPool
public final Call call
public final EventListener eventListener
private final java.lang.Object callStackTrace
private final RouteSelector routeSelector
private int refusedStreamCount
private RealConnection connection
private boolean reportedAcquired
private boolean released
private boolean canceled
private HttpCodec codec
public StreamAllocation(ConnectionPool connectionPool, Address address, Call call, EventListener eventListener, java.lang.Object callStackTrace)
public HttpCodec newStream(OkHttpClient client, Interceptor.Chain chain, boolean doExtensiveHealthChecks)
private RealConnection findHealthyConnection(int connectTimeout, int readTimeout, int writeTimeout, int pingIntervalMillis, boolean connectionRetryEnabled, boolean doExtensiveHealthChecks) throws java.io.IOException
java.io.IOException
private RealConnection findConnection(int connectTimeout, int readTimeout, int writeTimeout, int pingIntervalMillis, boolean connectionRetryEnabled) throws java.io.IOException
java.io.IOException
private java.net.Socket releaseIfNoNewStreams()
public void streamFinished(boolean noNewStreams, HttpCodec codec, long bytesRead, java.io.IOException e)
public HttpCodec codec()
private RouteDatabase routeDatabase()
public Route route()
public RealConnection connection()
public void release()
public void noNewStreams()
private java.net.Socket deallocate(boolean noNewStreams, boolean released, boolean streamFinished)
Returns a closeable that the caller should pass to Util.closeQuietly(java.io.Closeable)
upon completion
of the synchronized block. (We don't do I/O while synchronized on the connection pool.)
public void cancel()
public void streamFailed(java.io.IOException e)
public void acquire(RealConnection connection, boolean reportedAcquired)
connection
. Each call to this must be paired with a call to
release()
on the same connection.private void release(RealConnection connection)
public java.net.Socket releaseAndAcquire(RealConnection newConnection)
newConnection
instead. It is
only safe to call this if the held connection is newly connected but duplicated by newConnection
. Typically this occurs when concurrently connecting to an HTTP/2 webserver.
Returns a closeable that the caller should pass to Util.closeQuietly(java.io.Closeable)
upon completion
of the synchronized block. (We don't do I/O while synchronized on the connection pool.)
public boolean hasMoreRoutes()
public java.lang.String toString()
toString
in class java.lang.Object