Skip to content

Commit

Permalink
feat: make connectTimeoutMs configurable (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
dayaffe authored Sep 26, 2023
1 parent 2e4e744 commit 58eb4a3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ public extension DefaultSDKRuntimeConfiguration {
/// Is the CRT HTTP client.
static var defaultHttpClientEngine: HttpClientEngine { CRTClientEngine() }

/// The default HTTP client with a specified timeout
///
/// Is the CRT HTTP client.
static func httpClientEngineWithTimeout(timeoutMs: UInt32) -> HttpClientEngine {
return CRTClientEngine(config: CRTClientEngineConfig(connectTimeoutMs: timeoutMs))
}

/// The HTTP client configuration to use when none is provided.
///
/// Is the CRT HTTP client's configuration.
Expand Down
14 changes: 12 additions & 2 deletions Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ public class CRTClientEngine: HttpClientEngine {
private var connectionPools: [Endpoint: HTTPClientConnectionManager] = [:]
private var http2ConnectionPools: [Endpoint: HTTP2StreamManager] = [:]
private let sharedDefaultIO = SDKDefaultIO.shared
private let connectTimeoutMs: UInt32?

init(config: CRTClientEngineConfig) {
self.windowSize = config.windowSize
self.maxConnectionsPerEndpoint = config.maxConnectionsPerEndpoint
self.logger = SwiftLogger(label: "SerialExecutor")
self.connectTimeoutMs = config.connectTimeoutMs
}

func getOrCreateConnectionPool(endpoint: Endpoint) throws -> HTTPClientConnectionManager {
Expand Down Expand Up @@ -54,7 +56,11 @@ public class CRTClientEngine: HttpClientEngine {

var socketOptions = SocketOptions(socketType: .stream)
#if os(iOS) || os(watchOS)
socketOptions.connectTimeoutMs = 30_000
socketOptions.connectTimeoutMs = self.connectTimeoutMs ?? 30_000
#else
if let timeout = self.connectTimeoutMs {
socketOptions.connectTimeoutMs = timeout
}
#endif
let options = HTTPClientConnectionOptions(
clientBootstrap: sharedDefaultIO.clientBootstrap,
Expand All @@ -78,7 +84,11 @@ public class CRTClientEngine: HttpClientEngine {
private func createHTTP2ConnectionPool(endpoint: Endpoint) throws -> HTTP2StreamManager {
var socketOptions = SocketOptions(socketType: .stream)
#if os(iOS) || os(watchOS)
socketOptions.connectTimeoutMs = 30_000
socketOptions.connectTimeoutMs = self.connectTimeoutMs ?? 30_000
#else
if let timeout = self.connectTimeoutMs {
socketOptions.connectTimeoutMs = timeout
}
#endif
let tlsConnectionOptions = TLSConnectionOptions(
context: sharedDefaultIO.tlsContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ struct CRTClientEngineConfig {
/// If you set this in server mode, it enforces client authentication.
let verifyPeer: Bool

/// Timeout in MS for connections
let connectTimeoutMs: UInt32?

public init(
maxConnectionsPerEndpoint: Int = 50,
windowSize: Int = 16 * 1024 * 1024,
verifyPeer: Bool = true
verifyPeer: Bool = true,
connectTimeoutMs: UInt32? = nil
) {
self.maxConnectionsPerEndpoint = maxConnectionsPerEndpoint
self.windowSize = windowSize
self.verifyPeer = verifyPeer
self.connectTimeoutMs = connectTimeoutMs
}
}

0 comments on commit 58eb4a3

Please sign in to comment.