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

[#79] Draft: Add custom retry strategy implementation #86

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 Documentation/3.Advanced_HTTPClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ The options are:
- `exponential` and `fibonacci` are the same as `delayed`, but with sequentially increasing delay times
- `after(HTTPRequest, TimeInterval, AltRequestCatcher?)` will retry the original call after calling an alternate request. For example, if you are making an authenticated request and the session has expired; you can then call a login alternate request to perform a new login and retry the original call.
- `afterTask(TimeInterval, RetryTask, RetryTaskErrorCatcher?)` performs an async task before retrying the original request. By using an async `Task`, you may perform work outside of the scope of RealHTTP and inject whatever you need into the original request. Note that, like with the existing retry with `HTTPRequest` strategy, any error triggered by the async `Task` is not propagated to the original request. However, a callback could be provided to at least "see" it.
- `custom` will retry the original call after the amount of seconds returned by the closure you are required to provide for this case. The closure provides you with the failed `HTTPRequest` object so you have the chance to define any custom logic based on it and return the desired amount of seconds.

We'll take a closer look at these strategies below.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,21 @@ public enum HTTPResponseValidatorResult {
/// - the request you want to execute before retry the original request.
/// - the amount of time before retry the original request once you got the response for the alt request.
/// - an optional async callback to execute once you got the response of the alt request before retry the original request.
/// - `custom` will retry the original call after the amount of seconds returned by the closure you are required to provide for this case.
/// The closure provides you with the failed `HTTPRequest` object so you have the chance to define any custom logic based on it and return the desired amount of seconds.
public enum HTTPRetryStrategy {
public typealias AltRequestCatcher = ((_ request: HTTPRequest, _ response: HTTPResponse) async throws -> Void)
public typealias RetryTask = ((_ originalRequest: HTTPRequest) async throws -> Void)
public typealias RetryTaskErrorCatcher = ((_ error: Error) async -> Void)
public typealias CustomRetryIntervalProvider = (_ request: HTTPRequest) -> TimeInterval

case immediate
case delayed(_ interval: TimeInterval)
case exponential(_ base: Int)
case fibonacci
case after(HTTPRequest, TimeInterval, AltRequestCatcher?)
case afterTask(TimeInterval, RetryTask, RetryTaskErrorCatcher?)
case custom(_ retryIntervalProvider: CustomRetryIntervalProvider)

// MARK: - Internal Functions

Expand Down Expand Up @@ -108,6 +112,9 @@ public enum HTTPRetryStrategy {

case .afterTask:
return 0

case .custom(let retryIntervalProvider):
return retryIntervalProvider(request)
}
}

Expand Down