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

Constructors

AsyncTimeout
Link copied to clipboard
fun AsyncTimeout()

Types

Companion
Link copied to clipboard
object Companion

Functions

clearDeadline
Link copied to clipboard
open fun clearDeadline(): Timeout
Clears the deadline.
clearTimeout
Link copied to clipboard
open fun clearTimeout(): Timeout
Clears the timeout.
deadline
Link copied to clipboard
fun deadline(duration: Long, unit: TimeUnit): Timeout
Set a deadline of now plus duration time.
deadlineNanoTime
Link copied to clipboard
open fun deadlineNanoTime(): Long
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.
enter
Link copied to clipboard
fun enter()
equals
Link copied to clipboard
open operator fun equals(other: Any?): Boolean
exit
Link copied to clipboard
fun exit(): Boolean
Returns true if the timeout occurred.
hasDeadline
Link copied to clipboard
open fun hasDeadline(): Boolean
Returns true if a deadline is enabled.
hashCode
Link copied to clipboard
open fun hashCode(): Int
intersectWith
Link copied to clipboard
inline fun intersectWith(other: Timeout, block: () -> Unit)
Applies the minimum intersection between this timeout and other, run block, then finally rollback this timeout's values.
sink
Link copied to clipboard
fun sink(sink: Sink): Sink
Returns a new sink that delegates to sink, using this to implement timeouts.
source
Link copied to clipboard
fun source(source: Source): Source
Returns a new source that delegates to source, using this to implement timeouts.
throwIfReached
Link copied to clipboard
open fun throwIfReached()
Throws an InterruptedIOException if the deadline has been reached or if the current thread has been interrupted.
timeout
Link copied to clipboard
open fun timeout(timeout: Long, unit: TimeUnit): Timeout
Wait at most timeout time before aborting an operation.
timeoutNanos
Link copied to clipboard
open fun timeoutNanos(): Long
Returns the timeout in nanoseconds, or 0 for no timeout.
toString
Link copied to clipboard
open fun toString(): String
waitUntilNotified
Link copied to clipboard
fun waitUntilNotified(monitor: Any)
Waits on monitor until it is notified.
withTimeout
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.