Files
async-http-client/Sources/AsyncHTTPClient/HTTPClient+StructuredConcurrency.swift
T
Johannes Weiss 89dc8d0068 baby steps towards a Structured Concurrency API (#806)
At the moment, `HTTPClient`'s entire API surface violates Structured
Concurrency. Both the creation & shutdown of a HTTP client as well as
making requests (#807) doesn't follow Structured Concurrency. Some of
the problems are:

1. Upon return of methods, resources are still in active use in other
threads/tasks
2. Cancellation doesn't always work

This PR is baby steps towards a Structured Concurrency API, starting
with start/shutdown of the HTTP client.

Co-authored-by: Johannes Weiss <johannes@jweiss.io>
2025-02-06 17:11:37 +00:00

73 lines
3.0 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the AsyncHTTPClient open source project
//
// Copyright (c) 2025 Apple Inc. and the AsyncHTTPClient project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Logging
import NIO
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension HTTPClient {
#if compiler(>=6.0)
/// Start & automatically shut down a new ``HTTPClient``.
///
/// This method allows to start & automatically dispose of a ``HTTPClient`` following the principle of Structured Concurrency.
/// The ``HTTPClient`` is guaranteed to be shut down upon return, whether `body` throws or not.
///
/// This may be particularly useful if you cannot use the shared singleton (``HTTPClient/shared``).
public static func withHTTPClient<Return>(
eventLoopGroup: any EventLoopGroup = HTTPClient.defaultEventLoopGroup,
configuration: Configuration = Configuration(),
backgroundActivityLogger: Logger? = nil,
isolation: isolated (any Actor)? = #isolation,
_ body: (HTTPClient) async throws -> Return
) async throws -> Return {
let logger = (backgroundActivityLogger ?? HTTPClient.loggingDisabled)
let httpClient = HTTPClient(
eventLoopGroup: eventLoopGroup,
configuration: configuration,
backgroundActivityLogger: logger
)
return try await asyncDo {
try await body(httpClient)
} finally: { _ in
try await httpClient.shutdown()
}
}
#else
/// Start & automatically shut down a new ``HTTPClient``.
///
/// This method allows to start & automatically dispose of a ``HTTPClient`` following the principle of Structured Concurrency.
/// The ``HTTPClient`` is guaranteed to be shut down upon return, whether `body` throws or not.
///
/// This may be particularly useful if you cannot use the shared singleton (``HTTPClient/shared``).
public static func withHTTPClient<Return: Sendable>(
eventLoopGroup: any EventLoopGroup = HTTPClient.defaultEventLoopGroup,
configuration: Configuration = Configuration(),
backgroundActivityLogger: Logger? = nil,
_ body: (HTTPClient) async throws -> Return
) async throws -> Return {
let logger = (backgroundActivityLogger ?? HTTPClient.loggingDisabled)
let httpClient = HTTPClient(
eventLoopGroup: eventLoopGroup,
configuration: configuration,
backgroundActivityLogger: logger
)
return try await asyncDo {
try await body(httpClient)
} finally: { _ in
try await httpClient.shutdown()
}
}
#endif
}