Skip to content

[HTTPClient.Configuration] Make connection pool size configurable #437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ final class HTTPConnectionPool {
self._state = StateMachine(
eventLoopGroup: eventLoopGroup,
idGenerator: idGenerator,
maximumConcurrentHTTP1Connections: 8
maximumConcurrentHTTP1Connections: clientConfiguration.connectionPool.concurrentHTTP1ConnectionsPerHostSoftLimit
)
}

Expand Down
12 changes: 11 additions & 1 deletion Sources/AsyncHTTPClient/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -849,11 +849,21 @@ extension HTTPClient.Configuration {

/// Connection pool configuration.
public struct ConnectionPool: Hashable {
// Specifies amount of time connections are kept idle in the pool.
/// Specifies amount of time connections are kept idle in the pool. After this time has passed without a new
/// request the connections are closed.
public var idleTimeout: TimeAmount

/// The maximum number of connections that are kept alive in the connection pool per host. If requests with
/// an explicit eventLoopRequirement are sent, this number might be exceeded due to overflow connections.
public var concurrentHTTP1ConnectionsPerHostSoftLimit: Int

public init(idleTimeout: TimeAmount = .seconds(60)) {
self.init(idleTimeout: idleTimeout, concurrentHTTP1ConnectionsPerHostSoftLimit: 8)
}

public init(idleTimeout: TimeAmount, concurrentHTTP1ConnectionsPerHostSoftLimit: Int) {
self.idleTimeout = idleTimeout
self.concurrentHTTP1ConnectionsPerHostSoftLimit = concurrentHTTP1ConnectionsPerHostSoftLimit
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ extension HTTPClientTests {
("testCloseWhileBackpressureIsExertedIsFine", testCloseWhileBackpressureIsExertedIsFine),
("testErrorAfterCloseWhileBackpressureExerted", testErrorAfterCloseWhileBackpressureExerted),
("testRequestSpecificTLS", testRequestSpecificTLS),
("testConnectionPoolSizeConfigValueIsRespected", testConnectionPoolSizeConfigValueIsRespected),
]
}
}
43 changes: 43 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3060,4 +3060,47 @@ class HTTPClientTests: XCTestCase {
XCTAssertEqual(firstConnectionNumber, secondConnectionNumber, "Identical TLS configurations did not use the same connection")
XCTAssertNotEqual(thirdConnectionNumber, firstConnectionNumber, "Different TLS configurations did not use different connections.")
}

func testConnectionPoolSizeConfigValueIsRespected() {
let numberOfRequestsPerThread = 1000
let numberOfParallelWorkers = 16
let poolSize = 12

let httpBin = HTTPBin()
defer { XCTAssertNoThrow(try httpBin.shutdown()) }

let group = MultiThreadedEventLoopGroup(numberOfThreads: 4)
defer { XCTAssertNoThrow(try group.syncShutdownGracefully()) }

let configuration = HTTPClient.Configuration(
connectionPool: .init(
idleTimeout: .seconds(30),
concurrentHTTP1ConnectionsPerHostSoftLimit: poolSize
)
)
let client = HTTPClient(eventLoopGroupProvider: .shared(group), configuration: configuration)
defer { XCTAssertNoThrow(try client.syncShutdown()) }

let g = DispatchGroup()
for workerID in 0..<numberOfParallelWorkers {
DispatchQueue(label: "\(#file):\(#line):worker-\(workerID)").async(group: g) {
func makeRequest() {
let url = "http://127.0.0.1:\(httpBin.port)"
XCTAssertNoThrow(try client.get(url: url).wait())
}
for _ in 0..<numberOfRequestsPerThread {
makeRequest()
}
}
}
let timeout = DispatchTime.now() + .seconds(60)
switch g.wait(timeout: timeout) {
case .success:
break
case .timedOut:
XCTFail("Timed out")
}

XCTAssertEqual(httpBin.createdConnections, poolSize)
}
}