Skip to content
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

baby steps towards a Structured Concurrency API #806

Merged
merged 1 commit into from
Feb 6, 2025

Conversation

weissi
Copy link
Contributor

@weissi weissi commented Feb 5, 2025

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.

@weissi weissi requested review from FranzBusch and Lukasa February 5, 2025 10:33
@weissi weissi added the 🆕 semver/minor Adds new public API. label Feb 5, 2025
Copy link
Collaborator

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with this, but would like @fabianfett to review as well.

@FranzBusch
Copy link
Collaborator

I left some Sendable correctness remarks. Overall this looks good to me.

Comment on lines 39 to 44
public static func withHTTPClient<R: Sendable>(
eventLoopGroup: any EventLoopGroup = HTTPClient.defaultEventLoopGroup,
configuration: Configuration = Configuration(),
backgroundActivityLogger: Logger? = nil,
_ body: @escaping @Sendable (HTTPClient) async throws -> R
) async throws -> R {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static func withHTTPClient<R: Sendable>(
eventLoopGroup: any EventLoopGroup = HTTPClient.defaultEventLoopGroup,
configuration: Configuration = Configuration(),
backgroundActivityLogger: Logger? = nil,
_ body: @escaping @Sendable (HTTPClient) async throws -> R
) async throws -> R {
public static func withHTTPClient<Result: Sendable>(
eventLoopGroup: any EventLoopGroup = HTTPClient.defaultEventLoopGroup,
configuration: Configuration = Configuration(),
backgroundActivityLogger: Logger? = nil,
_ body: (HTTPClient) async throws -> Result
) async throws -> Result {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's in the 5.9 part

Sources/AsyncHTTPClient/StructuredConcurrencyHelpers.swift Outdated Show resolved Hide resolved
Sources/AsyncHTTPClient/StructuredConcurrencyHelpers.swift Outdated Show resolved Hide resolved
Sources/AsyncHTTPClient/StructuredConcurrencyHelpers.swift Outdated Show resolved Hide resolved
@weissi weissi force-pushed the jw-baby-sc branch 4 times, most recently from 11db0bf to 4f6856d Compare February 6, 2025 15:48
@weissi weissi requested a review from FranzBusch February 6, 2025 15:52
@weissi
Copy link
Contributor Author

weissi commented Feb 6, 2025

Thanks @FranzBusch for advice here! As you know, I still use Sendable everything -- but yeah for public SemVer'd APIs that is probably an issue, particularly with @MainActor that is of course used heavily on Apple platforms.

@weissi weissi merged commit 89dc8d0 into swift-server:main Feb 6, 2025
20 of 22 checks passed
@weissi weissi deleted the jw-baby-sc branch February 6, 2025 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🆕 semver/minor Adds new public API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants