Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
fumoboy007 committed Dec 13, 2023
0 parents commit e39eb20
Show file tree
Hide file tree
Showing 17 changed files with 956 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/documentation.yml
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
35 changes: 35 additions & 0 deletions .github/workflows/tests.yml
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"

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"
9 changes: 9 additions & 0 deletions .gitignore
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
9 changes: 9 additions & 0 deletions LICENSE.md
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.
32 changes: 32 additions & 0 deletions Package.resolved
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
}
41 changes: 41 additions & 0 deletions Package.swift
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: "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",
]
),
]
)
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# swift-retry

Retries in Swift with good default behavior 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)

## Usage

See the [documentation](https://fumoboy007.github.io/swift-retry/documentation/retry/).
33 changes: 33 additions & 0 deletions Sources/Retry/Backoff/Algorithms/ConstantBackoff.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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.

struct ConstantBackoff<ClockType: Clock>: BackoffAlgorithm {
private let delay: ClockType.Duration

init(delay: ClockType.Duration) {
self.delay = delay
}

func nextDelay() -> ClockType.Duration {
return delay
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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.

struct FullJitterExponentialBackoff<ClockType: Clock>: BackoffAlgorithm {
private let clockMinResolution: ClockType.Duration

private let baseDelayInClockTicks: Int
private let maxExponent: Int

private var attempt = 0

init(clock: ClockType,
baseDelay: ClockType.Duration,
maxDelay: ClockType.Duration?) {
self.clockMinResolution = clock.minimumResolution

self.baseDelayInClockTicks = Int((baseDelay / clockMinResolution).rounded())
precondition(baseDelayInClockTicks > 0)

var maxExponent = Self.closestBaseTwoExponentOfValue(lessThanOrEqualTo: Int.max / baseDelayInClockTicks)
if let maxDelay {
precondition(maxDelay >= baseDelay)
let maxDelayInClockTicks = Int((maxDelay / clockMinResolution).rounded())

maxExponent = min(Self.closestBaseTwoExponentOfValue(lessThanOrEqualTo: maxDelayInClockTicks),
maxExponent)
}
self.maxExponent = maxExponent
}

private static func closestBaseTwoExponentOfValue(lessThanOrEqualTo value: Int) -> Int {
precondition(value >= 0)

return max(Int.bitWidth - value.leadingZeroBitCount - 1, 0)
}

mutating func nextDelay() -> ClockType.Duration {
defer {
attempt += 1
}

let exponent = min(attempt, maxExponent)
let maxDelayInClockTicks = baseDelayInClockTicks * 1 << exponent

let delayInClockTicks = Int.random(in: 0...maxDelayInClockTicks)
return clockMinResolution * delayInClockTicks
}
}
Loading

0 comments on commit e39eb20

Please sign in to comment.