Skip to content

Commit 36285a0

Browse files
authored
fix: new config redesign (#294)
1 parent 05ab352 commit 36285a0

File tree

46 files changed

+528
-232
lines changed

Some content is hidden

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

46 files changed

+528
-232
lines changed

Packages/ClientRuntime/Sources/Config/Configuration.swift

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
public struct DefaultSDKRuntimeConfiguration: SDKRuntimeConfiguration {
9+
public var retrier: Retrier
10+
11+
public var logger: LogAgent
12+
13+
public init(_ clientName: String) throws {
14+
self.retrier = try SDKRetrier()
15+
self.logger = SwiftLogger(label: clientName)
16+
}
17+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
/// For each service, a concrete service client configuration class is generated to implement the SDKRuntimeConfiguration protocol.
9+
/// This generated concrete class provides a mechanism to adopt defaults or override by injection any of the parameters.
10+
/// If this concrete class is not sufficient for your use case, you have the ability to write a concrete class that conforms to SDKRuntimeConfiguration.
11+
public protocol SDKRuntimeConfiguration {
12+
var encoder: RequestEncoder? {get}
13+
var decoder: ResponseDecoder? {get}
14+
var httpClientEngine: HttpClientEngine {get}
15+
var httpClientConfiguration: HttpClientConfiguration {get}
16+
var idempotencyTokenGenerator: IdempotencyTokenGenerator {get}
17+
var retrier: Retrier {get}
18+
var logger: LogAgent {get}
19+
var clientLogMode: ClientLogMode {get}
20+
}
21+
22+
public extension SDKRuntimeConfiguration {
23+
var httpClientEngine: HttpClientEngine {
24+
return CRTClientEngine()
25+
}
26+
27+
var httpClientConfiguration: HttpClientConfiguration {
28+
return HttpClientConfiguration()
29+
}
30+
31+
var idempotencyTokenGenerator: IdempotencyTokenGenerator {
32+
return DefaultIdempotencyTokenGenerator()
33+
}
34+
35+
var clientLogMode: ClientLogMode {
36+
return .request
37+
}
38+
39+
var encoder: RequestEncoder? {
40+
return nil
41+
}
42+
43+
var decoder: ResponseDecoder? {
44+
return nil
45+
}
46+
}

Packages/ClientRuntime/Sources/Networking/Http/CRT/CRTClientEngine.swift

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,22 @@ public class CRTClientEngine: HttpClientEngine {
1818
private let CONTENT_LENGTH_HEADER = "Content-Length"
1919
private let AWS_COMMON_RUNTIME = "AwsCommonRuntime"
2020
private let DEFAULT_STREAM_WINDOW_SIZE = 16 * 1024 * 1024 // 16 MB
21-
22-
public let bootstrap: ClientBootstrap
23-
public let eventLoopGroup: EventLoopGroup
24-
private let socketOptions: SocketOptions
25-
private let tlsContextOptions: TlsContextOptions
26-
private let tlsContext: TlsContext
21+
2722
private let windowSize: Int
2823
private let maxConnectionsPerEndpoint: Int
2924

30-
init(eventLoopGroup: EventLoopGroup, config: CRTClientEngineConfig) throws {
25+
init(config: CRTClientEngineConfig = CRTClientEngineConfig()) {
3126
AwsCommonRuntimeKit.initialize()
3227
self.maxConnectionsPerEndpoint = config.maxConnectionsPerEndpoint
33-
self.eventLoopGroup = eventLoopGroup
34-
let hostResolver = DefaultHostResolver(eventLoopGroup: eventLoopGroup, maxHosts: 8, maxTTL: 30)
35-
self.bootstrap = try ClientBootstrap(eventLoopGroup: eventLoopGroup, hostResolver: hostResolver)
36-
self.socketOptions = SocketOptions(socketType: .stream)
37-
let tlsContextOptions = TlsContextOptions()
38-
tlsContextOptions.setVerifyPeer(config.verifyPeer)
39-
self.tlsContextOptions = tlsContextOptions
40-
self.tlsContext = try TlsContext(options: tlsContextOptions, mode: .client)
4128
self.windowSize = config.windowSize
4229
self.logger = SwiftLogger(label: "CRTClientEngine")
4330
self.crtLogger = Logger(pipe: stdout, level: .none, allocator: defaultAllocator)
4431
}
4532

46-
public required convenience init(eventLoopGroup: EventLoopGroup) throws {
47-
try self.init(eventLoopGroup: eventLoopGroup, config: CRTClientEngineConfig())
48-
}
49-
50-
public convenience init() throws {
51-
try self.init(eventLoopGroup: EventLoopGroup(threadCount: 1))
52-
}
53-
5433
private func createConnectionPool(endpoint: Endpoint) -> HttpClientConnectionManager {
55-
let tlsConnectionOptions = tlsContext.newConnectionOptions()
56-
let options = HttpClientConnectionOptions(clientBootstrap: bootstrap,
34+
let tlsConnectionOptions = SDKDefaultIO.shared.tlsContext.newConnectionOptions()
35+
let socketOptions = SocketOptions(socketType: .stream)
36+
let options = HttpClientConnectionOptions(clientBootstrap: SDKDefaultIO.shared.clientBootstrap,
5737
hostName: endpoint.host,
5838
initialWindowSize: windowSize,
5939
port: UInt16(endpoint.port),
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import AwsCommonRuntimeKit
9+
import class Foundation.ProcessInfo
10+
11+
public final class SDKDefaultIO {
12+
public static let shared = SDKDefaultIO()
13+
14+
public let eventLoopGroup: EventLoopGroup
15+
public let hostResolver: DefaultHostResolver
16+
public let clientBootstrap: ClientBootstrap
17+
public let tlsContext: TlsContext
18+
19+
private init() {
20+
self.eventLoopGroup = EventLoopGroup(threadCount: 0)
21+
self.hostResolver = DefaultHostResolver(eventLoopGroup: eventLoopGroup,
22+
maxHosts: 8,
23+
maxTTL: 30)
24+
25+
do {
26+
self.clientBootstrap = try ClientBootstrap(eventLoopGroup: eventLoopGroup,
27+
hostResolver: hostResolver)
28+
} catch {
29+
fatalError("""
30+
Client Bootstrap failed to create. This could be due to lack
31+
of memory but should never happen. Please open a Github issue with
32+
us at https://github.com/awslabs/aws-sdk-swift.
33+
""")
34+
}
35+
36+
let tlsContextOptions = TlsContextOptions()
37+
tlsContextOptions.setVerifyPeer(true)
38+
39+
do {
40+
self.tlsContext = try TlsContext(options: tlsContextOptions,
41+
mode: .client)
42+
} catch {
43+
fatalError("""
44+
Tls Context failed to create. This should never happen.Please open a
45+
Github issue with us at https://github.com/awslabs/aws-sdk-swift.
46+
""")
47+
}
48+
}
49+
}

Packages/ClientRuntime/Sources/Networking/Http/HttpClientEngine.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import AwsCommonRuntimeKit
66

77
public protocol HttpClientEngine {
8-
init(eventLoopGroup: EventLoopGroup) throws
9-
var eventLoopGroup: EventLoopGroup {get}
108
func executeWithClosure(request: SdkHttpRequest, completion: @escaping NetworkResult)
119
func execute(request: SdkHttpRequest) -> SdkFuture<HttpResponse>
1210
func close()

Packages/ClientRuntime/Sources/Networking/Streaming/ByteStream.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,3 @@ extension ByteStream: Codable {
6767
try container.encode(self.toBytes().toData())
6868
}
6969
}
70-

Packages/ClientRuntime/Sources/Networking/Streaming/DataStreamReader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public class DataStreamReader: StreamReader {
7070
}
7171

7272
public var contentLength: UInt? {
73-
withLockingClosure() {
73+
withLockingClosure {
7474
return byteBuffer.length
7575
}
7676
}

Packages/ClientRuntime/Sources/Retries/ExponentialBackOffRetryOptions.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@
77
import AwsCommonRuntimeKit
88

99
public struct ExponentialBackOffRetryOptions {
10-
let client: HttpClientEngine
1110
let maxRetries: Int
1211
let backOffScaleFactor: UInt32
1312
let jitterMode: ExponentialBackOffJitterMode
1413
let generateRandom: (() -> UInt64)?
1514

16-
public init(client: HttpClientEngine,
17-
maxRetries: Int = 10,
15+
public init(maxRetries: Int = 10,
1816
backOffScaleFactor: UInt32 = 25,
1917
jitterMode: ExponentialBackOffJitterMode = .default,
2018
generateRandom: (() -> UInt64)? = nil) {
21-
self.client = client
2219
self.maxRetries = maxRetries
2320
self.backOffScaleFactor = backOffScaleFactor
2421
self.jitterMode = jitterMode
@@ -28,7 +25,7 @@ public struct ExponentialBackOffRetryOptions {
2825

2926
extension ExponentialBackOffRetryOptions {
3027
func toCRTType() -> CRTExponentialBackoffRetryOptions {
31-
return CRTExponentialBackoffRetryOptions(eventLoopGroup: client.eventLoopGroup,
28+
return CRTExponentialBackoffRetryOptions(eventLoopGroup: SDKDefaultIO.shared.eventLoopGroup,
3229
maxRetries: maxRetries,
3330
backOffScaleFactor: backOffScaleFactor,
3431
jitterMode: jitterMode.toCRTType(),

Packages/ClientRuntime/Sources/Retries/SDKRetrier.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class SDKRetrier: Retrier {
1313
self.crtRetryStrategy = try CRTAWSRetryStrategy(options: options.toCRTType())
1414
}
1515

16-
public convenience init(clientEngine: HttpClientEngine) throws {
17-
let backOffOptions = ExponentialBackOffRetryOptions(client: clientEngine)
16+
public convenience init() throws {
17+
let backOffOptions = ExponentialBackOffRetryOptions()
1818
let retryOptions = RetryOptions(backOffRetryOptions: backOffOptions)
1919
try self.init(options: retryOptions)
2020
}

0 commit comments

Comments
 (0)