-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 751f704
Showing
32 changed files
with
1,896 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
name: Documentation | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
permissions: | ||
contents: read | ||
pages: write | ||
id-token: write | ||
|
||
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. | ||
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. | ||
concurrency: | ||
group: "pages" | ||
cancel-in-progress: false | ||
|
||
jobs: | ||
publish: | ||
name: Publish Documentation | ||
# TODO: Use `macos-latest` after the macOS 13 image graduates to GA. | ||
# https://github.com/actions/runner-images/issues/7508#issuecomment-1718206371 | ||
runs-on: macos-13 | ||
|
||
environment: | ||
name: github-pages | ||
url: ${{ steps.deployment.outputs.page_url }} | ||
|
||
steps: | ||
- name: Set up GitHub Pages | ||
uses: actions/configure-pages@v3 | ||
- uses: maxim-lobanov/setup-xcode@v1 | ||
with: | ||
xcode-version: latest-stable | ||
- name: Print Swift compiler version | ||
run: "swift --version" | ||
- uses: actions/checkout@v3 | ||
- name: Generate documentation | ||
run: "swift package generate-documentation --target Retry --disable-indexing --include-extended-types --transform-for-static-hosting --hosting-base-path swift-retry" | ||
- name: Upload documentation | ||
uses: actions/upload-pages-artifact@v2 | ||
with: | ||
path: ".build/plugins/Swift-DocC/outputs/Retry.doccarchive" | ||
- name: Deploy to GitHub Pages | ||
id: deployment | ||
uses: actions/deploy-pages@v2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name: Tests | ||
|
||
on: [push] | ||
|
||
# TODO: Add Windows job after Swift is added to the Windows images [1] or after | ||
# `swift-actions/setup-swift` supports Swift 5.9+ on Windows [2]. | ||
# 1. https://github.com/actions/runner-images/issues/8281 | ||
# 2. https://github.com/swift-actions/setup-swift/pull/470#issuecomment-1718406382 | ||
jobs: | ||
test-macos: | ||
name: Run Tests on macOS | ||
# TODO: Use `macos-latest` after the macOS 13 image graduates to GA. | ||
# https://github.com/actions/runner-images/issues/7508#issuecomment-1718206371 | ||
runs-on: macos-13 | ||
|
||
steps: | ||
- uses: maxim-lobanov/setup-xcode@v1 | ||
with: | ||
xcode-version: latest-stable | ||
- name: Print Swift compiler version | ||
run: "swift --version" | ||
- uses: actions/checkout@v3 | ||
- name: Run tests | ||
run: "swift test --parallel" | ||
|
||
test-linux: | ||
name: Run Tests on Linux | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Print Swift compiler version | ||
run: "swift --version" | ||
- uses: actions/checkout@v3 | ||
- name: Run tests | ||
run: "swift test --parallel" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/config/registries.json | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
.netrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# MIT License | ||
|
||
Copyright © 2023 Darren Mo. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"pins" : [ | ||
{ | ||
"identity" : "swift-docc-plugin", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-docc-plugin", | ||
"state" : { | ||
"revision" : "26ac5758409154cc448d7ab82389c520fa8a8247", | ||
"version" : "1.3.0" | ||
} | ||
}, | ||
{ | ||
"identity" : "swift-docc-symbolkit", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-docc-symbolkit", | ||
"state" : { | ||
"revision" : "b45d1f2ed151d057b54504d653e0da5552844e34", | ||
"version" : "1.0.0" | ||
} | ||
}, | ||
{ | ||
"identity" : "swift-log", | ||
"kind" : "remoteSourceControl", | ||
"location" : "http://github.com/apple/swift-log", | ||
"state" : { | ||
"revision" : "532d8b529501fb73a2455b179e0bbb6d49b652ed", | ||
"version" : "1.5.3" | ||
} | ||
} | ||
], | ||
"version" : 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// swift-tools-version: 5.9 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "swift-retry", | ||
platforms: [ | ||
.visionOS(.v1), | ||
.macOS(.v13), | ||
.macCatalyst(.v16), | ||
.iOS(.v16), | ||
.tvOS(.v16), | ||
.watchOS(.v9), | ||
], | ||
products: [ | ||
.library( | ||
name: "Retry", | ||
targets: [ | ||
"Retry", | ||
] | ||
), | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/apple/swift-docc-plugin.git", from: "1.3.0"), | ||
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.3"), | ||
], | ||
targets: [ | ||
.target( | ||
name: "Retry", | ||
dependencies: [ | ||
.product(name: "Logging", package: "swift-log"), | ||
] | ||
), | ||
.testTarget( | ||
name: "RetryTests", | ||
dependencies: [ | ||
"Retry", | ||
] | ||
), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# swift-retry | ||
|
||
Retries in Swift with sensible defaults and powerful flexibility. | ||
|
||
![Swift 5.9](https://img.shields.io/badge/swift-v5.9-%23F05138) | ||
![Linux, visionOS 1, macOS 13, iOS 16, tvOS 16, watchOS 9](https://img.shields.io/badge/platform-Linux%20%7C%20visionOS%201%20%7C%20macOS%2013%20%7C%20iOS%2016%20%7C%20tvOS%2016%20%7C%20watchOS%209-blue) | ||
![MIT License](https://img.shields.io/github/license/fumoboy007/swift-retry) | ||
![Automated Tests Workflow Status](https://img.shields.io/github/actions/workflow/status/fumoboy007/swift-retry/tests.yml?event=push&label=tests) | ||
|
||
## Features | ||
|
||
- Designed for [Swift Concurrency](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/concurrency/). | ||
- Sensible defaults for behavior such as the choice of backoff algorithm, the maximum number of attempts, etc. | ||
- Flexible enough for any use case. | ||
- Comprehensive tests and documentation. | ||
|
||
## Usage | ||
|
||
See the [documentation](https://fumoboy007.github.io/swift-retry/documentation/retry/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Use a fake `Clock` type for deterministic and efficient automated tests. | ||
|
||
// snippet.hide | ||
|
||
import Foundation | ||
import Retry | ||
import XCTest | ||
|
||
// snippet.show | ||
|
||
final class MyServiceImplementation<ClockType: Clock> where ClockType.Duration == Duration { | ||
private let clock: ClockType | ||
|
||
init(clock: ClockType) { | ||
self.clock = clock | ||
} | ||
|
||
func doSomethingReliably() async throws { | ||
try await retry(clock: clock) { | ||
try await doSomething() | ||
} | ||
} | ||
} | ||
|
||
final class MyServiceImplementationTests: XCTestCase { | ||
func testDoSomethingReliably_succeeds() async throws { | ||
let myService = MyServiceImplementation(clock: ClockFake()) | ||
try await myService.doSomethingReliably() | ||
} | ||
} | ||
|
||
// snippet.hide | ||
|
||
class ClockFake: Clock, @unchecked Sendable { | ||
typealias Instant = ContinuousClock.Instant | ||
|
||
private let realClock = ContinuousClock() | ||
|
||
private let lock = NSLock() | ||
|
||
private var _now: Instant | ||
|
||
init() { | ||
self._now = realClock.now | ||
} | ||
|
||
var now: Instant { | ||
lock.lock() | ||
defer { | ||
lock.unlock() | ||
} | ||
|
||
return _now | ||
} | ||
|
||
var minimumResolution: Duration { | ||
return realClock.minimumResolution | ||
} | ||
|
||
func sleep(until deadline: Instant, | ||
tolerance: Duration?) async throws { | ||
// Refactored into a non-async method so that `NSLock.lock` and `NSLock.unlock` can be used. | ||
// Cannot use the async-safe `NSLock.withLocking` method until the following change is released: | ||
// https://github.com/apple/swift-corelibs-foundation/pull/4736 | ||
sleep(until: deadline) | ||
} | ||
|
||
private func sleep(until deadline: Instant) { | ||
lock.lock() | ||
defer { | ||
lock.unlock() | ||
} | ||
|
||
_now = max(deadline, _now) | ||
} | ||
} | ||
|
||
func doSomething() async throws { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Retry an operation using the default retry behavior. | ||
|
||
// snippet.hide | ||
|
||
import Retry | ||
|
||
// snippet.show | ||
|
||
try await retry { | ||
try await doSomething() | ||
} | ||
|
||
// snippet.hide | ||
|
||
func doSomething() async throws { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Configure the retry behavior. | ||
|
||
// snippet.hide | ||
|
||
import Logging | ||
import Retry | ||
|
||
// snippet.show | ||
|
||
try await retry(maxAttempts: 5, | ||
backoff: .default(baseDelay: .milliseconds(500), | ||
maxDelay: .seconds(10)), | ||
logger: myLogger) { | ||
try await doSomething() | ||
} | ||
|
||
// snippet.hide | ||
|
||
let myLogger = Logger(label: "Example Code") | ||
|
||
func doSomething() async throws { | ||
} |
29 changes: 29 additions & 0 deletions
29
Snippets/Common Use Cases/EnableOrDisableRetriesForSpecificCodePaths.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Enable or disable retries based on which suboperation failed. | ||
|
||
// snippet.hide | ||
|
||
import Retry | ||
|
||
// snippet.show | ||
|
||
try await retry { | ||
do { | ||
try await doSomethingRetryable() | ||
} catch { | ||
throw Retryable(error) | ||
} | ||
|
||
do { | ||
try await doSomethingNotRetryable() | ||
} catch { | ||
throw NotRetryable(error) | ||
} | ||
} | ||
|
||
// snippet.hide | ||
|
||
func doSomethingRetryable() async throws { | ||
} | ||
|
||
func doSomethingNotRetryable() async throws { | ||
} |
47 changes: 47 additions & 0 deletions
47
Snippets/Common Use Cases/EnableOrDisableRetriesForSpecificErrorCases.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Specify which error cases are retryable. | ||
|
||
// snippet.hide | ||
|
||
import Retry | ||
|
||
// snippet.show | ||
|
||
try await retry { | ||
try await doSomething() | ||
} shouldRetry: { error in | ||
return error.isRetryable | ||
} | ||
|
||
extension Error { | ||
var isRetryable: Bool { | ||
switch self { | ||
case let error as MyError: | ||
return error.isRetryable | ||
|
||
default: | ||
return true | ||
} | ||
} | ||
} | ||
|
||
extension MyError { | ||
var isRetryable: Bool { | ||
switch self { | ||
case .myRetryableCase: | ||
return true | ||
|
||
case .myNotRetryableCase: | ||
return false | ||
} | ||
} | ||
} | ||
|
||
// snippet.hide | ||
|
||
func doSomething() async throws { | ||
} | ||
|
||
enum MyError: Error { | ||
case myRetryableCase | ||
case myNotRetryableCase | ||
} |
Oops, something went wrong.