Skip to content

Commit fdc7a1b

Browse files
authored
feat!: Create retry modules (#710)
1 parent 6c5c0f0 commit fdc7a1b

File tree

48 files changed

+255
-83
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+255
-83
lines changed

.swiftlint.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@ excluded:
22
- .build
33
- Sources/SmithyTestUtil/*
44
- Sources/WeatherSDK/*
5-
- Tests/ClientRuntimeTests/*
6-
- Tests/SmithyTestUtilTests/*
7-
- Tests/SmithyXMLTests/*
8-
- Tests/SmithyJSONTests/*
9-
- Tests/SmithyFormURLTests/*
10-
- Tests/SmithyTimestampsTests/*
11-
- Tests/WeatherSDKTests/*
5+
- Tests/*
126
- smithy-swift-codegen-test/build/*
137

148
analyzer_rules:

Package.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ let package = Package(
2929
],
3030
products: [
3131
.library(name: "ClientRuntime", targets: ["ClientRuntime"]),
32+
.library(name: "SmithyRetriesAPI", targets: ["SmithyRetriesAPI"]),
33+
.library(name: "SmithyRetries", targets: ["SmithyRetries"]),
3234
.library(name: "SmithyReadWrite", targets: ["SmithyReadWrite"]),
3335
.library(name: "SmithyXML", targets: ["SmithyXML"]),
3436
.library(name: "SmithyJSON", targets: ["SmithyJSON"]),
@@ -43,6 +45,8 @@ let package = Package(
4345
.target(
4446
name: "ClientRuntime",
4547
dependencies: [
48+
"SmithyRetriesAPI",
49+
"SmithyRetries",
4650
"SmithyXML",
4751
"SmithyJSON",
4852
"SmithyFormURL",
@@ -53,6 +57,19 @@ let package = Package(
5357
.copy("PrivacyInfo.xcprivacy")
5458
]
5559
),
60+
.target(
61+
name: "SmithyRetriesAPI"
62+
),
63+
.target(
64+
name: "SmithyRetries",
65+
dependencies: [
66+
.product(name: "AwsCommonRuntimeKit", package: "aws-crt-swift"),
67+
]
68+
),
69+
.testTarget(
70+
name: "SmithyRetriesTests",
71+
dependencies: ["ClientRuntime", "SmithyRetriesAPI", "SmithyRetries"]
72+
),
5673
.target(
5774
name: "SmithyReadWrite",
5875
dependencies: [
@@ -121,11 +138,11 @@ func addTestServiceTargets() {
121138
package.targets += [
122139
.target(
123140
name: "WeatherSDK",
124-
dependencies: ["SmithyTestUtil", "ClientRuntime"]
141+
dependencies: ["SmithyTestUtil", "ClientRuntime", "SmithyRetriesAPI", "SmithyRetries"]
125142
),
126143
.testTarget(
127144
name: "WeatherSDKTests",
128-
dependencies: ["WeatherSDK"]
145+
dependencies: ["WeatherSDK", "SmithyTestUtil"]
129146
)
130147
]
131148
}

Sources/ClientRuntime/Config/DefaultClientConfiguration.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8+
import struct SmithyRetriesAPI.RetryStrategyOptions
9+
810
public protocol DefaultClientConfiguration: ClientConfiguration {
911
/// The configuration for retry of failed network requests.
1012
///

Sources/ClientRuntime/Config/DefaultSDKRuntimeConfiguration.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8+
import protocol SmithyRetriesAPI.RetryStrategy
9+
import protocol SmithyRetriesAPI.RetryErrorInfoProvider
10+
import struct SmithyRetriesAPI.RetryStrategyOptions
11+
import struct SmithyRetries.DefaultRetryStrategy
12+
import struct SmithyRetries.ExponentialBackoffStrategy
13+
814
/// Provides configuration options for a Smithy-based service.
915
public struct DefaultSDKRuntimeConfiguration<DefaultSDKRuntimeRetryStrategy: RetryStrategy,
1016
DefaultSDKRuntimeRetryErrorInfoProvider: RetryErrorInfoProvider> {
@@ -107,7 +113,9 @@ public extension DefaultSDKRuntimeConfiguration {
107113
/// The retry strategy options to use when none is provided.
108114
///
109115
/// Defaults to options with the defaults defined in `RetryStrategyOptions`.
110-
static var defaultRetryStrategyOptions: RetryStrategyOptions { RetryStrategyOptions() }
116+
static var defaultRetryStrategyOptions: RetryStrategyOptions {
117+
RetryStrategyOptions(backoffStrategy: ExponentialBackoffStrategy())
118+
}
111119

112120
/// The log mode to use when none is provided
113121
///

Sources/ClientRuntime/Middleware/RetryMiddleware.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import struct Foundation.Locale
1010
import struct Foundation.TimeInterval
1111
import struct Foundation.TimeZone
1212
import struct Foundation.UUID
13+
import protocol SmithyRetriesAPI.RetryStrategy
14+
import protocol SmithyRetriesAPI.RetryErrorInfoProvider
15+
import struct SmithyRetriesAPI.RetryStrategyOptions
1316

1417
public struct RetryMiddleware<Strategy: RetryStrategy,
1518
ErrorInfoProvider: RetryErrorInfoProvider,
@@ -64,17 +67,15 @@ public struct RetryMiddleware<Strategy: RetryStrategy,
6467
context.getLogger()?.error("Failed to refresh retry token: \(errorInfo.errorType)")
6568
throw operationError
6669
}
67-
var estimatedSkew = context.attributes.get(key: AttributeKeys.estimatedSkew)
68-
if estimatedSkew == nil {
69-
estimatedSkew = 0
70+
var estimatedSkew = context.attributes.get(key: AttributeKeys.estimatedSkew) ?? {
7071
context.getLogger()?.info("Estimated skew not found; defaulting to zero.")
71-
}
72-
var socketTimeout = context.attributes.get(key: AttributeKeys.socketTimeout)
73-
if socketTimeout == nil {
74-
socketTimeout = 60.0
72+
return 0
73+
}()
74+
var socketTimeout = context.attributes.get(key: AttributeKeys.socketTimeout) ?? {
7575
context.getLogger()?.info("Socket timeout value not found; defaulting to 60 seconds.")
76-
}
77-
let ttlDateUTCString = getTTL(now: Date(), estimatedSkew: estimatedSkew!, socketTimeout: socketTimeout!)
76+
return 60.0
77+
}()
78+
let ttlDateUTCString = getTTL(now: Date(), estimatedSkew: estimatedSkew, socketTimeout: socketTimeout)
7879
input.headers.update(
7980
name: "amz-sdk-request",
8081
value: "ttl=\(ttlDateUTCString); attempt=\(attemptNumber + 1); max=\(maxRetries)"

Sources/ClientRuntime/Orchestrator/Orchestrator.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8+
import protocol SmithyRetriesAPI.RetryStrategy
9+
import struct SmithyRetriesAPI.RetryErrorInfo
10+
811
/// Orchestrates operation execution
912
///
1013
/// Execution performs the following steps in order:

Sources/ClientRuntime/Orchestrator/OrchestratorBuilder.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8+
import protocol SmithyRetriesAPI.RetryStrategy
9+
import struct SmithyRetriesAPI.RetryErrorInfo
10+
811
/// Builds an Orchestrator, combining runtime components, interceptors, serializers, and deserializers.
912
///
1013
/// Note: This is intended to be used within generated code, not directly.

Sources/ClientRuntime/Plugins/DefaultClientPlugin.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8+
import struct SmithyRetries.DefaultRetryStrategy
9+
810
public class DefaultClientPlugin: Plugin {
911
public init() {}
1012
public func configureClient(clientConfiguration: ClientConfiguration) {

Sources/ClientRuntime/Plugins/HttpClientPlugin.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8+
import struct SmithyRetries.DefaultRetryStrategy
9+
810
public class DefaultHttpClientPlugin: Plugin {
911

1012
var httpClientConfiguration: HttpClientConfiguration

Sources/ClientRuntime/Plugins/RetryPlugin.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8+
import struct SmithyRetriesAPI.RetryStrategyOptions
9+
810
public class RetryPlugin: Plugin {
911

1012
private var retryStrategyOptions: RetryStrategyOptions

0 commit comments

Comments
 (0)