-
-
Notifications
You must be signed in to change notification settings - Fork 87
Add RowStreamStateMachine #176
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
fabianfett
merged 1 commit into
vapor:main
from
fabianfett:ff-add-rowstreamstatemachine
Sep 18, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
163 changes: 163 additions & 0 deletions
163
Sources/PostgresNIO/New/Connection State Machine/RowStreamStateMachine.swift
This file contains hidden or 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,163 @@ | ||
import NIOCore | ||
|
||
/// A sub state for receiving data rows. Stores whether the consumer has either signaled demand and whether the | ||
/// channel has issued `read` events. | ||
/// | ||
/// This should be used as a SubStateMachine in QuerySubStateMachines. | ||
struct RowStreamStateMachine { | ||
|
||
enum Action { | ||
case read | ||
case wait | ||
} | ||
|
||
private enum State { | ||
/// The state machines expects further writes to `channelRead`. The writes are appended to the buffer. | ||
case waitingForRows(CircularBuffer<PSQLBackendMessage.DataRow>) | ||
/// The state machines expects a call to `demandMoreResponseBodyParts` or `read`. The buffer is | ||
/// empty. It is preserved for performance reasons. | ||
case waitingForReadOrDemand(CircularBuffer<PSQLBackendMessage.DataRow>) | ||
/// The state machines expects a call to `read`. The buffer is empty. It is preserved for performance reasons. | ||
case waitingForRead(CircularBuffer<PSQLBackendMessage.DataRow>) | ||
/// The state machines expects a call to `demandMoreResponseBodyParts`. The buffer is empty. It is | ||
/// preserved for performance reasons. | ||
case waitingForDemand(CircularBuffer<PSQLBackendMessage.DataRow>) | ||
|
||
case modifying | ||
} | ||
|
||
private var state: State | ||
|
||
init() { | ||
self.state = .waitingForRows(CircularBuffer(initialCapacity: 32)) | ||
} | ||
|
||
mutating func receivedRow(_ newRow: PSQLBackendMessage.DataRow) { | ||
switch self.state { | ||
case .waitingForRows(var buffer): | ||
self.state = .modifying | ||
buffer.append(newRow) | ||
self.state = .waitingForRows(buffer) | ||
|
||
// For all the following cases, please note: | ||
// Normally these code paths should never be hit. However there is one way to trigger | ||
// this: | ||
// | ||
// If the server decides to close a connection, NIO will forward all outstanding | ||
// `channelRead`s without waiting for a next `context.read` call. For this reason we might | ||
// receive new rows, when we don't expect them here. | ||
case .waitingForRead(var buffer): | ||
self.state = .modifying | ||
buffer.append(newRow) | ||
self.state = .waitingForRead(buffer) | ||
|
||
case .waitingForDemand(var buffer): | ||
self.state = .modifying | ||
buffer.append(newRow) | ||
self.state = .waitingForDemand(buffer) | ||
|
||
case .waitingForReadOrDemand(var buffer): | ||
self.state = .modifying | ||
buffer.append(newRow) | ||
self.state = .waitingForReadOrDemand(buffer) | ||
|
||
case .modifying: | ||
preconditionFailure("Invalid state: \(self.state)") | ||
} | ||
} | ||
|
||
mutating func channelReadComplete() -> CircularBuffer<PSQLBackendMessage.DataRow>? { | ||
switch self.state { | ||
case .waitingForRows(let buffer): | ||
if buffer.isEmpty { | ||
self.state = .waitingForRead(buffer) | ||
return nil | ||
} else { | ||
var newBuffer = buffer | ||
newBuffer.removeAll(keepingCapacity: true) | ||
self.state = .waitingForReadOrDemand(newBuffer) | ||
return buffer | ||
} | ||
|
||
case .waitingForRead, | ||
.waitingForDemand, | ||
.waitingForReadOrDemand: | ||
preconditionFailure("How can we receive a body part, after a channelReadComplete, but no read has been forwarded yet. Invalid state: \(self.state)") | ||
|
||
case .modifying: | ||
preconditionFailure("Invalid state: \(self.state)") | ||
} | ||
} | ||
|
||
mutating func demandMoreResponseBodyParts() -> Action { | ||
switch self.state { | ||
case .waitingForDemand(let buffer): | ||
self.state = .waitingForRows(buffer) | ||
return .read | ||
|
||
case .waitingForReadOrDemand(let buffer): | ||
self.state = .waitingForRead(buffer) | ||
return .wait | ||
|
||
case .waitingForRead: | ||
// If we are `.waitingForRead`, no action needs to be taken. Demand has already been | ||
// signaled. Once we receive the next `read`, we will forward it, right away | ||
return .wait | ||
|
||
case .waitingForRows: | ||
// If we are `.waitingForRows`, no action needs to be taken. As soon as we receive | ||
// the next `channelReadComplete` we will forward all buffered data | ||
return .wait | ||
|
||
case .modifying: | ||
preconditionFailure("Invalid state: \(self.state)") | ||
} | ||
} | ||
|
||
mutating func read() -> Action { | ||
switch self.state { | ||
case .waitingForRows: | ||
// This should never happen. But we don't want to precondition this behavior. Let's just | ||
// pass the read event on | ||
return .read | ||
|
||
case .waitingForReadOrDemand(let buffer): | ||
self.state = .waitingForDemand(buffer) | ||
return .wait | ||
|
||
case .waitingForRead(let buffer): | ||
self.state = .waitingForRows(buffer) | ||
return .read | ||
|
||
case .waitingForDemand: | ||
// we have already received a read event. We will issue it as soon as we received demand | ||
// from the consumer | ||
return .wait | ||
|
||
case .modifying: | ||
preconditionFailure("Invalid state: \(self.state)") | ||
} | ||
} | ||
|
||
mutating func end() -> CircularBuffer<PSQLBackendMessage.DataRow> { | ||
switch self.state { | ||
case .waitingForRows(let buffer): | ||
return buffer | ||
|
||
case .waitingForReadOrDemand(let buffer), | ||
.waitingForRead(let buffer), | ||
.waitingForDemand(let buffer): | ||
|
||
// Normally this code path should never be hit. However there is one way to trigger | ||
// this: | ||
// | ||
// If the server decides to close a connection, NIO will forward all outstanding | ||
// `channelRead`s without waiting for a next `context.read` call. For this reason we might | ||
// receive a call to `end()`, when we don't expect it here. | ||
return buffer | ||
|
||
case .modifying: | ||
preconditionFailure("Invalid state: \(self.state)") | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Yeah, like this! This is better than my suggestion above, do it this way there too!