AsyncTimeout

open class AsyncTimeout : Timeout

This timeout uses a background thread to take action exactly when the timeout occurs. Use this to implement timeouts where they aren't supported natively, such as to sockets that are blocked on writing.

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 and 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 afterward. 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.

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard
open fun awaitSignal(condition: Condition)

Waits on monitor until it is signaled. Throws InterruptedIOException if either the thread is interrupted or if this timeout elapses before monitor is signaled. The caller must hold the lock that monitor is bound to.

Link copied to clipboard
open override fun cancel()

Prevent all current applications of this timeout from firing. Use this when a time-limited operation should no longer be time-limited because the nature of the operation has changed.

Link copied to clipboard

Clears the deadline.

Link copied to clipboard
open fun clearTimeout(): Timeout

Clears the timeout. Operating system timeouts may still apply.

Link copied to clipboard
fun deadline(duration: Long, unit: TimeUnit): Timeout

Set a deadline of now plus duration time.

Link copied to clipboard

Returns the nano time when the deadline will be reached.

open fun deadlineNanoTime(deadlineNanoTime: Long): Timeout

Sets the nano time when the deadline will be reached. All operations must complete before this time. Use a deadline to set a maximum bound on the time spent on a sequence of operations.

Link copied to clipboard
fun enter()
Link copied to clipboard
fun exit(): Boolean

Returns true if the timeout occurred.

Link copied to clipboard
open fun hasDeadline(): Boolean

Returns true if a deadline is enabled.

Link copied to clipboard
inline fun <T> intersectWith(other: Timeout, block: () -> T): T

Applies the minimum intersection between this timeout and other, run block, then finally rollback this timeout's values.

Link copied to clipboard
fun sink(sink: Sink): Sink

Returns a new sink that delegates to sink, using this to implement timeouts. This works best if timedOut is overridden to interrupt sink's current operation.

Link copied to clipboard
fun source(source: Source): Source

Returns a new source that delegates to source, using this to implement timeouts. This works best if timedOut is overridden to interrupt source's current operation.

Link copied to clipboard
open fun throwIfReached()

Throws an InterruptedIOException if the deadline has been reached or if the current thread has been interrupted. This method doesn't detect timeouts; that should be implemented to asynchronously abort an in-progress operation.

Link copied to clipboard
open fun timeout(timeout: Long, unit: TimeUnit): Timeout

Wait at most timeout time before aborting an operation. Using a per-operation timeout means that as long as forward progress is being made, no sequence of operations will fail.

Link copied to clipboard
fun Timeout.timeout(timeout: Long, unit: DurationUnit): Timeout
Link copied to clipboard
open fun timeoutNanos(): Long

Returns the timeout in nanoseconds, or 0 for no timeout.

Link copied to clipboard
open fun waitUntilNotified(monitor: Any)

Waits on monitor until it is notified. Throws InterruptedIOException if either the thread is interrupted or if this timeout elapses before monitor is notified. The caller must be synchronized on monitor.

Link copied to clipboard
inline fun <T> withTimeout(block: () -> T): T

Surrounds block with calls to enter and exit, throwing an exception from newTimeoutException if a timeout occurred.