Package-level declarations

Types

Link copied to clipboard
class Debouncer(val window: Duration, scope: CoroutineScope)

Runs only the last block submitted within a time window, discarding earlier ones.

Link copied to clipboard
class KeyedMutex<K>

A map of independent mutexes keyed by K, with mutual exclusion per key.

Link copied to clipboard
class SingleFlight<K, T>

Deduplicates concurrent work by key: callers with equal keys share one in-flight execution.

Link copied to clipboard
class SuspendLazy<T>

A value initialized on first get, with one shared initialization across concurrent callers.

Link copied to clipboard
class Throttler(val window: Duration, timeSource: TimeSource.WithComparableMarks = TimeSource.Monotonic)

Runs at most one block per time window, rejecting calls that arrive too soon.

Functions

Link copied to clipboard
suspend fun <T> List<Deferred<T>>.awaitAllSettled(): List<Result<T>>

Awaits every deferred value and returns each outcome as a Result, in the original order.

Link copied to clipboard
suspend fun <T> Deferred<T>.awaitOrDefault(timeout: Duration, default: T): T

Awaits this deferred value and returns default if timeout elapses first.

Link copied to clipboard
suspend fun <T> Deferred<T>.awaitOrNull(timeout: Duration): T?

Awaits this deferred value and returns null if timeout elapses first.

Link copied to clipboard
suspend fun <T> firstSuccessOf(vararg blocks: suspend CoroutineScope.() -> T): T

Runs every block concurrently and returns the result of the first one to succeed.

Link copied to clipboard
fun CoroutineScope.launchCatching(onError: (Throwable) -> Unit, block: suspend CoroutineScope.() -> Unit): Job

Launches a coroutine that reports failures of block to onError instead of the scope.

Link copied to clipboard
fun CoroutineScope.launchDelayed(delay: Duration, block: suspend CoroutineScope.() -> Unit): Job

Launches a coroutine that runs block after delay.

Link copied to clipboard
fun CoroutineScope.launchPeriodic(interval: Duration, initialDelay: Duration = Duration.ZERO, action: suspend () -> Unit): Job

Launches a coroutine that runs action repeatedly with a fixed pause between runs.

Link copied to clipboard
suspend fun <T> Iterable<T>.parallelFilter(concurrency: Int = Int.MAX_VALUE, predicate: suspend (T) -> Boolean): List<T>

Evaluates predicate for every element concurrently and returns the matching elements in the original order.

Link copied to clipboard
suspend fun <T> Iterable<T>.parallelFilterNot(concurrency: Int = Int.MAX_VALUE, predicate: suspend (T) -> Boolean): List<T>

Evaluates predicate for every element concurrently and returns the non-matching elements in the original order.

Link copied to clipboard
suspend fun <T, R> Iterable<T>.parallelFlatMap(concurrency: Int = Int.MAX_VALUE, transform: suspend (T) -> Iterable<R>): List<R>

Transforms every element concurrently and returns the flattened results in the original order.

Link copied to clipboard
suspend fun <T> Iterable<T>.parallelForEach(concurrency: Int = Int.MAX_VALUE, action: suspend (T) -> Unit)

Runs action for every element concurrently and returns when all have finished.

Link copied to clipboard
suspend fun <T> Iterable<T>.parallelForEachIndexed(concurrency: Int = Int.MAX_VALUE, action: suspend (index: Int, element: T) -> Unit)

Runs action for every element with its index concurrently and returns when all have finished.

Link copied to clipboard
suspend fun <T, R> Iterable<T>.parallelMap(concurrency: Int = Int.MAX_VALUE, transform: suspend (T) -> R): List<R>

Transforms every element concurrently and returns the results in the original order.

Link copied to clipboard
suspend fun <T, R> Iterable<T>.parallelMapIndexed(concurrency: Int = Int.MAX_VALUE, transform: suspend (index: Int, element: T) -> R): List<R>

Transforms every element with its index concurrently and returns the results in the original order.

Link copied to clipboard
suspend fun <T, R : Any> Iterable<T>.parallelMapIndexedNotNull(concurrency: Int = Int.MAX_VALUE, transform: suspend (index: Int, element: T) -> R?): List<R>

Transforms every element with its index concurrently and returns the non-null results in the original order.

Link copied to clipboard
suspend fun <T, R : Any> Iterable<T>.parallelMapNotNull(concurrency: Int = Int.MAX_VALUE, transform: suspend (T) -> R?): List<R>

Transforms every element concurrently and returns the non-null results in the original order.

Link copied to clipboard
suspend fun <T> raceOf(vararg blocks: suspend CoroutineScope.() -> T): T

Runs every block concurrently and returns the result of the first one to complete.

Link copied to clipboard
suspend fun <T> retry(times: Int, delayBetween: Duration = Duration.ZERO, block: suspend (attempt: Int) -> T): T

Runs block up to times attempts and returns the first successful result.

Link copied to clipboard
suspend fun <T> retryForever(delayBetween: Duration = Duration.ZERO, retryIf: (Throwable) -> Boolean = { true }, block: suspend (attempt: Int) -> T): T

Runs block repeatedly until it succeeds and returns that first successful result.

Link copied to clipboard
suspend fun <T> retryWithBackoff(times: Int, initialDelay: Duration, factor: Double = 2.0, maxDelay: Duration = Duration.INFINITE, retryIf: (Throwable) -> Boolean = { true }, block: suspend (attempt: Int) -> T): T

Runs block up to times attempts with exponentially growing delays between failures.

Link copied to clipboard
suspend fun <T> runCatchingCancellable(block: suspend () -> T): Result<T>

Runs block and wraps its outcome in a Result, keeping cancellation intact.

Link copied to clipboard
suspend fun <T, R> T.runCatchingCancellable(block: suspend T.() -> R): Result<R>

Runs block with this as its receiver and wraps its outcome in a Result, keeping cancellation intact.

Link copied to clipboard
fun <T> suspendLazy(initializer: suspend () -> T): SuspendLazy<T>

Creates a SuspendLazy backed by initializer.

Link copied to clipboard
inline fun <T> Mutex.tryWithLock(block: () -> T): T?

Runs block while holding this mutex only if the mutex is free, returning null otherwise.

Link copied to clipboard
inline fun <T> Semaphore.tryWithPermit(block: () -> T): T?

Runs block while holding a permit only if one is available, returning null otherwise.

Link copied to clipboard
suspend fun <T> Mutex.withLockOrNull(timeout: Duration, block: suspend () -> T): T?

Runs block while holding this mutex, waiting at most timeout for the lock, returning null on timeout.

Link copied to clipboard
suspend fun <T> Semaphore.withPermitOrNull(timeout: Duration, block: suspend () -> T): T?

Runs block while holding a permit, waiting at most timeout for one, returning null on timeout.

Link copied to clipboard
suspend fun <T> withTimeoutOrDefault(timeout: Duration, default: T, block: suspend CoroutineScope.() -> T): T

Runs block with the given timeout and returns default if the timeout elapses first.