-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Async calls from AsyncHTTPClient (#555)
* Use async HTTPClient calls Currently streaming is not working * Comments * Extract HTTPBody out so it can be used in request * Flush body, when not expecting anything * Remove eventLoop from function comment headers
- Loading branch information
1 parent
6531e31
commit 367f5ae
Showing
13 changed files
with
740 additions
and
770 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Soto for AWS open source project | ||
// | ||
// Copyright (c) 2023 the Soto project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of Soto project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the AsyncHTTPClient open source project | ||
// | ||
// Copyright (c) 2022 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@usableFromInline | ||
struct AnyAsyncSequence<Element>: Sendable, AsyncSequence { | ||
@usableFromInline typealias AsyncIteratorNextCallback = () async throws -> Element? | ||
|
||
@usableFromInline struct AsyncIterator: AsyncIteratorProtocol { | ||
@usableFromInline let nextCallback: AsyncIteratorNextCallback | ||
|
||
@inlinable init(nextCallback: @escaping AsyncIteratorNextCallback) { | ||
self.nextCallback = nextCallback | ||
} | ||
|
||
@inlinable mutating func next() async throws -> Element? { | ||
try await self.nextCallback() | ||
} | ||
} | ||
|
||
@usableFromInline var makeAsyncIteratorCallback: @Sendable () -> AsyncIteratorNextCallback | ||
|
||
@inlinable init<SequenceOfBytes>( | ||
_ asyncSequence: SequenceOfBytes | ||
) where SequenceOfBytes: AsyncSequence & Sendable, SequenceOfBytes.Element == Element { | ||
self.makeAsyncIteratorCallback = { | ||
var iterator = asyncSequence.makeAsyncIterator() | ||
return { | ||
try await iterator.next() | ||
} | ||
} | ||
} | ||
|
||
@inlinable func makeAsyncIterator() -> AsyncIterator { | ||
.init(nextCallback: self.makeAsyncIteratorCallback()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Soto for AWS open source project | ||
// | ||
// Copyright (c) 2017-2023 the Soto project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of Soto project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import NIOCore | ||
import NIOPosix | ||
|
||
/// Provide ByteBuffer as an AsyncSequence of equal size blocks | ||
struct ByteBufferAsyncSequence: AsyncSequence, Sendable { | ||
typealias Element = ByteBuffer | ||
|
||
let byteBuffer: ByteBuffer | ||
let chunkSize: Int | ||
|
||
init( | ||
_ byteBuffer: ByteBuffer, | ||
chunkSize: Int | ||
) { | ||
self.byteBuffer = byteBuffer | ||
self.chunkSize = chunkSize | ||
} | ||
|
||
struct AsyncIterator: AsyncIteratorProtocol { | ||
var byteBuffer: ByteBuffer | ||
let chunkSize: Int | ||
|
||
mutating func next() async throws -> ByteBuffer? { | ||
let size = Swift.min(self.chunkSize, self.byteBuffer.readableBytes) | ||
if size > 0 { | ||
return self.byteBuffer.readSlice(length: size) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func makeAsyncIterator() -> AsyncIterator { | ||
.init(byteBuffer: self.byteBuffer, chunkSize: self.chunkSize) | ||
} | ||
} | ||
|
||
extension ByteBuffer { | ||
func asyncSequence(chunkSize: Int) -> ByteBufferAsyncSequence { | ||
return ByteBufferAsyncSequence(self, chunkSize: chunkSize) | ||
} | ||
} |
Oops, something went wrong.