Skip to content

okio / okio / AsyncTimeout

AsyncTimeout

(jvm) 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

Name Summary
(jvm) <init> 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.AsyncTimeout()

Functions

Name Summary
(jvm) enter fun enter():Unit
(jvm) exit Returns true if the timeout occurred.fun exit():Boolean
(jvm) newTimeoutException Returns an 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.open fun newTimeoutException(cause:IOException?):IOException
(jvm) 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.fun sink(sink:Sink):Sink
(jvm) 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.fun source(source:Source):Source
(jvm) timedOut Invoked by the watchdog thread when the time between calls to enter and exit has exceeded the timeout.open fun timedOut():Unit
(jvm) withTimeout Surrounds block with calls to enter and exit, throwing an exception from newTimeoutException if a timeout occurred.fun <T> withTimeout(block: () -> T): T

Inherited Functions

Name Summary
(jvm) clearDeadline Clears the deadline.open fun clearDeadline():Timeout
(jvm) clearTimeout Clears the timeout. Operating system timeouts may still apply.open fun clearTimeout():Timeout
(jvm) deadline Set a deadline of now plus duration time.fun deadline(duration:Long, unit:TimeUnit):Timeout
(jvm) deadlineNanoTime Returns the nano time when the deadline will be reached.open fun deadlineNanoTime():Long
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.open fun deadlineNanoTime(deadlineNanoTime:Long):Timeout
(jvm) hasDeadline Returns true if a deadline is enabled.open fun hasDeadline():Boolean
(jvm) intersectWith Applies the minimum intersection between this timeout and other, run block, then finally rollback this timeout’s values.fun intersectWith(other:Timeout, block: () ->Unit):Unit
(jvm) 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.open fun throwIfReached():Unit
(jvm) 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.open fun timeout(timeout:Long, unit:TimeUnit):Timeout
(jvm) timeoutNanos Returns the timeout in nanoseconds, or 0 for no timeout.open fun timeoutNanos():Long
(jvm) waitUntilNotified 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.fun waitUntilNotified(monitor:Any):Unit