WeakTimeoutQueue

class WeakTimeoutQueue

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:

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

Types

Registration

interface Registration : CompletionHandler, DisposableHandle

register function result

Constructors

<init>

WeakTimeoutQueue(
    timeoutMillis: Long,
    clock: () -> Long = { System.currentTimeMillis() })

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.

Properties

timeoutMillis

val timeoutMillis: Long

Functions

cancel

fun cancel(): Unit

Cancel all registered timeouts

process

fun process(): Unit

Process and cancel all jobs that are timed out

register

fun register(job: Job): Registration

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

withTimeout

suspend fun <T> withTimeout(
    block: suspend CoroutineScope.() -> T
): T

Execute block and cancel if doesn’t complete in time.