Skip to content

Commit f228a33

Browse files
czechboy0dnadoba
andauthored
Adopt a custom shared client (#18)
### Motivation We previously defaulted the HTTPClient to .init(), but that's not correct as it was never getting shut down. ### Modifications Instead of creating a new client, just introduce our own shared one and use that as the default value. Once AHC provides a shared client, we can default to that in the configuration initializer. ### Result No more crashing clients on dealloc. ### Test Plan All tests pass. --------- Co-authored-by: David Nadoba <[email protected]>
1 parent 7e40bff commit f228a33

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

Sources/OpenAPIAsyncHTTPClient/AsyncHTTPClientTransport.swift

+11-2
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,24 @@ public struct AsyncHTTPClientTransport: ClientTransport {
6868
/// The HTTP client used for performing HTTP calls.
6969
public var client: HTTPClient
7070

71+
/// The default shared HTTP client.
72+
///
73+
/// This is a workaround for the lack of a shared client
74+
/// in AsyncHTTPClient. Do not use this value directly, outside of
75+
/// the `Configuration.init(client:timeout:)` initializer, as it will
76+
/// likely be removed in the future.
77+
private static let sharedClient: HTTPClient = .init()
78+
7179
/// The default request timeout.
7280
public var timeout: TimeAmount
7381

7482
/// Creates a new configuration with the specified client and timeout.
7583
/// - Parameters:
7684
/// - client: The underlying client used to perform HTTP operations.
85+
/// Provide nil to use the shared internal client.
7786
/// - timeout: The request timeout, defaults to 1 minute.
78-
public init(client: HTTPClient = .init(), timeout: TimeAmount = .minutes(1)) {
79-
self.client = client
87+
public init(client: HTTPClient? = nil, timeout: TimeAmount = .minutes(1)) {
88+
self.client = client ?? Self.sharedClient
8089
self.timeout = timeout
8190
}
8291
}

Tests/OpenAPIAsyncHTTPClientTests/Test_AsyncHTTPClientTransport.swift

+1-9
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,8 @@ class Test_AsyncHTTPClientTransport: XCTestCase {
8282
}
8383

8484
func testSend() async throws {
85-
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
86-
let httpClient = HTTPClient(
87-
eventLoopGroupProvider: .shared(eventLoopGroup),
88-
configuration: .init()
89-
)
90-
defer {
91-
try! httpClient.syncShutdown()
92-
}
9385
let transport = AsyncHTTPClientTransport(
94-
configuration: .init(client: httpClient),
86+
configuration: .init(),
9587
requestSender: TestSender.test
9688
)
9789
let request: OpenAPIRuntime.Request = .init(

0 commit comments

Comments
 (0)