Use new final element support from AsyncStreaming#170
Conversation
## Motivation `AsyncStreaming` added support for final elements, which lets us drop our concluding reader and writer protocols and use `AsyncReader` and `CallerAsyncWriter` directly with `FinalElement == HTTPFields?` to carry the HTTP trailers inline. ## Modifications This PR moves the HTTP body shape over to `AsyncReader` and `CallerAsyncWriter` from `AsyncStreaming` and introduces a new `HTTPResponseSender` protocol. With both together the code becomes significantly easier, we get to fuse the final buffer and the fin flag on the wire, and `HTTPResponseSender` makes it possible to flush head, body and trailers all in one go. All four client and server conformers (URLSession, AsyncHTTPClient, Fetch, NIO) and the example middlewares are updated to the new shape, and the in-process test harness uses upstream `DuplexAsyncChannel` for both directions. ## Result No more concluding reader and writer APIs and instead only async streaming and HTTP specific types.
8e3a65d to
45aa7e1
Compare
| url: "https://github.com/apple/swift-async-algorithms.git", | ||
| from: "1.1.4", | ||
| url: "https://github.com/FranzBusch/swift-async-algorithms.git", | ||
| revision: "e1a6dff375ca9fc079d02276f489663ad9204e01", |
There was a problem hiding this comment.
We need to wait for a release with this PR apple/swift-async-algorithms#425
| /// | ||
| /// Convenience for HTTP body readers (`FinalElement == HTTPFields?`). | ||
| /// The buffer's initial free capacity acts as a hard cap; surplus bytes | ||
| /// are read and discarded. |
There was a problem hiding this comment.
Let's revisit the behavior of collect later. I think it should throw or at least make people aware that the buffer contains truncated data.
There was a problem hiding this comment.
Agreed. There is a TODO here to move this over to async algos. We should revisit the behavior then
| done = true | ||
| } | ||
| if chunk.count == 0 { | ||
| if finalElement == nil { |
There was a problem hiding this comment.
What does this check do? Why is done true when the chunk is empty and there is no final element?
Also double optional is a bit trippy. Does this check pass just .none or .some(.none) as well?
|
Thanks for doing this! Great that it's a net deletion of lines |
| /// Convenience for HTTP body readers (`FinalElement == HTTPFields?`). | ||
| /// The buffer's initial free capacity acts as a hard cap; surplus bytes | ||
| /// are read and discarded. | ||
| // TODO: This should be moved to the AsyncStreaming module |
There was a problem hiding this comment.
Some reason why it's not there now?
| // TODO: This is a temporary fix that informs clients that this server does not support | ||
| // keep-alive. This server should be updated to eventually support keep-alive. |
There was a problem hiding this comment.
This has been fixed upstream. I don't know if you care about it though, or if someone should focus on merging #137
There was a problem hiding this comment.
We need to land this prior to fixing #137 since it requires the server repo to use the same updated abstract APIs
121541e to
c1acb68
Compare
0ace645 to
0f5a5f3
Compare
This unblocks apple/swift-http-api-proposal#170 --------- Co-authored-by: Fabian Fett <fabianfett@apple.com> Co-authored-by: George Barnett <gbarnett@apple.com> Co-authored-by: Mads Odgaard <mads.odgaard@frameo.com> Co-authored-by: Cory Benfield <lukasa@apple.com> Co-authored-by: Si Beaumont <simonjbeaumont@gmail.com> Co-authored-by: hamzahrmalik <hamzah_malik@apple.com> Co-authored-by: PAVANSAI KUMAR ALLADI <ptpavansai3@gmail.com> Co-authored-by: Pavan sai kumar alladi <p_alladi@apple.com>
Motivation
AsyncStreamingadded support for final elements which will allow us to remove our concluding reader and writer protocols.Modifications
This PR moves over to the final elements from
AsyncStreamingand introduces a newHTTPResponseSenderprotocol. With both of them together the code becomes significantly easier and we support fusing the final buffer and the fin flag on the wire. Furthermore, theHTTPResponseSendermakes it possible to flush head, body and trailers all in one go.## Result
No more concluding reader and writer APIs and instead only async streaming and HTTP specific types.