public final class Dispatcher
extends java.lang.Object
Each dispatcher uses an ExecutorService
to run calls internally. If you supply your
own executor, it should be able to run the configured maximum number
of calls concurrently.
Modifier and Type | Field and Description |
---|---|
private java.util.concurrent.ExecutorService |
executorService
Executes calls.
|
private java.lang.Runnable |
idleCallback |
private int |
maxRequests |
private int |
maxRequestsPerHost |
private java.util.Deque<RealCall.AsyncCall> |
readyAsyncCalls
Ready async calls in the order they'll be run.
|
private java.util.Deque<RealCall.AsyncCall> |
runningAsyncCalls
Running asynchronous calls.
|
private java.util.Deque<RealCall> |
runningSyncCalls
Running synchronous calls.
|
Constructor and Description |
---|
Dispatcher() |
Dispatcher(java.util.concurrent.ExecutorService executorService) |
Modifier and Type | Method and Description |
---|---|
void |
cancelAll()
Cancel all calls currently enqueued or executing.
|
(package private) void |
enqueue(RealCall.AsyncCall call) |
(package private) void |
executed(RealCall call)
Used by
Call#execute to signal it is in-flight. |
java.util.concurrent.ExecutorService |
executorService() |
private RealCall.AsyncCall |
findExistingCallWithHost(java.lang.String host) |
private <T> void |
finished(java.util.Deque<T> calls,
T call) |
(package private) void |
finished(RealCall.AsyncCall call)
Used by
AsyncCall#run to signal completion. |
(package private) void |
finished(RealCall call)
Used by
Call#execute to signal completion. |
int |
getMaxRequests() |
int |
getMaxRequestsPerHost() |
private boolean |
promoteAndExecute()
Promotes eligible calls from
readyAsyncCalls to runningAsyncCalls and runs
them on the executor service. |
java.util.List<Call> |
queuedCalls()
Returns a snapshot of the calls currently awaiting execution.
|
int |
queuedCallsCount() |
java.util.List<Call> |
runningCalls()
Returns a snapshot of the calls currently being executed.
|
int |
runningCallsCount() |
void |
setIdleCallback(java.lang.Runnable idleCallback)
Set a callback to be invoked each time the dispatcher becomes idle (when the number of running
calls returns to zero).
|
void |
setMaxRequests(int maxRequests)
Set the maximum number of requests to execute concurrently.
|
void |
setMaxRequestsPerHost(int maxRequestsPerHost)
Set the maximum number of requests for each host to execute concurrently.
|
private int maxRequests
private int maxRequestsPerHost
@Nullable private java.lang.Runnable idleCallback
@Nullable private java.util.concurrent.ExecutorService executorService
private final java.util.Deque<RealCall.AsyncCall> readyAsyncCalls
private final java.util.Deque<RealCall.AsyncCall> runningAsyncCalls
private final java.util.Deque<RealCall> runningSyncCalls
public Dispatcher(java.util.concurrent.ExecutorService executorService)
public Dispatcher()
public java.util.concurrent.ExecutorService executorService()
public void setMaxRequests(int maxRequests)
If more than maxRequests
requests are in flight when this is invoked, those requests
will remain in flight.
public int getMaxRequests()
public void setMaxRequestsPerHost(int maxRequestsPerHost)
If more than maxRequestsPerHost
requests are in flight when this is invoked, those
requests will remain in flight.
WebSocket connections to hosts do not count against this limit.
public int getMaxRequestsPerHost()
public void setIdleCallback(@Nullable java.lang.Runnable idleCallback)
Note: The time at which a call is considered idle is different depending
on whether it was run asynchronously or
synchronously. Asynchronous calls become idle after the
onResponse
or onFailure
callback has
returned. Synchronous calls become idle once execute()
returns. This
means that if you are doing synchronous calls the network layer will not truly be idle until
every returned Response
has been closed.
void enqueue(RealCall.AsyncCall call)
@Nullable private RealCall.AsyncCall findExistingCallWithHost(java.lang.String host)
public void cancelAll()
private boolean promoteAndExecute()
readyAsyncCalls
to runningAsyncCalls
and runs
them on the executor service. Must not be called with synchronization because executing calls
can call into user code.void executed(RealCall call)
Call#execute
to signal it is in-flight.void finished(RealCall.AsyncCall call)
AsyncCall#run
to signal completion.void finished(RealCall call)
Call#execute
to signal completion.private <T> void finished(java.util.Deque<T> calls, T call)
public java.util.List<Call> queuedCalls()
public java.util.List<Call> runningCalls()
public int queuedCallsCount()
public int runningCallsCount()