Skip to content

Commit

Permalink
Release 1.1.0 (#8)
Browse files Browse the repository at this point in the history
Release `1.1.0`
  • Loading branch information
ns-vasilev authored Dec 8, 2023
2 parents c0e2970 + 1dd21a5 commit ed25d1d
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 14 deletions.
1 change: 0 additions & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ opt_in_rules: # some rules are only opt-in
- empty_count
- empty_string
- empty_xctest_method
- enum_case_associated_values_count
- explicit_init
- fallthrough
- fatal_error_message
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
All notable changes to this project will be documented in this file.

#### 1.x Releases
- `1.1.x` Releases - [1.0.0](#110)
- `1.0.x` Releases - [1.0.0](#100)

## [1.1.0](https://github.com/space-code/typhoon/releases/tag/1.1.0)
Released on 2023-12-08.

#### Added
- Increase the test coverage of the project
- Added in Pull Request [#6](https://github.com/space-code/typhoon/pull/6).
- Implement error handling in RetryPolicyService
- Added in Pull Request [#5](https://github.com/space-code/typhoon/pull/5).
- Implement exponential backoff with jitter
- Added in Pull Request [#4](https://github.com/space-code/typhoon/pull/4).

## [1.0.0](https://github.com/space-code/typhoon/releases/tag/1.0.0)
Released on 2023-11-10.

Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@

## Usage

`Typhoon` provides two retry policy strategies:
`Typhoon` provides three retry policy strategies:

```swift
/// A retry strategy with a constant number of attempts and fixed duration between retries.
case constant(retry: Int, duration: DispatchTimeInterval)

/// A retry strategy with an exponential increase in duration between retries.
case exponential(retry: Int, multiplier: Double, duration: DispatchTimeInterval)
case exponential(retry: Int, multiplier: Double = 2.0, duration: DispatchTimeInterval)

/// A retry strategy with exponential increase in duration between retries and added jitter.
case exponentialWithJitter(retry: Int, jitterFactor: Double = 0.1, maxInterval: UInt64? = 60, multiplier: Double = 2.0, duration: DispatchTimeInterval)
```

Create a `RetryPolicyService` instance and pass a desired strategy like this:
Expand Down Expand Up @@ -87,4 +90,4 @@ Please feel free to help out with this project! If you see something that could
Nikita Vasilev, [email protected]

## License
typhoon is available under the MIT license. See the LICENSE file for more info.
typhoon is available under the MIT license. See the LICENSE file for more info.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ public protocol IRetryPolicyService {
///
/// - Parameters:
/// - strategy: The strategy defining the behavior of the retry policy.
/// - onFailure: An optional closure called on each failure to handle or log errors.
/// - closure: The closure that will be retried based on the specified strategy.
///
/// - Returns: The result of the closure's execution after retrying based on the policy.
func retry<T>(strategy: RetryPolicyStrategy?, _ closure: () async throws -> T) async throws -> T
func retry<T>(
strategy: RetryPolicyStrategy?,
onFailure: ((Error) async -> Void)?,
_ closure: () async throws -> T
) async throws -> T
}

public extension IRetryPolicyService {
Expand All @@ -27,6 +32,17 @@ public extension IRetryPolicyService {
///
/// - Returns: The result of the closure's execution after retrying based on the policy.
func retry<T>(_ closure: () async throws -> T) async throws -> T {
try await retry(strategy: nil, closure)
try await retry(strategy: nil, onFailure: nil, closure)
}

/// Retries a closure with a given strategy.
///
/// - Parameters:
/// - strategy: The strategy defining the behavior of the retry policy.
/// - closure: The closure that will be retried based on the specified strategy.
///
/// - Returns: The result of the closure's execution after retrying based on the policy.
func retry<T>(strategy: RetryPolicyStrategy?, _ closure: () async throws -> T) async throws -> T {
try await retry(strategy: strategy, onFailure: nil, closure)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ public final class RetryPolicyService {
// MARK: IRetryPolicyService

extension RetryPolicyService: IRetryPolicyService {
public func retry<T>(strategy: RetryPolicyStrategy?, _ closure: () async throws -> T) async throws -> T {
public func retry<T>(
strategy: RetryPolicyStrategy?,
onFailure: ((Error) async -> Void)?,
_ closure: () async throws -> T
) async throws -> T {
for duration in RetrySequence(strategy: strategy ?? self.strategy) {
try Task.checkCancellation()

do {
return try await closure()
} catch {}
} catch {
await onFailure?(error)
}

try await Task.sleep(nanoseconds: duration)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ struct RetryIterator: IteratorProtocol {
let value = duration * pow(multiplier, Double(retries))
return UInt64(value * .nanosec)
}
case let .exponentialWithJitter(_, jitterFactor, maxInterval, multiplier, duration):
if let duration = duration.double {
let exponentialBackoff = duration * pow(multiplier, Double(retries))
let jitter = Double.random(in: -jitterFactor * exponentialBackoff ... jitterFactor * exponentialBackoff)
let value = max(0, exponentialBackoff + jitter)
return min(maxInterval ?? UInt64.max, UInt64(value * .nanosec))
}
}

return 0
Expand Down
31 changes: 30 additions & 1 deletion Sources/Typhoon/Classes/Strategy/RetryPolicyStrategy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,35 @@ import Foundation
/// A strategy used to define different retry policies.
public enum RetryPolicyStrategy {
/// A retry strategy with a constant number of attempts and fixed duration between retries.
///
/// - Parameters:
/// - retry: The number of retry attempts.
/// - duration: The initial duration between retries.
case constant(retry: Int, duration: DispatchTimeInterval)

/// A retry strategy with an exponential increase in duration between retries.
case exponential(retry: Int, multiplier: Double, duration: DispatchTimeInterval)
///
/// - Parameters:
/// - retry: The number of retry attempts.
/// - multiplier: The multiplier for calculating the exponential backoff duration (default is 2).
/// - duration: The initial duration between retries.
case exponential(retry: Int, multiplier: Double = 2, duration: DispatchTimeInterval)

/// A retry strategy with exponential increase in duration between retries and added jitter.
///
/// - Parameters:
/// - retry: The number of retry attempts.
/// - jitterFactor: The factor to control the amount of jitter (default is 0.1).
/// - maxInterval: The maximum allowed interval between retries (default is 60 seconds).
/// - multiplier: The multiplier for calculating the exponential backoff duration (default is 2).
/// - duration: The initial duration between retries.
case exponentialWithJitter(
retry: Int,
jitterFactor: Double = 0.1,
maxInterval: UInt64? = 60,
multiplier: Double = 2,
duration: DispatchTimeInterval
)

/// The number of retry attempts based on the strategy.
public var retries: Int {
Expand All @@ -20,6 +45,8 @@ public enum RetryPolicyStrategy {
return retry
case let .exponential(retry, _, _):
return retry
case let .exponentialWithJitter(retry, _, _, _, _):
return retry
}
}

Expand All @@ -30,6 +57,8 @@ public enum RetryPolicyStrategy {
return duration
case let .exponential(_, _, duration):
return duration
case let .exponentialWithJitter(_, _, _, _, duration):
return duration
}
}
}
16 changes: 16 additions & 0 deletions Tests/TyphoonTests/UnitTests/RetryPolicyServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ final class RetryPolicyServiceTests: XCTestCase {
// then
XCTAssertEqual(counter, .retry)
}

func test_thatRetryServiceHandlesErrorOnFailureCallback_whenErrorOcurred() async {
// when
var failureError: NSError?
do {
_ = try await sut.retry(
strategy: .constant(retry: .retry, duration: .nanoseconds(1)),
onFailure: { error in failureError = error as NSError }
) {
throw URLError(.unknown)
}
} catch {}

// then
XCTAssertEqual(failureError as? URLError, URLError(.unknown))
}
}

// MARK: - Constants
Expand Down
47 changes: 47 additions & 0 deletions Tests/TyphoonTests/UnitTests/RetryPolicyStrategyTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// Typhoon
// Copyright © 2023 Space Code. All rights reserved.
//

import Typhoon
import XCTest

// MARK: - RetryPolicyStrategyTests

final class RetryPolicyStrategyTests: XCTestCase {
// MARK: Tests

func test_thatRetryPolicyStrategyReturnsDuration_whenTypeIsConstant() {
// when
let duration = RetryPolicyStrategy.constant(retry: .retry, duration: .second).duration

// then
XCTAssertEqual(duration, .second)
}

func test_thatRetryPolicyStrategyReturnsDuration_whenTypeIsExponential() {
// when
let duration = RetryPolicyStrategy.exponential(retry: .retry, duration: .second).duration

// then
XCTAssertEqual(duration, .second)
}

func test_thatRetryPolicyStrategyReturnsDuration_whenTypeIsExponentialWithJitter() {
// when
let duration = RetryPolicyStrategy.exponentialWithJitter(retry: .retry, duration: .second).duration

// then
XCTAssertEqual(duration, .second)
}
}

// MARK: Constants

private extension Int {
static let retry = 5
}

private extension DispatchTimeInterval {
static let second = DispatchTimeInterval.seconds(1)
}
75 changes: 70 additions & 5 deletions Tests/TyphoonTests/UnitTests/RetrySequenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,94 @@ final class RetrySequenceTests: XCTestCase {

func test_thatRetrySequenceCreatesASequence_whenStrategyIsConstant() {
// given
let sequence = RetrySequence(strategy: .constant(retry: .retry, duration: .nanoseconds(1)))
let sequence = RetrySequence(strategy: .constant(retry: .retry, duration: .nanosecond))

// when
let result: [UInt64] = sequence.map { $0 }

// then
XCTAssertEqual(result, [1, 1, 1, 1, 1, 1])
XCTAssertEqual(result, [1, 1, 1, 1, 1, 1, 1, 1])
}

func test_thatRetrySequenceCreatesASequence_whenStrategyIsExponential() {
// given
let sequence = RetrySequence(strategy: .exponential(retry: .retry, multiplier: 2, duration: .nanoseconds(1)))
let sequence = RetrySequence(strategy: .exponential(retry: .retry, duration: .nanosecond))

// when
let result: [UInt64] = sequence.map { $0 }

// then
XCTAssertEqual(result, [1, 2, 4, 8, 16, 32])
XCTAssertEqual(result, [1, 2, 4, 8, 16, 32, 64, 128])
}

func test_thatRetrySequenceCreatesASequence_whenStrategyIsExponentialWithJitter() {
// given
let sequence = RetrySequence(
strategy: .exponentialWithJitter(
retry: .retry,
jitterFactor: .jitterFactor,
maxInterval: .maxInterval,
duration: .nanosecond
)
)

// when
let result: [UInt64] = sequence.map { $0 }

// then
XCTAssertEqual(result.count, 8)
XCTAssertEqual(result[0], 1, accuracy: 1)
XCTAssertEqual(result[1], 2, accuracy: 1)
XCTAssertEqual(result[2], 4, accuracy: 1)
XCTAssertEqual(result[3], 8, accuracy: 1)
XCTAssertEqual(result[4], 16, accuracy: 2)
XCTAssertEqual(result[5], 32, accuracy: 4)
XCTAssertEqual(result[6], 64, accuracy: 7)
XCTAssertEqual(result[7], .maxInterval)
}

func test_thatRetrySequenceDoesNotLimitASequence_whenStrategyIsExponentialWithJitterAndMaxIntervalIsNil() {
// given
let sequence = RetrySequence(
strategy: .exponentialWithJitter(
retry: .retry,
jitterFactor: .jitterFactor,
maxInterval: nil,
duration: .nanosecond
)
)

// when
let result: [UInt64] = sequence.map { $0 }

// then
XCTAssertEqual(result.count, 8)
XCTAssertEqual(result[0], 1, accuracy: 1)
XCTAssertEqual(result[1], 2, accuracy: 1)
XCTAssertEqual(result[2], 4, accuracy: 1)
XCTAssertEqual(result[3], 8, accuracy: 1)
XCTAssertEqual(result[4], 16, accuracy: 2)
XCTAssertEqual(result[5], 32, accuracy: 4)
XCTAssertEqual(result[6], 64, accuracy: 8)
XCTAssertEqual(result[7], 128, accuracy: 13)
}
}

// MARK: - Constant

private extension Int {
static let retry: Int = 6
static let retry: Int = 8
}

private extension UInt64 {
static let maxInterval: UInt64 = 60
}

private extension Double {
static let multiplier = 2.0
static let jitterFactor = 0.1
}

private extension DispatchTimeInterval {
static let nanosecond = DispatchTimeInterval.nanoseconds(1)
}

0 comments on commit ed25d1d

Please sign in to comment.