mirror of
https://github.com/swift-server/swift-openapi-async-http-client.git
synced 2026-05-03 07:42:29 +00:00
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 <dnadoba@gmail.com>
This commit is contained in:
@@ -68,15 +68,24 @@ public struct AsyncHTTPClientTransport: ClientTransport {
|
||||
/// The HTTP client used for performing HTTP calls.
|
||||
public var client: HTTPClient
|
||||
|
||||
/// The default shared HTTP client.
|
||||
///
|
||||
/// This is a workaround for the lack of a shared client
|
||||
/// in AsyncHTTPClient. Do not use this value directly, outside of
|
||||
/// the `Configuration.init(client:timeout:)` initializer, as it will
|
||||
/// likely be removed in the future.
|
||||
private static let sharedClient: HTTPClient = .init()
|
||||
|
||||
/// The default request timeout.
|
||||
public var timeout: TimeAmount
|
||||
|
||||
/// Creates a new configuration with the specified client and timeout.
|
||||
/// - Parameters:
|
||||
/// - client: The underlying client used to perform HTTP operations.
|
||||
/// Provide nil to use the shared internal client.
|
||||
/// - timeout: The request timeout, defaults to 1 minute.
|
||||
public init(client: HTTPClient = .init(), timeout: TimeAmount = .minutes(1)) {
|
||||
self.client = client
|
||||
public init(client: HTTPClient? = nil, timeout: TimeAmount = .minutes(1)) {
|
||||
self.client = client ?? Self.sharedClient
|
||||
self.timeout = timeout
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user