Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 1.1.1 #43

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ xcuserdata/
DerivedData/
.swiftpm/
.netrc
Derived/
2 changes: 2 additions & 0 deletions Sources/SwiftRetrier/Core/Model/Policies/RetryPolicy.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Foundation

public protocol RetryPolicy: Sendable {
@MainActor
func shouldRetry(on attemptFailure: AttemptFailure) -> RetryDecision
@MainActor
func policyAfter(attemptFailure: AttemptFailure, delay: TimeInterval) -> any RetryPolicy
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public typealias GiveUpCriteria = @Sendable (
public typealias GiveUpCriteria = @MainActor @Sendable (
_ attemptFailure: AttemptFailure,
_ nestedPolicyDelay: TimeInterval
) -> Bool
Expand All @@ -15,6 +15,7 @@ public struct GiveUpCriteriaPolicyWrapper: RetryPolicy {
self.giveUpCriteria = giveUpCriteria
}

@MainActor
public func shouldRetry(on attemptFailure: AttemptFailure) -> RetryDecision {
return switch wrapped.shouldRetry(on: attemptFailure) {
case .giveUp:
Expand All @@ -28,6 +29,7 @@ public struct GiveUpCriteriaPolicyWrapper: RetryPolicy {
}
}

@MainActor
public func policyAfter(attemptFailure: AttemptFailure, delay: TimeInterval) -> any RetryPolicy {
GiveUpCriteriaPolicyWrapper(
wrapped: wrapped.policyAfter(attemptFailure: attemptFailure, delay: delay),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public extension RetryPolicy {
}
}

func giveUpOnErrors(matching finalErrorCriteria: @escaping @Sendable (Error) -> Bool) -> RetryPolicy {
func giveUpOnErrors(matching finalErrorCriteria: @escaping @Sendable @MainActor (Error) -> Bool) -> RetryPolicy {
GiveUpCriteriaPolicyWrapper(wrapped: self) { attempt, _ in
finalErrorCriteria(attempt.error)
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/SwiftRetrier/Core/Retriers/SimpleRetrier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ public class SimpleRetrier<Output: Sendable>: SingleOutputRetrier, @unchecked Se
throw error
case .retry(delay: let delay):
try await Task.sleep(nanoseconds: nanoseconds(delay))
policy = policy.policyAfter(attemptFailure: attemptFailure, delay: delay)
policy = await MainActor.run { [policy] in
policy.policyAfter(attemptFailure: attemptFailure, delay: delay)
}
attemptIndex += 1
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftRetrier/DSL/ColdRepeater.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import Combine
@preconcurrency import Combine

public struct ColdRepeater {
public struct ColdRepeater: Sendable {
let policy: RetryPolicy
let repeatDelay: TimeInterval
let conditionPublisher: AnyPublisher<Bool, Never>?
Expand All @@ -24,7 +24,7 @@ public extension ColdRepeater {
return ColdRepeater(policy: policy, repeatDelay: repeatDelay, conditionPublisher: conditionPublisher)
}

func giveUpOnErrors(matching finalErrorCriteria: @escaping @Sendable (Error) -> Bool) -> ColdRepeater {
func giveUpOnErrors(matching finalErrorCriteria: @escaping @Sendable @MainActor (Error) -> Bool) -> ColdRepeater {
let policy = policy.giveUpOnErrors(matching: finalErrorCriteria)
return ColdRepeater(policy: policy, repeatDelay: repeatDelay, conditionPublisher: conditionPublisher)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftRetrier/DSL/ColdRetrier.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import Combine

public struct ColdRetrier {
public struct ColdRetrier: @unchecked Sendable {
let policy: RetryPolicy
let conditionPublisher: AnyPublisher<Bool, Never>?
}
Expand All @@ -23,7 +23,7 @@ public extension ColdRetrier {
return ColdRetrier(policy: policy, conditionPublisher: conditionPublisher)
}

func giveUpOnErrors(matching finalErrorCriteria: @escaping @Sendable (Error) -> Bool) -> ColdRetrier {
func giveUpOnErrors(matching finalErrorCriteria: @escaping @Sendable @MainActor (Error) -> Bool) -> ColdRetrier {
let policy = policy.giveUpOnErrors(matching: finalErrorCriteria)
return ColdRetrier(policy: policy, conditionPublisher: conditionPublisher)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import Combine

public class AnySingleOutputRetrier<Value: Sendable>: AnyRetrier<Value>, SingleOutputRetrier, @unchecked Sendable {
public class AnySingleOutputRetrier<Value: Sendable>: AnyRetrier<Value>, SingleOutputRetrier {

private let outputBlock: @Sendable () async throws -> Output

Expand Down