WeakTimeoutQueue

expect class WeakTimeoutQueue(timeoutMillis: Long, clock: () -> Long = { GMTDate().timestamp })
actual class WeakTimeoutQueue(val 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

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

Constructors

Link copied to clipboard
expect fun WeakTimeoutQueue(timeoutMillis: Long, clock: () -> Long = { GMTDate().timestamp })
actual fun WeakTimeoutQueue(timeoutMillis: Long, clock: () -> Long)
actual fun WeakTimeoutQueue(timeoutMillis: Long, clock: () -> Long)

Functions

Link copied to clipboard
expect fun cancel()
actual fun cancel()

Cancel all registered timeouts.

actual fun cancel()
Link copied to clipboard
expect fun process()
actual fun process()

Process and cancel all jobs that are timed out

actual fun process()
Link copied to clipboard
expect suspend fun <T> withTimeout(block: suspend CoroutineScope.() -> T): T
actual 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.

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

Properties

Link copied to clipboard
expect val timeoutMillis: Long
actual val timeoutMillis: Long
actual val timeoutMillis: Long