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.
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")
]
)
]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 -> Resultnonisolated(nonsending)
public func retryIndefinite<Result, ErrorType: Error>(
backoffFactor: Int = 2,
backoffDuration: Duration = .milliseconds(100),
tolerance: Duration? = nil,
operation: () async throws(ErrorType) -> Result,
) async throws -> ResultAs 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
}