Retry
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
The configuration for the retry mechanism.
See also
Properties
A mutable shared flow that emits events of type Event. Such events can be listened to by:
Functions
Cancels all listeners registered. Subsequent registrations should not be affected.
Decorates a BiFunction with this retry mechanism.
Decorates a BiFunction with this retry mechanism by providing an additional RetryContext.
Decorates a Function with this retry mechanism by providing an additional RetryContext.
Decorates a Supplier with this retry mechanism by providing an additional RetryContext.
Provides a default result mapper for decorating functions which returns the result unmodified or applies the exception handler from the configuration.
Executes a Supplier with this retry mechanism.
Executes an operation with this retry mechanism.
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.
Executes the given action when a retry event occurs. This function can be used to listen to all retry events.
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).
Executes the given action when a retry is attempted. The underlying event is emitted when a retry is attempted, before entering the delay phase.
Registers a listener that will be called when a specific subtype of Event is emitted.
Executes the given action when a retry is successful. The underlying event is emitted when the operation is completed successfully after a retry.