-
Notifications
You must be signed in to change notification settings - Fork 125
Call didSendRequestPart
at the right time
#566
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
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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
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 |
---|---|---|
|
@@ -28,30 +28,48 @@ struct HTTP1ConnectionStateMachine { | |
|
||
enum Action { | ||
/// A action to execute, when we consider a request "done". | ||
enum FinalStreamAction { | ||
enum FinalSuccessfulStreamAction { | ||
/// Close the connection | ||
case close | ||
/// If the server has replied, with a status of 200...300 before all data was sent, a request is considered succeeded, | ||
/// as soon as we wrote the request end onto the wire. | ||
case sendRequestEnd | ||
/// | ||
/// The promise is an optional write promise. | ||
case sendRequestEnd(EventLoopPromise<Void>?) | ||
/// Inform an observer that the connection has become idle | ||
case informConnectionIsIdle | ||
} | ||
|
||
/// A action to execute, when we consider a request "done". | ||
enum FinalFailedStreamAction { | ||
/// Close the connection | ||
/// | ||
/// The promise is an optional write promise. | ||
case close(EventLoopPromise<Void>?) | ||
/// Inform an observer that the connection has become idle | ||
/// | ||
/// The promise is an optional write promise. | ||
case informConnectionIsIdle(EventLoopPromise<Void>?) | ||
/// Fail the write promise | ||
case failWritePromise(EventLoopPromise<Void>?) | ||
/// Do nothing. | ||
case none | ||
} | ||
|
||
case sendRequestHead(HTTPRequestHead, startBody: Bool) | ||
case sendBodyPart(IOData) | ||
case sendRequestEnd | ||
case sendBodyPart(IOData, EventLoopPromise<Void>?) | ||
case sendRequestEnd(EventLoopPromise<Void>?) | ||
case failSendBodyPart(Error, EventLoopPromise<Void>?) | ||
case failSendStreamFinished(Error, EventLoopPromise<Void>?) | ||
|
||
case pauseRequestBodyStream | ||
case resumeRequestBodyStream | ||
|
||
case forwardResponseHead(HTTPResponseHead, pauseRequestBodyStream: Bool) | ||
case forwardResponseBodyParts(CircularBuffer<ByteBuffer>) | ||
|
||
case failRequest(Error, FinalStreamAction) | ||
FranzBusch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case succeedRequest(FinalStreamAction, CircularBuffer<ByteBuffer>) | ||
FranzBusch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case failRequest(Error, FinalFailedStreamAction) | ||
case succeedRequest(FinalSuccessfulStreamAction, CircularBuffer<ByteBuffer>) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not an issue introduced by you. Urgh. I think we should change the order here (first buffer, than last action). Maybe as a follow up. |
||
|
||
case read | ||
case close | ||
|
@@ -173,7 +191,7 @@ struct HTTP1ConnectionStateMachine { | |
// as closed. | ||
// | ||
// TODO: AHC should support a fast rescheduling mechanism here. | ||
return .failRequest(HTTPClientError.remoteConnectionClosed, .none) | ||
return .failRequest(HTTPClientError.remoteConnectionClosed, .failWritePromise(nil)) | ||
FranzBusch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
case .idle: | ||
var requestStateMachine = HTTPRequestStateMachine(isChannelWritable: self.isChannelWritable) | ||
|
@@ -189,25 +207,25 @@ struct HTTP1ConnectionStateMachine { | |
} | ||
} | ||
|
||
mutating func requestStreamPartReceived(_ part: IOData) -> Action { | ||
mutating func requestStreamPartReceived(_ part: IOData, promise: EventLoopPromise<Void>?) -> Action { | ||
guard case .inRequest(var requestStateMachine, let close) = self.state else { | ||
preconditionFailure("Invalid state: \(self.state)") | ||
} | ||
|
||
return self.avoidingStateMachineCoW { state -> Action in | ||
let action = requestStateMachine.requestStreamPartReceived(part) | ||
let action = requestStateMachine.requestStreamPartReceived(part, promise: promise) | ||
state = .inRequest(requestStateMachine, close: close) | ||
return state.modify(with: action) | ||
} | ||
} | ||
|
||
mutating func requestStreamFinished() -> Action { | ||
mutating func requestStreamFinished(promise: EventLoopPromise<Void>?) -> Action { | ||
guard case .inRequest(var requestStateMachine, let close) = self.state else { | ||
preconditionFailure("Invalid state: \(self.state)") | ||
} | ||
|
||
return self.avoidingStateMachineCoW { state -> Action in | ||
let action = requestStateMachine.requestStreamFinished() | ||
let action = requestStateMachine.requestStreamFinished(promise: promise) | ||
state = .inRequest(requestStateMachine, close: close) | ||
return state.modify(with: action) | ||
} | ||
|
@@ -377,10 +395,10 @@ extension HTTP1ConnectionStateMachine.State { | |
return .pauseRequestBodyStream | ||
case .resumeRequestBodyStream: | ||
return .resumeRequestBodyStream | ||
case .sendBodyPart(let part): | ||
return .sendBodyPart(part) | ||
case .sendRequestEnd: | ||
return .sendRequestEnd | ||
case .sendBodyPart(let part, let writePromise): | ||
return .sendBodyPart(part, writePromise) | ||
case .sendRequestEnd(let writePromise): | ||
return .sendRequestEnd(writePromise) | ||
case .forwardResponseHead(let head, let pauseRequestBodyStream): | ||
return .forwardResponseHead(head, pauseRequestBodyStream: pauseRequestBodyStream) | ||
case .forwardResponseBodyParts(let parts): | ||
|
@@ -390,41 +408,57 @@ extension HTTP1ConnectionStateMachine.State { | |
preconditionFailure("Invalid state: \(self)") | ||
} | ||
|
||
let newFinalAction: HTTP1ConnectionStateMachine.Action.FinalStreamAction | ||
let newFinalAction: HTTP1ConnectionStateMachine.Action.FinalSuccessfulStreamAction | ||
switch finalAction { | ||
case .close: | ||
self = .closing | ||
newFinalAction = .close | ||
case .sendRequestEnd: | ||
newFinalAction = .sendRequestEnd | ||
case .sendRequestEnd(let writePromise): | ||
newFinalAction = .sendRequestEnd(writePromise) | ||
case .none: | ||
self = .idle | ||
newFinalAction = close ? .close : .informConnectionIsIdle | ||
} | ||
return .succeedRequest(newFinalAction, finalParts) | ||
|
||
case .failRequest(let error, let finalAction): | ||
switch self { | ||
case .initialized: | ||
switch (self, finalAction) { | ||
case (.initialized, _): | ||
preconditionFailure("Invalid state: \(self)") | ||
case .idle: | ||
|
||
case (.idle, _): | ||
preconditionFailure("How can we fail a task, if we are idle") | ||
case .inRequest(_, close: let close): | ||
if close || finalAction == .close { | ||
self = .closing | ||
return .failRequest(error, .close) | ||
} else { | ||
self = .idle | ||
return .failRequest(error, .informConnectionIsIdle) | ||
} | ||
|
||
case .closing: | ||
// If we are either in .inRequest(_, close: true) or the final action is .close | ||
// we have to fail the request with .close() | ||
case (.inRequest(_, let close), .none) where close: | ||
self = .closing | ||
return .failRequest(error, .close(nil)) | ||
|
||
case (.inRequest(_, _), .close(let writePromise)): | ||
self = .closing | ||
return .failRequest(error, .close(writePromise)) | ||
|
||
// otherwise we fail with .informConnectionIsIdle | ||
case (.inRequest(_, _), .none): | ||
self = .idle | ||
return .failRequest(error, .informConnectionIsIdle(nil)) | ||
FranzBusch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
case (.closing, .close(let writePromise)): | ||
return .failRequest(error, .failWritePromise(writePromise)) | ||
|
||
case (.closing, .none): | ||
return .failRequest(error, .none) | ||
case .closed: | ||
|
||
case (.closed, .close(let writePromise)): | ||
// this state can be reached, if the connection was unexpectedly closed by remote | ||
return .failRequest(error, .failWritePromise(writePromise)) | ||
|
||
case (.closed, .none): | ||
// this state can be reached, if the connection was unexpectedly closed by remote | ||
return .failRequest(error, .none) | ||
|
||
case .modifying: | ||
case (.modifying, _): | ||
preconditionFailure("Invalid state: \(self)") | ||
} | ||
|
||
|
@@ -433,6 +467,12 @@ extension HTTP1ConnectionStateMachine.State { | |
|
||
case .wait: | ||
return .wait | ||
|
||
case .failSendBodyPart(let error, let writePromise): | ||
return .failSendBodyPart(error, writePromise) | ||
|
||
case .failSendStreamFinished(let error, let writePromise): | ||
return .failSendStreamFinished(error, writePromise) | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.