-
Notifications
You must be signed in to change notification settings - Fork 126
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
Add an AsyncWriter #519
Conversation
|
||
let errorAction = self.lock.withLock { () -> ErrorAction in | ||
switch self._state { | ||
case .buffering: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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>?) |
There was a problem hiding this comment.
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
.
Co-authored-by: George Barnett <gbarnett@apple.com>
d094a67
to
56f72d6
Compare
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) | ||
class AsyncSequenceWriter<E>: AsyncSequence { | ||
typealias AsyncIterator = Iterator | ||
typealias Element = E |
There was a problem hiding this comment.
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?) { |
There was a problem hiding this comment.
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
56f72d6
to
b9a2b21
Compare
Thanks for the thorough code review @dnadoba. Great catches. Hope all is fixed now. |
b9a2b21
to
660426f
Compare
660426f
to
cbc2446
Compare
There was a problem hiding this 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.
@swift-server-bot test this please |
private var _state = State.buffering(.init(), nil) | ||
private let lock = Lock() | ||
|
||
public var hasDemand: Bool { |
There was a problem hiding this comment.
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)
Motivation
This PR adds an AsyncWriter that we can be used to test our new async upload streaming API.
Changes
Result
We will be able to test async upload streaming API in #518.