Skip to content

Replace TransactionBody with NIOAsyncSequenceProducer #677

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

Merged
merged 11 commits into from
Apr 12, 2023
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let package = Package(
.library(name: "AsyncHTTPClient", targets: ["AsyncHTTPClient"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.42.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.50.0"),
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.22.0"),
.package(url: "https://github.com/apple/swift-nio-http2.git", from: "1.19.0"),
.package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.13.0"),
Expand Down
2 changes: 1 addition & 1 deletion [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let package = Package(
.library(name: "AsyncHTTPClient", targets: ["AsyncHTTPClient"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.42.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.50.0"),
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.22.0"),
.package(url: "https://github.com/apple/swift-nio-http2.git", from: "1.19.0"),
.package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.13.0"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the AsyncHTTPClient open source project
//
// Copyright (c) 2023 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 NIOCore

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
@usableFromInline
struct AnyAsyncSequenceProducerDelegate: NIOAsyncSequenceProducerDelegate {
@usableFromInline
var delegate: NIOAsyncSequenceProducerDelegate

@inlinable
init<Delegate: NIOAsyncSequenceProducerDelegate>(_ delegate: Delegate) {
self.delegate = delegate
}

@inlinable
func produceMore() {
self.delegate.produceMore()
}

@inlinable
func didTerminate() {
self.delegate.didTerminate()
}
}
40 changes: 28 additions & 12 deletions Sources/AsyncHTTPClient/AsyncAwait/HTTPClientResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,25 @@ public struct HTTPClientResponse: Sendable {
}

init(
bag: Transaction,
requestMethod: HTTPMethod,
version: HTTPVersion,
status: HTTPResponseStatus,
headers: HTTPHeaders,
requestMethod: HTTPMethod
body: TransactionBody
) {
let contentLength = HTTPClientResponse.expectedContentLength(requestMethod: requestMethod, headers: headers, status: status)
self.init(version: version, status: status, headers: headers, body: .init(TransactionBody(bag, expectedContentLength: contentLength)))
self.init(
version: version,
status: status,
headers: headers,
body: .init(.transaction(
body,
expectedContentLength: HTTPClientResponse.expectedContentLength(
requestMethod: requestMethod,
headers: headers,
status: status
)
))
)
}
}

Expand Down Expand Up @@ -94,8 +105,8 @@ extension HTTPClientResponse {
/// - Returns: the number of bytes collected over time
@inlinable public func collect(upTo maxBytes: Int) async throws -> ByteBuffer {
switch self.storage {
case .transaction(let transactionBody):
if let contentLength = transactionBody.expectedContentLength {
case .transaction(_, let expectedContentLength):
if let contentLength = expectedContentLength {
if contentLength > maxBytes {
throw NIOTooManyBytesError()
}
Expand Down Expand Up @@ -127,10 +138,19 @@ extension HTTPClientResponse {
}
}

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
@usableFromInline
typealias TransactionBody = NIOThrowingAsyncSequenceProducer<
ByteBuffer,
Error,
NIOAsyncSequenceProducerBackPressureStrategies.HighLowWatermark,
AnyAsyncSequenceProducerDelegate
>

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension HTTPClientResponse.Body {
@usableFromInline enum Storage: Sendable {
case transaction(TransactionBody)
case transaction(TransactionBody, expectedContentLength: Int?)
case anyAsyncSequence(AnyAsyncSequence<ByteBuffer>)
}
}
Expand All @@ -141,7 +161,7 @@ extension HTTPClientResponse.Body.Storage: AsyncSequence {

@inlinable func makeAsyncIterator() -> AsyncIterator {
switch self {
case .transaction(let transaction):
case .transaction(let transaction, _):
return .transaction(transaction.makeAsyncIterator())
case .anyAsyncSequence(let anyAsyncSequence):
return .anyAsyncSequence(anyAsyncSequence.makeAsyncIterator())
Expand Down Expand Up @@ -172,10 +192,6 @@ extension HTTPClientResponse.Body.Storage.AsyncIterator: AsyncIteratorProtocol {

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension HTTPClientResponse.Body {
init(_ body: TransactionBody) {
self.init(storage: .transaction(body))
}

@inlinable init(_ storage: Storage) {
self.storage = storage
}
Expand Down
Loading