public class AsyncTimeout extends Timeout
Subclasses should override timedOut()
to take action when a timeout occurs. This method
will be invoked by the shared watchdog thread so it should not do any long-running operations.
Otherwise we risk starving other timeouts from being triggered.
Use sink(okio.Sink)
and source(okio.Source)
to apply this timeout to a stream. The returned value
will apply the timeout to each operation on the wrapped stream.
Callers should call enter()
before doing work that is subject to timeouts, and exit()
afterwards. The return value of exit()
indicates whether a timeout was triggered.
Note that the call to timedOut()
is asynchronous, and may be called after exit()
.
Modifier and Type | Class and Description |
---|---|
private static class |
AsyncTimeout.Watchdog |
Modifier and Type | Field and Description |
---|---|
(package private) static AsyncTimeout |
head
The watchdog thread processes a linked list of pending timeouts, sorted in the order to be
triggered.
|
private static long |
IDLE_TIMEOUT_MILLIS
Duration for the watchdog thread to be idle before it shuts itself down.
|
private static long |
IDLE_TIMEOUT_NANOS |
private boolean |
inQueue
True if this node is currently in the queue.
|
private AsyncTimeout |
next
The next node in the linked list.
|
private static int |
TIMEOUT_WRITE_SIZE
Don't write more than 64 KiB of data at a time, give or take a segment.
|
private long |
timeoutAt
If scheduled, this is the time that the watchdog should time this out.
|
Constructor and Description |
---|
AsyncTimeout() |
Modifier and Type | Method and Description |
---|---|
(package private) static AsyncTimeout |
awaitTimeout()
Removes and returns the node at the head of the list, waiting for it to time out if necessary.
|
private static boolean |
cancelScheduledTimeout(AsyncTimeout node)
Returns true if the timeout occurred.
|
void |
enter() |
boolean |
exit()
Returns true if the timeout occurred.
|
(package private) void |
exit(boolean throwOnTimeout)
Throws an IOException if
throwOnTimeout is true and a timeout occurred. |
(package private) java.io.IOException |
exit(java.io.IOException cause)
Returns either
cause or an IOException that's caused by cause if a timeout
occurred. |
protected java.io.IOException |
newTimeoutException(java.io.IOException cause)
Returns an
IOException to represent a timeout. |
private long |
remainingNanos(long now)
Returns the amount of time left until the time out.
|
private static void |
scheduleTimeout(AsyncTimeout node,
long timeoutNanos,
boolean hasDeadline) |
Sink |
sink(Sink sink)
Returns a new sink that delegates to
sink , using this to implement timeouts. |
Source |
source(Source source)
Returns a new source that delegates to
source , using this to implement timeouts. |
protected void |
timedOut()
|
clearDeadline, clearTimeout, deadline, deadlineNanoTime, deadlineNanoTime, hasDeadline, minTimeout, throwIfReached, timeout, timeoutNanos, waitUntilNotified
private static final int TIMEOUT_WRITE_SIZE
private static final long IDLE_TIMEOUT_MILLIS
private static final long IDLE_TIMEOUT_NANOS
@Nullable static AsyncTimeout head
Head's 'next' points to the first element of the linked list. The first element is the next
node to time out, or null if the queue is empty. The head is null until the watchdog thread is
started and also after being idle for IDLE_TIMEOUT_MILLIS
.
private boolean inQueue
@Nullable private AsyncTimeout next
private long timeoutAt
public final void enter()
private static void scheduleTimeout(AsyncTimeout node, long timeoutNanos, boolean hasDeadline)
public final boolean exit()
private static boolean cancelScheduledTimeout(AsyncTimeout node)
private long remainingNanos(long now)
protected void timedOut()
public final Sink sink(Sink sink)
sink
, using this to implement timeouts. This works
best if timedOut()
is overridden to interrupt sink
's current operation.public final Source source(Source source)
source
, using this to implement timeouts. This
works best if timedOut()
is overridden to interrupt sink
's current operation.final void exit(boolean throwOnTimeout) throws java.io.IOException
throwOnTimeout
is true
and a timeout occurred. See
newTimeoutException(java.io.IOException)
for the type of exception thrown.java.io.IOException
final java.io.IOException exit(java.io.IOException cause) throws java.io.IOException
cause
or an IOException that's caused by cause
if a timeout
occurred. See newTimeoutException(java.io.IOException)
for the type of exception
returned.java.io.IOException
protected java.io.IOException newTimeoutException(@Nullable java.io.IOException cause)
IOException
to represent a timeout. By default this method returns InterruptedIOException
. If cause
is non-null it is set as the cause of the
returned exception.@Nullable static AsyncTimeout awaitTimeout() throws java.lang.InterruptedException
head
if there was no node at the head of the list when starting, and
there continues to be no node after waiting IDLE_TIMEOUT_NANOS
. It returns null if a
new node was inserted while waiting. Otherwise this returns the node being waited on that has
been removed.java.lang.InterruptedException