Retry

class Retry(val config: RetryConfig = defaultRetryConfig()) : FlowEventListenerImpl<RetryEvent> (source)

A Retry is a reactive resilience mechanism that can be used to retry an operation when it fails and the failure is a transient (temporary) fault. Operations can be decorated and executed on demand. A retry mechanism is initialized with a RetryConfig that, through pre-configured policies, define its behaviour.

The retry mechanism implements the following state machine:

                       +------------------+  retried once   +---------+
+-----------+ ---> | Returns Normally | --------------> | Success |
| Operation | +------------------+ +---------+
| Called | +-------+ +----------+
+-----------+ ---> | Fails | ---> | Consults |
^ +-------+ | Policies |
| +----------+
| |
+-------+ can use retry |
| Retry | <--------------------------|
+-------+ | expected
| failure +-------+
|------------> | Error |
| +-------+
+---------+ |
| Ignored | <------|
| Error |
+---------+

Examples of usage:

// use predefined retry policies
val retry = Retry()

// use custom policies
val retry = Retry(
retryConfig {
maxAttempts = 5 // initial call + 4 retries
retryIf { it is NetworkError }
retryOnResult { it is "success" }
constantDelay(500.milliseconds)
// customDelay { attempt, context -> ... }
// exceptionHandler { exception -> ... }
}
)

// execute a supplier
retry.executeSupplier {
// operation
}

// execute a supplier with a context
retry.executeCtxSupplier { ctx ->
// operation
}

// decorate a supplier
val decoratedSupplier = retry.decorateSupplier {
// operation
}
// and call it later
val result = decoratedSupplier()

// listen to specific events
retry.onRetry { event -> println(event) }

// listen to all events
retry.onEvent { event -> println(event) }

// cancel all registered listeners
retry.cancelListeners()

Parameters

config

The configuration for the retry mechanism.

See also

Constructors

Link copied to clipboard
constructor(config: RetryConfig = defaultRetryConfig())

Properties

Link copied to clipboard
Link copied to clipboard
open override val events: MutableSharedFlow<RetryEvent>

A mutable shared flow that emits events of type Event. Such events can be listened to by:

Link copied to clipboard
open override val scope: CoroutineScope

The scope in which each listener will be launched upon registration.

Functions

Link copied to clipboard
open override fun cancelListeners()

Cancels all listeners registered. Subsequent registrations should not be affected.

Link copied to clipboard
inline fun <A, B, R> decorateBiFunction(crossinline block: BiFunction<A, B, R>): BiFunction<A, B, R?>

Decorates a BiFunction with this retry mechanism.

Link copied to clipboard
inline fun <A, B, R, T> decorateCtxBiFunction(noinline resultMapper: ResultMapper<R, T> = defaultResultMapper(), crossinline block: CtxBiFunction<RetryContext, A, B, R>): BiFunction<A, B, T?>

Decorates a BiFunction with this retry mechanism by providing an additional RetryContext.

Link copied to clipboard
inline fun <A, R, T> decorateCtxFunction(noinline resultMapper: ResultMapper<R, T> = defaultResultMapper(), crossinline block: CtxFunction<RetryContext, A, R>): Function<A, T?>

Decorates a Function with this retry mechanism by providing an additional RetryContext.

Link copied to clipboard
inline fun <R, T> decorateCtxSupplier(noinline resultMapper: ResultMapper<R, T> = defaultResultMapper(), crossinline block: CtxSupplier<RetryContext, R>): Supplier<T?>

Decorates a Supplier with this retry mechanism by providing an additional RetryContext.

Link copied to clipboard
inline fun <A, R> decorateFunction(crossinline block: Function<A, R>): Function<A, R?>

Decorates a Function with this retry mechanism.

Link copied to clipboard
inline fun <R> decorateSupplier(crossinline block: Supplier<R>): Supplier<R?>

Decorates a Supplier with this retry mechanism.

Link copied to clipboard

Provides a default result mapper for decorating functions which returns the result unmodified or applies the exception handler from the configuration.

Link copied to clipboard
inline suspend fun <R, T> executeCtxSupplier(noinline resultMapper: ResultMapper<R, T> = defaultResultMapper(), crossinline block: CtxSupplier<RetryContext, R>): T?

Executes a Supplier with this retry mechanism.

Link copied to clipboard
internal suspend fun <A, B, R, T> executeOperation(inputA: A, inputB: B, resultMapper: ResultMapper<R, T>, block: CtxBiFunction<RetryContext, A, B, R>): T?

Executes an operation with this retry mechanism.

Link copied to clipboard
inline suspend fun <R> executeSupplier(crossinline block: Supplier<R>): R?

Executes a Supplier with this retry mechanism.

Link copied to clipboard
suspend fun onError(action: suspend (RetryEvent.RetryOnError) -> Unit): Job

Executes the given action when an error occurs during a retry attempt. The underlying event is emitted when an exception occurs during a retry attempt.

Link copied to clipboard
open suspend override fun onEvent(action: suspend (RetryEvent) -> Unit): Job

Executes the given action when a retry event occurs. This function can be used to listen to all retry events.

Link copied to clipboard
suspend fun onIgnoredError(action: suspend (RetryEvent.RetryOnIgnoredError) -> Unit): Job

Executes the given action when an error is ignored during a retry attempt. The underlying event is emitted when a retry is not needed to complete the operation (e.g., the exception that occurred is not a retryable exception).

Link copied to clipboard
suspend fun onRetry(action: suspend (RetryEvent.RetryOnRetry) -> Unit): Job

Executes the given action when a retry is attempted. The underlying event is emitted when a retry is attempted, before entering the delay phase.

Link copied to clipboard
protected inline suspend fun <EventType : RetryEvent> onSpecificEvent(crossinline action: suspend (EventType) -> Unit): Job

Registers a listener that will be called when a specific subtype of Event is emitted.

Link copied to clipboard
suspend fun onSuccess(action: suspend (RetryEvent.RetryOnSuccess) -> Unit): Job

Executes the given action when a retry is successful. The underlying event is emitted when the operation is completed successfully after a retry.