WeakTimeoutQueue

class WeakTimeoutQueue(timeoutMillis: Long, clock: () -> Long)

It provides ability to cancel jobs and schedule coroutine with timeout. Unlike regular withTimeout this implementation is never scheduling timer tasks but only checks for current time. This makes timeout measurement much cheaper and doesn't require any watchdog thread.

There are two limitations:

  • timeout period is fixed

  • job cancellation is not guaranteed if no new jobs scheduled

The last one limitation is generally unacceptable however in the particular use-case (closing IDLE connection) it is just fine as we really don't care about stalling IDLE connections if there are no more incoming

Constructors

Link copied to clipboard
fun WeakTimeoutQueue(timeoutMillis: Long, clock: () -> Long = { System.currentTimeMillis() })

Types

Link copied to clipboard
interface Registration : Function1<cause: Throwable?, Unit> , DisposableHandle

register function result

Functions

Link copied to clipboard
fun cancel()

Cancel all registered timeouts

Link copied to clipboard
fun process()

Process and cancel all jobs that are timed out

Link copied to clipboard
fun register(job: Job): WeakTimeoutQueue.Registration

Register job in this queue. It will be cancelled if doesn't complete in time.

Link copied to clipboard
suspend fun <T> withTimeout(block: suspend CoroutineScope.() -> T): T

Execute block and cancel if doesn't complete in time. Unlike the regular kotlinx.coroutines withTimeout, this also checks for cancellation first and fails immediately.

Properties

Link copied to clipboard
val timeoutMillis: Long