Skip to content

Commit fa8d65d

Browse files
sichanyooSichan Yoo
andauthored
Add maxConnections config to HttpClientConfiguration, and have it passed to HTTP client inits. (#915)
Co-authored-by: Sichan Yoo <[email protected]>
1 parent 4acd065 commit fa8d65d

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Sources/ClientRuntime/Config/DefaultSDKRuntimeConfiguration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public extension DefaultSDKRuntimeConfiguration {
9797
let connectTimeoutMs = httpClientConfiguration.connectTimeout.map { UInt32($0 * 1000) }
9898
let socketTimeout = UInt32(httpClientConfiguration.socketTimeout)
9999
let config = CRTClientEngineConfig(
100+
maxConnectionsPerEndpoint: httpClientConfiguration.maxConnections,
100101
telemetry: httpClientConfiguration.telemetry ?? CRTClientEngine.noOpCrtClientEngineTelemetry,
101102
connectTimeoutMs: connectTimeoutMs,
102103
crtTlsOptions: httpClientConfiguration.tlsConfiguration as? CRTClientTLSOptions,

Sources/ClientRuntime/Networking/Http/HttpClientConfiguration.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ public class HttpClientConfiguration {
2929
/// If none is provided, defaults to no extra headers.
3030
public var defaultHeaders: Headers
3131

32+
/// The maximum connections the HTTP client makes per host or per endpoint depending on the OS.
33+
///
34+
/// For Apple platforms, it will be per host.
35+
/// For Linux, it will be per endpoint (protocol + host + port).
36+
public var maxConnections: Int
37+
3238
// add any other properties here you want to give the service operations
3339
// control over to be mapped to the Http Client
3440

@@ -56,19 +62,30 @@ public class HttpClientConfiguration {
5662
/// Note that certain headers may cause your API request to fail. Defaults to no headers.
5763
/// - protocolType: The HTTP scheme (`http` or `https`) to be used for API requests. Defaults to the operation's standard configuration.
5864
/// - tlsConfiguration: Optional custom TLS configuration for HTTPS requests. If `nil`, defaults to a standard configuration.
65+
/// - maxConnections: The maximum number of connections the HTTP client makes per host (for Apple platforms) or per endpoint (for Linux). For non-mac Apple platforms, defaults to 6. For macOS and Linux, defaults to 50.
5966
public init(
6067
connectTimeout: TimeInterval? = nil,
6168
socketTimeout: TimeInterval = 60.0,
6269
protocolType: URIScheme = .https,
6370
defaultHeaders: Headers = Headers(),
6471
tlsConfiguration: (any TLSConfiguration)? = nil,
65-
telemetry: HttpTelemetry? = nil
72+
telemetry: HttpTelemetry? = nil,
73+
maxConnections: Int? = nil
6674
) {
6775
self.socketTimeout = socketTimeout
6876
self.protocolType = protocolType
6977
self.defaultHeaders = defaultHeaders
7078
self.connectTimeout = connectTimeout
7179
self.tlsConfiguration = tlsConfiguration
7280
self.telemetry = telemetry
81+
if let maxConnections {
82+
self.maxConnections = maxConnections
83+
} else {
84+
#if os(macOS) || os(Linux)
85+
self.maxConnections = 50
86+
#else // iOS, ipadOS, watchOS, tvOS.
87+
self.maxConnections = 6 // URLSession default.
88+
#endif
89+
}
7390
}
7491
}

Sources/ClientRuntime/Networking/Http/URLSession/URLSessionConfiguration+HTTPClientConfiguration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extension URLSessionConfiguration {
1717
config.httpShouldSetCookies = false
1818
config.httpCookieAcceptPolicy = .never
1919
config.httpCookieStorage = nil
20+
config.httpMaximumConnectionsPerHost = httpClientConfiguration.maxConnections
2021
return config
2122
}
2223
}

0 commit comments

Comments
 (0)