Retry

@Target(allowedTargets = [AnnotationTarget.FUNCTION])
annotation class Retry(val enabled: Boolean = true, val maxRetries: Int = DEFAULT_MAX_RETRIES, val minBackoff: Int = DEFAULT_MIN_BACKOFF, val executionTimeout: Int = DEFAULT_EXECUTION_TIMEOUT, val recoverable: Array<KClass<out Throwable>> = [], val unrecoverable: Array<KClass<out Throwable>> = [])

Enables retry mechanism for annotated functions with configurable retry policies.

This annotation provides resilient error handling by automatically retrying failed operations according to specified policies. It's essential for handling transient failures in distributed systems, network operations, and external service calls.

The retry mechanism supports:

  • Exponential backoff with jitter

  • Configurable retry counts and timeouts

  • Selective exception handling (recoverable vs unrecoverable)

  • Circuit breaker patterns integration

Example usage:

class PaymentService {

@Retry(
maxRetries = 3,
minBackoff = 1,
recoverable = [IOException::class, TimeoutException::class],
unrecoverable = [IllegalArgumentException::class]
)
fun processPayment(request: PaymentRequest): PaymentResult {
// Network call that might fail temporarily
return paymentGateway.charge(request)
}

@Retry(enabled = false) // Disable retries for this operation
fun validatePayment(request: PaymentRequest) {
// Validation that should fail fast
require(request.amount > 0) { "Amount must be positive" }
}
}

Parameters

enabled

Whether retry functionality is enabled. Defaults to true.

maxRetries

Maximum number of retry attempts. Defaults to DEFAULT_MAX_RETRIES.

minBackoff

Minimum backoff duration in seconds before first retry. Uses exponential backoff for subsequent retries. See java.time.temporal.ChronoUnit.SECONDS for time units.

executionTimeout

Maximum timeout in seconds for each execution attempt. Prevents hanging operations. See java.time.temporal.ChronoUnit.SECONDS.

recoverable

Array of exception types that should trigger retries when encountered. Only these exceptions will cause retry attempts.

unrecoverable

Array of exception types that should NOT trigger retries. These exceptions fail immediately without retry.

See also

for the default retry limit

for the default backoff duration

for the default execution timeout

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
val enabled: Boolean = true
Link copied to clipboard

Maximum execution timeout in seconds for each retry attempt.

Link copied to clipboard

Maximum number of retry attempts.

Link copied to clipboard

Minimum backoff duration in seconds before the first retry.

Link copied to clipboard
Link copied to clipboard