retryWithBudget

suspend fun <T> retryWithBudget(budget: RetryBudget, retries: Long = 3, initialDelay: Duration = 100.milliseconds, maxDelay: Duration = 1.minutes, factor: Double = 2.0, block: suspend () -> T): T(source)

Like retryWithExponentialBackoff, but every retry (not the initial attempt) must first withdraw a token from budget -- see RetryBudget for why this matters. A successful call (whether on the first attempt or a later one) deposits a token back into the budget.

Return

The result of the successful execution

Parameters

budget

Shared RetryBudget -- use the same instance across every call site retrying against a given downstream, so the budget reflects total system-wide retry volume for it.

retries

The maximum number of retry attempts after the initial attempt (defaults to 3)

initialDelay

The initial delay before the first retry (defaults to 100 milliseconds)

maxDelay

The maximum delay between retries (defaults to 1 minute)

factor

The exponential backoff factor (defaults to 2.0)

block

The suspend operation to execute

Throws

if the operation fails and the budget has no tokens left

the original exception, if retries are exhausted with budget still available