ExponentialBackoff

data class ExponentialBackoff(val maxAttempts: Int = DEFAULT_MAX_ATTEMPTS, val initialDelay: Duration = DEFAULT_INITIAL_DELAY, val maxDelay: Duration = DEFAULT_MAX_DELAY, val backoffFactor: Double = DEFAULT_BACKOFF_FACTOR, val jitter: Double = DEFAULT_JITTER) : RetryPolicy

A RetryPolicy that retries every failure with exponentially growing, capped and jittered delays.

Constructors

Link copied to clipboard
constructor(maxAttempts: Int = DEFAULT_MAX_ATTEMPTS, initialDelay: Duration = DEFAULT_INITIAL_DELAY, maxDelay: Duration = DEFAULT_MAX_DELAY, backoffFactor: Double = DEFAULT_BACKOFF_FACTOR, jitter: Double = DEFAULT_JITTER)

Types

Link copied to clipboard
class Builder

Fluent builder for ExponentialBackoff, provided primarily for Java callers. Any property left unset falls back to the same default as the constructor.

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Multiplier applied to the delay after each retry. Defaults to 2.0.

Link copied to clipboard

Delay before the first retry. Defaults to 1 second.

Link copied to clipboard

Randomness factor, not a duration: each delay is multiplied by a random factor in [1 - jitter, 1 + jitter], never going below zero, after being capped low enough that the result stays within maxDelay; 0 removes randomness. Defaults to 1.0.

Link copied to clipboard

Attempts including the first, so 1 disables retries. Defaults to 5.

Link copied to clipboard

Upper bound on any single delay. Defaults to 60 seconds.

Functions

Link copied to clipboard

Returns initialDelay in whole milliseconds. Java cannot read initialDelay (mangled).

Link copied to clipboard

Returns maxDelay in whole milliseconds. Java cannot read maxDelay (mangled).

Link copied to clipboard
open override fun nextRetryDelay(failedAttempts: Int): Duration

Returns the delay before the next attempt once failedAttempts attempts have failed, and is called only after shouldRetry returned true. A zero or negative delay retries at once, and Duration.INFINITE waits until the retrying coroutine is canceled.

Link copied to clipboard
open override fun shouldRetry(failure: Exception, failedAttempts: Int): Boolean

Returns whether to retry once failedAttempts attempts (at least 1) have failed, the last with failure.

Link copied to clipboard

Returns a Builder initialized with this instance's properties, primarily for Java callers. Prefer it over copy from Java: copy takes every property positionally, so its signature changes whenever a property is added.