Skip to content

Commit 5c7899a

Browse files
committed
Code review
1 parent 28c237d commit 5c7899a

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

Diff for: Sources/AsyncHTTPClient/ConnectionPool/HTTP2/HTTP2ClientRequestHandler.swift

+8-5
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class HTTP2ClientRequestHandler: ChannelDuplexHandler {
2424

2525
private let eventLoop: EventLoop
2626

27-
private(set) var channelContext: ChannelHandlerContext?
28-
private(set) var state: HTTPRequestStateMachine = .init(isChannelWritable: false)
29-
private(set) var request: HTTPExecutingRequest?
27+
private var channelContext: ChannelHandlerContext?
28+
private var state: HTTPRequestStateMachine = .init(isChannelWritable: false)
29+
private var request: HTTPExecutingRequest?
3030

3131
init(eventLoop: EventLoop) {
3232
self.eventLoop = eventLoop
@@ -96,10 +96,13 @@ class HTTP2ClientRequestHandler: ChannelDuplexHandler {
9696
case .sendRequestHead(let head, let startBody):
9797
if startBody {
9898
context.writeAndFlush(self.wrapOutboundOut(.head(head)), promise: nil)
99+
self.request!.requestHeadSent()
99100
self.request!.resumeRequestBodyStream()
100101
} else {
101-
context.writeAndFlush(self.wrapOutboundOut(.head(head)), promise: nil)
102-
context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
102+
context.write(self.wrapOutboundOut(.head(head)), promise: nil)
103+
context.write(self.wrapOutboundOut(.end(nil)), promise: nil)
104+
context.flush()
105+
self.request!.requestHeadSent()
103106
}
104107

105108
case .pauseRequestBodyStream:

Diff for: Sources/AsyncHTTPClient/ConnectionPool/HTTP2/HTTP2IdleHandler.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ extension HTTP2IdleHandler {
120120
preconditionFailure("Invalid state")
121121
}
122122

123-
let maxStream = settings.first(where: { $0.parameter == .maxConcurrentStreams })?.value ?? 100
123+
let maxStream = settings.last(where: { $0.parameter == .maxConcurrentStreams })?.value ?? 100
124124

125125
self.state = .active(openStreams: 0, maxStreams: maxStream)
126126
return .notifyConnectionNewSettings(settings)
@@ -137,7 +137,7 @@ extension HTTP2IdleHandler {
137137
self.state = .goAwayReceived(openStreams: openStreams, maxStreams: maxStreams)
138138
return .notifyConnectionGoAwayReceived
139139
case .goAwayReceived:
140-
preconditionFailure("Invalid state")
140+
return .nothing
141141
case .closed:
142142
preconditionFailure("Invalid state")
143143
}

Diff for: Tests/AsyncHTTPClientTests/HTTPRequestStateMachineTests+XCTest.swift

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ extension HTTPRequestStateMachineTests {
4242
("testResponseReadingWithBackpressureEndOfResponseAllowsReadEventsToTriggerDirectly", testResponseReadingWithBackpressureEndOfResponseAllowsReadEventsToTriggerDirectly),
4343
("testCancellingARequestInStateInitializedKeepsTheConnectionAlive", testCancellingARequestInStateInitializedKeepsTheConnectionAlive),
4444
("testCancellingARequestBeforeBeingSendKeepsTheConnectionAlive", testCancellingARequestBeforeBeingSendKeepsTheConnectionAlive),
45+
("testConnectionBecomesWritableBeforeFirstRequest", testConnectionBecomesWritableBeforeFirstRequest),
4546
("testCancellingARequestThatIsSent", testCancellingARequestThatIsSent),
4647
("testRemoteSuddenlyClosesTheConnection", testRemoteSuddenlyClosesTheConnection),
4748
("testReadTimeoutLeadsToFailureWithEverythingAfterBeingIgnored", testReadTimeoutLeadsToFailureWithEverythingAfterBeingIgnored),

Diff for: Tests/AsyncHTTPClientTests/HTTPRequestStateMachineTests.swift

+18
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,24 @@ class HTTPRequestStateMachineTests: XCTestCase {
350350
XCTAssertEqual(state.requestCancelled(), .failRequest(HTTPClientError.cancelled, .none))
351351
}
352352

353+
func testConnectionBecomesWritableBeforeFirstRequest() {
354+
var state = HTTPRequestStateMachine(isChannelWritable: false)
355+
XCTAssertEqual(state.writabilityChanged(writable: true), .wait)
356+
357+
// --- sending request
358+
let requestHead = HTTPRequestHead(version: .http1_1, method: .GET, uri: "/")
359+
let metadata = RequestFramingMetadata(connectionClose: false, body: .none)
360+
XCTAssertEqual(state.startRequest(head: requestHead, metadata: metadata), .sendRequestHead(requestHead, startBody: false))
361+
362+
// --- receiving response
363+
let responseHead = HTTPResponseHead(version: .http1_1, status: .ok, headers: ["content-length": "4"])
364+
XCTAssertEqual(state.channelRead(.head(responseHead)), .forwardResponseHead(responseHead, pauseRequestBodyStream: false))
365+
let responseBody = ByteBuffer(bytes: [1, 2, 3, 4])
366+
XCTAssertEqual(state.channelRead(.body(responseBody)), .wait)
367+
XCTAssertEqual(state.channelRead(.end(nil)), .succeedRequest(.none, .init([responseBody])))
368+
XCTAssertEqual(state.channelReadComplete(), .wait)
369+
}
370+
353371
func testCancellingARequestThatIsSent() {
354372
var state = HTTPRequestStateMachine(isChannelWritable: true)
355373
let requestHead = HTTPRequestHead(version: .http1_1, method: .GET, uri: "/")

0 commit comments

Comments
 (0)