Giter Site home page Giter Site logo

fumoboy007 / swift-retry Goto Github PK

View Code? Open in Web Editor NEW
103.0 103.0 0.0 75 KB

Retries in Swift with sensible defaults and powerful flexibility.

Home Page: https://fumoboy007.github.io/swift-retry/documentation/retry/

License: MIT License

Swift 100.00%
async-await backoff-algorithms backoff-strategy exponential-backoff jitter retries retry retrying swift swift-concurrency

swift-retry's Introduction

swift-retry

Retries in Swift with sensible defaults and powerful flexibility.

Swift 5.9 Linux, visionOS 1, macOS 13, iOS 16, tvOS 16, watchOS 9 MIT License Automated Tests Workflow Status

Basic Usage

try await retry {
   try await doSomething()
}

See the documentation for examples of more advanced use cases.

Overview

Designed for Swift Concurrency

The retry function is an async function that runs the given async closure repeatedly until it succeeds or until the failure is no longer retryable. The function sleeps in between attempts while respecting task cancellation.

Sensible Defaults

The library uses similar defaults as Amazon Web Services and Google Cloud.

An important but often overlooked default is the choice of backoff algorithm, which determines how long to sleep in between attempts. This library chooses an exponential backoff algorithm by default, which is suitable for most use cases. Most retry use cases involve a resource, such as a server, with potentially many clients where an exponential backoff algorithm would be ideal to avoid DDoSing the resource.

Powerful Flexibility

The API provides several customization points to accommodate any use case:

  • Retries can be selectively enabled or disabled for specific error cases by providing a custom recoverFromFailure closure. Retries can also be selectively enabled or disabled for specific code paths by wrapping thrown errors with Retryable or NotRetryable.
  • The RetryConfiguration type encapsulates the retry behavior so that it can be reused across multiple call sites without duplicating code.
  • The Backoff type represents the choice of algorithm that will be used to determine how long to sleep in between attempts. It has built-in support for common algorithms but can be initialized with a custom BackoffAlgorithm implementation if needed.
  • The clock that is used to sleep in between attempts can be replaced. For example, one might use a fake Clock implementation in automated tests to ensure the tests are deterministic and efficient.

Safe Retries

The module exposes a RetryableRequest protocol to add safe retry methods to a conforming request type. The retry methods in the protocol are similar to the top-level retry functions, but safer. The retry methods in the protocol enforce that the request is idempotent since it is unsafe to retry a non-idempotent request.

To retry HTTP requests, consider using the swift-http-error-handling package, which adds RetryableRequest conformance to the standard HTTPRequest type.

swift-retry's People

Contributors

fumoboy007 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.