throttle

fun <T> Flow<T>.throttle(elementsPerInterval: Int, interval: Duration, strategy: ThrottleStrategy = ThrottleStrategy.Suspend): Flow<T>

Throttles the emission of elements from the Flow based on the specified elementsPerInterval, interval, and strategy.

Return

A new Flow with throttling applied based on the specified parameters.

Example usage:

flowOf(1, 2, 3, 4, 5)
.throttle(elementsPerInterval = 2, interval = 1.seconds, strategy = ThrottleStrategy.Suspend)
// Output: 1, 2, (1s delay), 3, 4, (1s delay), 5

Parameters

elementsPerInterval

The maximum number of elements allowed to be emitted per interval.

interval

A Duration specifying the time interval for throttling the flow.

strategy

The ThrottleStrategy to apply when the flow exceeds the specified rate. Defaults to ThrottleStrategy.Suspend. ThrottleStrategy.Suspend will suspend the emission until the next interval. ThrottleStrategy.Drop will drop the element if the rate is exceeded.


fun <T, P> Flow<T>.throttle(strategy: ThrottleStrategy = ThrottleStrategy.Suspend, semaphore: suspend CoroutineScope.() -> AsyncSemaphore<P>): Flow<T>

Throttles the emission of elements from the Flow based on the specified semaphore, interval, and strategy.

Return

A new Flow with throttling applied based on the specified parameters.

Example usage:

flowOf(1, 2, 3, 4, 5)
.throttle(strategy = ThrottleStrategy.Suspend) {
// custom semaphore implementation
}

Parameters

strategy

The ThrottleStrategy to apply when the flow exceeds the specified rate. Defaults to ThrottleStrategy.Suspend. ThrottleStrategy.Suspend will suspend the emission until the next interval. ThrottleStrategy.Drop will drop the element if the rate is exceeded.

semaphore

The AsyncSemaphore that controls the emission of the elements.