SingleFlight

class SingleFlight<K, T>

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

The first caller for a key runs its block; every caller that arrives while that execution is in flight waits for it and receives the same result or failure. When the execution finishes, the key is released and the next caller starts a fresh one, so completed keys hold no memory.

Thread safety: this class is safe for concurrent use from multiple coroutines and threads; all internal state is guarded by a Mutex.

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard
suspend fun inFlightKeyCount(): Int

Returns the number of keys with an execution currently in flight.

Link copied to clipboard
suspend fun isInFlight(key: K): Boolean

Returns true while an execution for key is in flight.

Link copied to clipboard
suspend fun run(key: K, block: suspend () -> T): T

Runs block for key, or joins an execution already in flight for an equal key.