Skip to content

Commit 58eb4a3

Browse files
authored
feat: make connectTimeoutMs configurable (#592)
1 parent 2e4e744 commit 58eb4a3

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

Sources/ClientRuntime/Config/DefaultSDKRuntimeConfiguration.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ public extension DefaultSDKRuntimeConfiguration {
9090
/// Is the CRT HTTP client.
9191
static var defaultHttpClientEngine: HttpClientEngine { CRTClientEngine() }
9292

93+
/// The default HTTP client with a specified timeout
94+
///
95+
/// Is the CRT HTTP client.
96+
static func httpClientEngineWithTimeout(timeoutMs: UInt32) -> HttpClientEngine {
97+
return CRTClientEngine(config: CRTClientEngineConfig(connectTimeoutMs: timeoutMs))
98+
}
99+
93100
/// The HTTP client configuration to use when none is provided.
94101
///
95102
/// Is the CRT HTTP client's configuration.

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ public class CRTClientEngine: HttpClientEngine {
1919
private var connectionPools: [Endpoint: HTTPClientConnectionManager] = [:]
2020
private var http2ConnectionPools: [Endpoint: HTTP2StreamManager] = [:]
2121
private let sharedDefaultIO = SDKDefaultIO.shared
22+
private let connectTimeoutMs: UInt32?
2223

2324
init(config: CRTClientEngineConfig) {
2425
self.windowSize = config.windowSize
2526
self.maxConnectionsPerEndpoint = config.maxConnectionsPerEndpoint
2627
self.logger = SwiftLogger(label: "SerialExecutor")
28+
self.connectTimeoutMs = config.connectTimeoutMs
2729
}
2830

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

5557
var socketOptions = SocketOptions(socketType: .stream)
5658
#if os(iOS) || os(watchOS)
57-
socketOptions.connectTimeoutMs = 30_000
59+
socketOptions.connectTimeoutMs = self.connectTimeoutMs ?? 30_000
60+
#else
61+
if let timeout = self.connectTimeoutMs {
62+
socketOptions.connectTimeoutMs = timeout
63+
}
5864
#endif
5965
let options = HTTPClientConnectionOptions(
6066
clientBootstrap: sharedDefaultIO.clientBootstrap,
@@ -78,7 +84,11 @@ public class CRTClientEngine: HttpClientEngine {
7884
private func createHTTP2ConnectionPool(endpoint: Endpoint) throws -> HTTP2StreamManager {
7985
var socketOptions = SocketOptions(socketType: .stream)
8086
#if os(iOS) || os(watchOS)
81-
socketOptions.connectTimeoutMs = 30_000
87+
socketOptions.connectTimeoutMs = self.connectTimeoutMs ?? 30_000
88+
#else
89+
if let timeout = self.connectTimeoutMs {
90+
socketOptions.connectTimeoutMs = timeout
91+
}
8292
#endif
8393
let tlsConnectionOptions = TLSConnectionOptions(
8494
context: sharedDefaultIO.tlsContext,

Sources/ClientRuntime/Networking/Http/CRT/CRTClientEngineConfig.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@ struct CRTClientEngineConfig {
1717
/// If you set this in server mode, it enforces client authentication.
1818
let verifyPeer: Bool
1919

20+
/// Timeout in MS for connections
21+
let connectTimeoutMs: UInt32?
22+
2023
public init(
2124
maxConnectionsPerEndpoint: Int = 50,
2225
windowSize: Int = 16 * 1024 * 1024,
23-
verifyPeer: Bool = true
26+
verifyPeer: Bool = true,
27+
connectTimeoutMs: UInt32? = nil
2428
) {
2529
self.maxConnectionsPerEndpoint = maxConnectionsPerEndpoint
2630
self.windowSize = windowSize
2731
self.verifyPeer = verifyPeer
32+
self.connectTimeoutMs = connectTimeoutMs
2833
}
2934
}

0 commit comments

Comments
 (0)