Skip to content

A Swift nano-library for retrying asynchronous operations

License

Notifications You must be signed in to change notification settings

thecoolwinter/Retry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🔄 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
}

About

A Swift nano-library for retrying asynchronous operations

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages