Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension HTTPClientRequest {
enum Body {
case asyncSequence(
length: RequestBodyLength,
nextBodyPart: (ByteBufferAllocator) async throws -> ByteBuffer?
makeAsyncIterator: @Sendable () -> ((ByteBufferAllocator) async throws -> ByteBuffer?)
)
case sequence(
length: RequestBodyLength,
Expand Down Expand Up @@ -80,7 +80,7 @@ extension HTTPClientRequest.Prepared.Body {
init(_ body: HTTPClientRequest.Body) {
switch body.mode {
case .asyncSequence(let length, let makeAsyncIterator):
self = .asyncSequence(length: length, nextBodyPart: makeAsyncIterator())
self = .asyncSequence(length: length, makeAsyncIterator: makeAsyncIterator)
case .sequence(let length, let canBeConsumedMultipleTimes, let makeCompleteBody):
self = .sequence(
length: length,
Expand Down
6 changes: 6 additions & 0 deletions Sources/AsyncHTTPClient/AsyncAwait/HTTPClientRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,9 @@ extension HTTPClientRequest.Body {
}
}
}

@available(*, unavailable)
extension HTTPClientRequest.Body.AsyncIterator: Sendable {}

@available(*, unavailable)
extension HTTPClientRequest.Body.AsyncIterator.Storage: Sendable {}
8 changes: 5 additions & 3 deletions Sources/AsyncHTTPClient/AsyncAwait/Transaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ final class Transaction:

private func continueRequestBodyStream(
_ allocator: ByteBufferAllocator,
next: @escaping ((ByteBufferAllocator) async throws -> ByteBuffer?)
makeAsyncIterator: @Sendable @escaping () -> ((ByteBufferAllocator) async throws -> ByteBuffer?)
) {
Task {
let next = makeAsyncIterator()

do {
while let part = try await next(allocator) {
do {
Expand Down Expand Up @@ -199,9 +201,9 @@ extension Transaction: HTTPExecutableRequest {

case .startStream(let allocator):
switch self.request.body {
case .asyncSequence(_, let next):
case .asyncSequence(_, let makeAsyncIterator):
// it is safe to call this async here. it dispatches...
self.continueRequestBodyStream(allocator, next: next)
self.continueRequestBodyStream(allocator, makeAsyncIterator: makeAsyncIterator)

case .byteBuffer(let byteBuffer):
self.writeOnceAndOneTimeOnly(byteBuffer: byteBuffer)
Expand Down
3 changes: 2 additions & 1 deletion Tests/AsyncHTTPClientTests/HTTPClientRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,9 @@ extension Optional where Wrapped == HTTPClientRequest.Prepared.Body {
throw LengthMismatch(announcedLength: announcedLength, actualLength: Int64(buffer.readableBytes))
}
return buffer
case .asyncSequence(length: let announcedLength, let generate):
case .asyncSequence(length: let announcedLength, let makeAsyncIterator):
var accumulatedBuffer = ByteBuffer()
let generate = makeAsyncIterator()
while var buffer = try await generate(ByteBufferAllocator()) {
accumulatedBuffer.writeBuffer(&buffer)
}
Expand Down
Loading