Skip to content

Commit

Permalink
fix: new config redesign (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
kneekey23 authored Jul 29, 2021
1 parent 05ab352 commit 36285a0
Show file tree
Hide file tree
Showing 46 changed files with 528 additions and 232 deletions.
33 changes: 0 additions & 33 deletions Packages/ClientRuntime/Sources/Config/Configuration.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

public struct DefaultSDKRuntimeConfiguration: SDKRuntimeConfiguration {
public var retrier: Retrier

public var logger: LogAgent

public init(_ clientName: String) throws {
self.retrier = try SDKRetrier()
self.logger = SwiftLogger(label: clientName)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

/// For each service, a concrete service client configuration class is generated to implement the SDKRuntimeConfiguration protocol.
/// This generated concrete class provides a mechanism to adopt defaults or override by injection any of the parameters.
/// If this concrete class is not sufficient for your use case, you have the ability to write a concrete class that conforms to SDKRuntimeConfiguration.
public protocol SDKRuntimeConfiguration {
var encoder: RequestEncoder? {get}
var decoder: ResponseDecoder? {get}
var httpClientEngine: HttpClientEngine {get}
var httpClientConfiguration: HttpClientConfiguration {get}
var idempotencyTokenGenerator: IdempotencyTokenGenerator {get}
var retrier: Retrier {get}
var logger: LogAgent {get}
var clientLogMode: ClientLogMode {get}
}

public extension SDKRuntimeConfiguration {
var httpClientEngine: HttpClientEngine {
return CRTClientEngine()
}

var httpClientConfiguration: HttpClientConfiguration {
return HttpClientConfiguration()
}

var idempotencyTokenGenerator: IdempotencyTokenGenerator {
return DefaultIdempotencyTokenGenerator()
}

var clientLogMode: ClientLogMode {
return .request
}

var encoder: RequestEncoder? {
return nil
}

var decoder: ResponseDecoder? {
return nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,22 @@ public class CRTClientEngine: HttpClientEngine {
private let CONTENT_LENGTH_HEADER = "Content-Length"
private let AWS_COMMON_RUNTIME = "AwsCommonRuntime"
private let DEFAULT_STREAM_WINDOW_SIZE = 16 * 1024 * 1024 // 16 MB

public let bootstrap: ClientBootstrap
public let eventLoopGroup: EventLoopGroup
private let socketOptions: SocketOptions
private let tlsContextOptions: TlsContextOptions
private let tlsContext: TlsContext

private let windowSize: Int
private let maxConnectionsPerEndpoint: Int

init(eventLoopGroup: EventLoopGroup, config: CRTClientEngineConfig) throws {
init(config: CRTClientEngineConfig = CRTClientEngineConfig()) {
AwsCommonRuntimeKit.initialize()
self.maxConnectionsPerEndpoint = config.maxConnectionsPerEndpoint
self.eventLoopGroup = eventLoopGroup
let hostResolver = DefaultHostResolver(eventLoopGroup: eventLoopGroup, maxHosts: 8, maxTTL: 30)
self.bootstrap = try ClientBootstrap(eventLoopGroup: eventLoopGroup, hostResolver: hostResolver)
self.socketOptions = SocketOptions(socketType: .stream)
let tlsContextOptions = TlsContextOptions()
tlsContextOptions.setVerifyPeer(config.verifyPeer)
self.tlsContextOptions = tlsContextOptions
self.tlsContext = try TlsContext(options: tlsContextOptions, mode: .client)
self.windowSize = config.windowSize
self.logger = SwiftLogger(label: "CRTClientEngine")
self.crtLogger = Logger(pipe: stdout, level: .none, allocator: defaultAllocator)
}

public required convenience init(eventLoopGroup: EventLoopGroup) throws {
try self.init(eventLoopGroup: eventLoopGroup, config: CRTClientEngineConfig())
}

public convenience init() throws {
try self.init(eventLoopGroup: EventLoopGroup(threadCount: 1))
}

private func createConnectionPool(endpoint: Endpoint) -> HttpClientConnectionManager {
let tlsConnectionOptions = tlsContext.newConnectionOptions()
let options = HttpClientConnectionOptions(clientBootstrap: bootstrap,
let tlsConnectionOptions = SDKDefaultIO.shared.tlsContext.newConnectionOptions()
let socketOptions = SocketOptions(socketType: .stream)
let options = HttpClientConnectionOptions(clientBootstrap: SDKDefaultIO.shared.clientBootstrap,
hostName: endpoint.host,
initialWindowSize: windowSize,
port: UInt16(endpoint.port),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import AwsCommonRuntimeKit
import class Foundation.ProcessInfo

public final class SDKDefaultIO {
public static let shared = SDKDefaultIO()

public let eventLoopGroup: EventLoopGroup
public let hostResolver: DefaultHostResolver
public let clientBootstrap: ClientBootstrap
public let tlsContext: TlsContext

private init() {
self.eventLoopGroup = EventLoopGroup(threadCount: 0)
self.hostResolver = DefaultHostResolver(eventLoopGroup: eventLoopGroup,
maxHosts: 8,
maxTTL: 30)

do {
self.clientBootstrap = try ClientBootstrap(eventLoopGroup: eventLoopGroup,
hostResolver: hostResolver)
} catch {
fatalError("""
Client Bootstrap failed to create. This could be due to lack
of memory but should never happen. Please open a Github issue with
us at https://github.com/awslabs/aws-sdk-swift.
""")
}

let tlsContextOptions = TlsContextOptions()
tlsContextOptions.setVerifyPeer(true)

do {
self.tlsContext = try TlsContext(options: tlsContextOptions,
mode: .client)
} catch {
fatalError("""
Tls Context failed to create. This should never happen.Please open a
Github issue with us at https://github.com/awslabs/aws-sdk-swift.
""")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import AwsCommonRuntimeKit

public protocol HttpClientEngine {
init(eventLoopGroup: EventLoopGroup) throws
var eventLoopGroup: EventLoopGroup {get}
func executeWithClosure(request: SdkHttpRequest, completion: @escaping NetworkResult)
func execute(request: SdkHttpRequest) -> SdkFuture<HttpResponse>
func close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,3 @@ extension ByteStream: Codable {
try container.encode(self.toBytes().toData())
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class DataStreamReader: StreamReader {
}

public var contentLength: UInt? {
withLockingClosure() {
withLockingClosure {
return byteBuffer.length
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@
import AwsCommonRuntimeKit

public struct ExponentialBackOffRetryOptions {
let client: HttpClientEngine
let maxRetries: Int
let backOffScaleFactor: UInt32
let jitterMode: ExponentialBackOffJitterMode
let generateRandom: (() -> UInt64)?

public init(client: HttpClientEngine,
maxRetries: Int = 10,
public init(maxRetries: Int = 10,
backOffScaleFactor: UInt32 = 25,
jitterMode: ExponentialBackOffJitterMode = .default,
generateRandom: (() -> UInt64)? = nil) {
self.client = client
self.maxRetries = maxRetries
self.backOffScaleFactor = backOffScaleFactor
self.jitterMode = jitterMode
Expand All @@ -28,7 +25,7 @@ public struct ExponentialBackOffRetryOptions {

extension ExponentialBackOffRetryOptions {
func toCRTType() -> CRTExponentialBackoffRetryOptions {
return CRTExponentialBackoffRetryOptions(eventLoopGroup: client.eventLoopGroup,
return CRTExponentialBackoffRetryOptions(eventLoopGroup: SDKDefaultIO.shared.eventLoopGroup,
maxRetries: maxRetries,
backOffScaleFactor: backOffScaleFactor,
jitterMode: jitterMode.toCRTType(),
Expand Down
4 changes: 2 additions & 2 deletions Packages/ClientRuntime/Sources/Retries/SDKRetrier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class SDKRetrier: Retrier {
self.crtRetryStrategy = try CRTAWSRetryStrategy(options: options.toCRTType())
}

public convenience init(clientEngine: HttpClientEngine) throws {
let backOffOptions = ExponentialBackOffRetryOptions(client: clientEngine)
public convenience init() throws {
let backOffOptions = ExponentialBackOffRetryOptions()
let retryOptions = RetryOptions(backOffRetryOptions: backOffOptions)
try self.init(options: retryOptions)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import AwsCommonRuntimeKit
@testable import ClientRuntime

class MockHttpClientEngine: HttpClientEngine {

let eventLoopGroup: EventLoopGroup

convenience init() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
package software.amazon.smithy.swift.codegen

import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.swift.codegen.model.buildSymbol

/**
* Commonly used runtime types. Provides a single definition of a runtime symbol such that codegen isn't littered
* with inline symbol creation which makes refactoring of the runtime more difficult and error prone.
*
* NOTE: Not all symbols need be added here but it doesn't hurt to define runtime symbols once.
*/
object ClientRuntimeTypes {
object Http {
val HttpClientEngine = runtimeSymbol("HttpClientEngine")
val HttpClientConfiguration = runtimeSymbol("HttpClientConfiguration")
}

object Serde {
val RequestEncoder = runtimeSymbol("RequestEncoder")
val ResponseDecoder = runtimeSymbol("ResponseDecoder")
}

object Core {
val Logger = runtimeSymbol("LogAgent")
val ClientLogMode = runtimeSymbol("ClientLogMode")
val IdempotencyTokenGenerator = runtimeSymbol("IdempotencyTokenGenerator")
val Retrier = runtimeSymbol("Retrier")
}
}

private fun runtimeSymbol(name: String): Symbol = buildSymbol {
this.name = name
dependency(SwiftDependency.CLIENT_RUNTIME)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.traits.StreamingTrait
import software.amazon.smithy.swift.codegen.integration.ProtocolGenerator
import software.amazon.smithy.swift.codegen.model.camelCaseName
import software.amazon.smithy.swift.codegen.model.capitalizedName

/*
* Generates a Swift protocol for the service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import software.amazon.smithy.model.shapes.UnionShape
import software.amazon.smithy.model.traits.EnumTrait
import software.amazon.smithy.model.traits.StreamingTrait
import software.amazon.smithy.swift.codegen.model.hasTrait
import software.amazon.smithy.swift.codegen.model.recursiveSymbol
import software.amazon.smithy.utils.StringUtils.lowerCase

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import software.amazon.smithy.swift.codegen.integration.ProtocolGenerator
import software.amazon.smithy.swift.codegen.model.getTrait
import software.amazon.smithy.swift.codegen.model.hasTrait
import software.amazon.smithy.swift.codegen.model.isError
import software.amazon.smithy.swift.codegen.model.recursiveSymbol

fun MemberShape.isRecursiveMember(index: TopologicalIndex): Boolean {
val shapeId = toShapeId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import software.amazon.smithy.codegen.core.SymbolReference
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.Shape
import software.amazon.smithy.swift.codegen.integration.SwiftIntegration
import software.amazon.smithy.swift.codegen.model.defaultValue
import software.amazon.smithy.swift.codegen.model.isBoxed
import java.nio.file.Paths

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import software.amazon.smithy.model.shapes.Shape
import software.amazon.smithy.model.traits.DeprecatedTrait
import software.amazon.smithy.model.traits.DocumentationTrait
import software.amazon.smithy.model.traits.EnumDefinition
import software.amazon.smithy.swift.codegen.model.defaultValue
import software.amazon.smithy.swift.codegen.model.isBoxed
import software.amazon.smithy.utils.CodeWriter
import java.util.function.BiFunction

Expand Down
Loading

0 comments on commit 36285a0

Please sign in to comment.