-
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.
Add a
RetryableRequest
protocol that adds safe retry methods to a c…
…onforming request type.
- Loading branch information
1 parent
71ebf57
commit bd3f43e
Showing
6 changed files
with
445 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,64 @@ | ||
// Conform a request type to ``RetryableRequest`` to add safe retry methods to the request type. | ||
|
||
// snippet.hide | ||
|
||
import Retry | ||
|
||
// snippet.show | ||
|
||
extension MyRequest: RetryableRequest { | ||
var isIdempotent: Bool { | ||
// ... | ||
// snippet.hide | ||
return true | ||
// snippet.show | ||
} | ||
|
||
func unsafeRetryIgnoringIdempotency<ClockType, ReturnType>( | ||
with configuration: RetryConfiguration<ClockType>, | ||
@_inheritActorContext @_implicitSelfCapture operation: (Self) async throws -> ReturnType | ||
) async throws -> ReturnType { | ||
// We can override the `shouldRetry` closure to automatically handle errors specific to | ||
// the communication protocol. | ||
let configuration = configuration.withShouldRetry { error in | ||
switch error { | ||
case is MyTransientCommunicationError: | ||
return true | ||
|
||
case is MyNonTransientCommunicationError: | ||
return false | ||
|
||
default: | ||
return configuration.shouldRetry(error) | ||
} | ||
} | ||
|
||
return try await Retry.retry(with: configuration) { | ||
return try await operation(self) | ||
} | ||
} | ||
} | ||
|
||
// snippet.hide | ||
|
||
let myRequest = MyRequest() | ||
|
||
// snippet.show | ||
|
||
try await myRequest.retry { request in | ||
try await perform(request) | ||
} | ||
|
||
// snippet.hide | ||
|
||
struct MyRequest { | ||
} | ||
|
||
enum MyTransientCommunicationError: Error { | ||
} | ||
|
||
enum MyNonTransientCommunicationError: Error { | ||
} | ||
|
||
func perform(_ request: MyRequest) 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
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
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
Oops, something went wrong.