Contents

thecoolwinter/retry

Retry is a nano-library for retrying asynchronous operations. Supports cancellation, customizable backoff duration and exponential factor, and calculates random jitter to avoid the thundering herd problem.

Installation

You can use the Swift Package Manager to download and import the library into your project:

dependencies: [
    .package(url: "https://github.com/thecoolwinter/Retry.git", from: "1.0.0")
]

Then under targets:

targets: [
    .target(
    	// ...
        dependencies: [
            .product(name: "Retry", package: "Retry")
        ]
    )
]

API

The following two functions are exposed by this library. See their in-source documentation for more details.

nonisolated(nonsending)
public func retry<Result, ErrorType: Error>(
    maxAttempts: Int,
    backoffFactor: Int = 2,
    backoffDuration: Duration = .milliseconds(100),
    tolerance: Duration? = nil,
    operation: () async throws(ErrorType) -> Result,
) async throws -> Result
nonisolated(nonsending)
public func retryIndefinite<Result, ErrorType: Error>(
    backoffFactor: Int = 2,
    backoffDuration: Duration = .milliseconds(100),
    tolerance: Duration? = nil,
    operation: () async throws(ErrorType) -> Result,
) async throws -> Result

As well as a BackoffStrategy struct, that calculates exponential backoff.

public struct BackoffStrategy {
    public init(factor: Int = 2, initial: Duration = .milliseconds(100))
    public mutating func nextDuration() -> Duration
}

Package Metadata

Repository: thecoolwinter/retry

Default branch: main

README: README.md