Skip to content

Add an AsyncWriter #519

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 4 commits into from
Dec 6, 2021
Merged

Conversation

fabianfett
Copy link
Member

Motivation

This PR adds an AsyncWriter that we can be used to test our new async upload streaming API.

Changes

  • Adds an AsyncWriter to test utils

Result

We will be able to test async upload streaming API in #518.

@fabianfett fabianfett added the semver/none No version bump required. label Dec 6, 2021
@fabianfett fabianfett requested review from glbrntt and dnadoba December 6, 2021 08:29
@fabianfett fabianfett mentioned this pull request Dec 6, 2021

let errorAction = self.lock.withLock { () -> ErrorAction in
switch self._state {
case .buffering:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intended that we drop all buffered elements if we fail self? If yes we should document that behaviour. Otherwise we could use Result<ByteBuffer?, Error> and enqueue the error properly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a precondition that checks that the demand continuation is nil, otherwise we will never resume it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or fail the continuation.

}
}

public func demand() async throws {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this method used for? Does it wait until someone calls next but the buffer is empty?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment.

}

private enum State {
case buffering(CircularBuffer<ByteBuffer?>, CheckedContinuation<Void, Error>?)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to know that this is a ByteBuffer? Otherwise I would prefer it to be generic to reduce the accessible API surface inside the implementation of AsyncSequenceWriter.

@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
class AsyncSequenceWriter<E>: AsyncSequence {
typealias AsyncIterator = Iterator
typealias Element = E
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just call the generic Element and then don't need this typealias.

case none
}

private func writeBufferOrEnd(_ byteBuffer: Element?) {
Copy link
Collaborator

@dnadoba dnadoba Dec 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: rename byteBuffer to just element everywhere

@fabianfett
Copy link
Member Author

Thanks for the thorough code review @dnadoba. Great catches. Hope all is fixed now.

Copy link
Collaborator

@dnadoba dnadoba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! That's really convenient test utility which we will use a lot in the upcoming async/await tests.

@dnadoba
Copy link
Collaborator

dnadoba commented Dec 6, 2021

@swift-server-bot test this please

private var _state = State.buffering(.init(), nil)
private let lock = Lock()

public var hasDemand: Bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: drop the public as the class is internal (and elsewhere)

@fabianfett fabianfett merged commit a79206d into swift-server:main Dec 6, 2021
@fabianfett fabianfett deleted the ff-add-async-writer branch December 6, 2021 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/none No version bump required.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants