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

feat: Make RetryStrategyOptions initializer public #571

Merged
merged 2 commits into from
Jul 31, 2023
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
10 changes: 7 additions & 3 deletions Sources/ClientRuntime/Retries/ExponentialBackoffStrategy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@
import func Foundation.pow
import struct Foundation.TimeInterval

struct ExponentialBackoffStrategy: RetryBackoffStrategy {
public struct ExponentialBackoffStrategy: RetryBackoffStrategy {
let options: ExponentialBackoffStrategyOptions

var random: () -> Double = { Double.random(in: 0.0...1.0) }

// values set by Retry Behavior 2.0 SEP
let r = 2.0

init(options: ExponentialBackoffStrategyOptions = ExponentialBackoffStrategyOptions()) {
public init() {
self.init(options: ExponentialBackoffStrategyOptions())
}
Comment on lines +19 to +21
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not keep the optional options as paramter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExponentialBackoffStrategyOptions is not (yet) public. Not much point in making it public right now since there's no other backoff strategy available and we'd prefer that customers use our settings.

We may revisit this in the future.


init(options: ExponentialBackoffStrategyOptions) {
self.options = options
}

func computeNextBackoffDelay(attempt: Int) -> TimeInterval {
public func computeNextBackoffDelay(attempt: Int) -> TimeInterval {
min(random() * pow(r, Double(attempt)), options.maxBackoff)
}
}
2 changes: 1 addition & 1 deletion Sources/ClientRuntime/Retries/RetryStrategyOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public struct RetryStrategyOptions {
/// - maxRetriesBase: The number of times to retry the initial request. Defaults to 2.
/// - availableCapacity: The number of available tokens in a retry quota. Defaults to 500.
/// - maxCapacity: The max number of tokens in a retry quota. Defaults to 500.
init(
public init(
backoffStrategy: RetryBackoffStrategy = ExponentialBackoffStrategy(),
maxRetriesBase: Int = 2,
availableCapacity: Int = 500,
Expand Down