immobiliare / RealHTTP

🌀swift async http client - fast, lightweight, type-safe
MIT License
282 stars 28 forks source link

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

Open NicFontana opened 1 year ago

NicFontana commented 1 year ago

Hello,

this PR should add the feature requested in #79.

You can provide a custom retry strategy by implementing a closure that takes the current HTTPRequest as input and returns an amount of seconds of your choice.

For example, the requested .exponential(base: 2, from: 1) could be easily implemented in this way:

let strategy: HTTPRetryStrategy = .custom { request in
    let numberOfPreviousAttempts = request.currentRetry
    let maximumNumberOfAttempts = request.maxRetries

    guard numberOfPreviousAttempts < maximumNumberOfAttempts else { return 0 }
    return pow(Double(2), Double(numberOfPreviousAttempts + 1))
}

Closes #79