async-http-client icon indicating copy to clipboard operation
async-http-client copied to clipboard

offer Structured Concurrency API that supports HTTP response streaming

Open weissi opened this issue 10 months ago • 4 comments

Currently, most of HTTPClient's API surface only supports Structured Concurrency violating functions. Baby steps to rectify this have been done in #806. But the elephant in the room is of course the unstructured HTTP request API.

We need to also provide a

try await httpClient.execute(request) { status, responseBodyStream in
   ...
}

API that fulfils Structured Concurrency by supporting cancellation and tearing everything down when the body function returns or throws without ineffective and ugly hacks like deinit (which still violates Structured Concurrency because it doesn't follow the structure of the code).

weissi avatar Feb 05 '25 11:02 weissi

I think we need slightly more since request can also be streamed and AsyncSequence is not enough for that. I'm thinking gRPC got it right here:

swift

try await httpClient.execute { requestWriter in
  try await requestWriter.write()
  return [:] // trailers
} onResponse: { status, responseBodyStream, trailers in
   print(status)
   for try await responseData in responseBodyStream { ... }
   try await trailers.value()
}

FranzBusch avatar Feb 05 '25 13:02 FranzBusch

I think we need slightly more since request can also be streamed and AsyncSequence is not enough for that.

Possibly, but currently we do need an AsyncSequence<BB>, right? So this is will legal

try await httpClient.post("https://foo.com", body: myAsyncSequence)

Don't need any with* stuff here.

weissi avatar Feb 05 '25 14:02 weissi

I think we need slightly more since request can also be streamed and AsyncSequence is not enough for that.

Possibly, but currently we do need an AsyncSequence<BB>, right? So this is will legal

try await httpClient.post("https://foo.com", body: myAsyncSequence)

Don't need any with* stuff here.

Yes if we just wrap the current AHC APIs than this is enough. In general this API should always be legal it is just a convenience on top of the bi-di streaming API with a writer.

FranzBusch avatar Feb 05 '25 14:02 FranzBusch

Yes if we just wrap the current AHC APIs than this is enough. In general this API should always be legal it is just a convenience on top of the bi-di streaming API with a writer.

Gotcha, yes. So I think there are (at least) two parts to this:

  1. For any existing AHC API offer one that does the same but adds Structured Concurrency
  2. Offer other, more general APIs

I was thinking (1) for this ticket and you added (2). That's fine, I'm happy with this, just to check we're on the same page.

weissi avatar Feb 05 '25 15:02 weissi