mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-25 04:47:50 +00:00
Propagate read() through the pipeline. (#231)
Motivation: NIO's backpressure management systems rely on catching calls to read() in the ChannelPipeline. This only works if your Channel implementation actually makes those calls to read()! Sadly, HTTP2StreamChannel did not: we instead just recursed directly into the Channel's read0(). While I'm here I also noticed that we produced a lot of channelReadComplete messages in the pipeline unnecessarily. We did this in the service of having a read "fast-path" where new frames would be automatically delivered into the ChannelPipeline without being buffered until channelReadComplete. This fast-path is probably inadvisable: I don't think it provided a meaningful performance benefit anyway, especially as the removal of that fast-path allowed us to suppress unnecessary calls to channelReadComplete, and therefore unnecessary calls to read() as well. Modifications: - Forced autoRead through the pipeline. - Removed read() fast-path. - Suppressed channelReadComplete if no frames were delivered. - Updated tests. Result: Clearer I/O path, better auto-read handling.
This commit is contained in:
1 parent
7026b3bbea
commit
d87661d4e8
4 files changed
+91
-45
No files matched your search
@@ -237,9 +237,7 @@ final class HTTP2StreamChannel<Message: HTTP2FramePayloadConvertible & HTTP2Fram
|
||||
|
||||
self.modifyingState { $0.activate() }
|
||||
self.pipeline.fireChannelActive()
|
||||
if self.autoRead {
|
||||
self.read0()
|
||||
}
|
||||
self.tryToAutoRead()
|
||||
self.deliverPendingWrites()
|
||||
}
|
||||
|
||||
@@ -592,22 +590,30 @@ final class HTTP2StreamChannel<Message: HTTP2FramePayloadConvertible & HTTP2Fram
|
||||
return
|
||||
}
|
||||
|
||||
// If there are no pending reads, do nothing.
|
||||
guard self.pendingReads.count > 0 else {
|
||||
return
|
||||
}
|
||||
|
||||
// Ok, we're satisfying a read here.
|
||||
self.unsatisfiedRead = false
|
||||
self.deliverPendingReads()
|
||||
|
||||
// If auto-read is turned on, recurse into read0.
|
||||
// This cannot recurse indefinitely unless frames are being delivered
|
||||
// by the read stacks, which is generally fairly unlikely to continue unbounded.
|
||||
if self.autoRead {
|
||||
self.read0()
|
||||
}
|
||||
self.tryToAutoRead()
|
||||
}
|
||||
|
||||
private func changeWritability(to newWritability: Bool) {
|
||||
self._isWritable.store(newWritability)
|
||||
self.pipeline.fireChannelWritabilityChanged()
|
||||
}
|
||||
|
||||
private func tryToAutoRead() {
|
||||
if self.autoRead {
|
||||
// If auto-read is turned on, recurse into channelPipeline.read().
|
||||
// This cannot recurse indefinitely unless frames are being delivered
|
||||
// by the read stacks, which is generally fairly unlikely to continue unbounded.
|
||||
self.pipeline.read()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK:- Functions used to manage pending reads and writes.
|
||||
@@ -688,19 +694,13 @@ internal extension HTTP2StreamChannel {
|
||||
|
||||
let message = Message(http2Frame: frame)
|
||||
|
||||
if self.unsatisfiedRead {
|
||||
// We don't need to account for this frame in the window manager: it's being delivered
|
||||
// straight into the pipeline.
|
||||
self.pipeline.fireChannelRead(NIOAny(message))
|
||||
} else {
|
||||
// Record the size of the frame so that when we receive a window update event our
|
||||
// calculation on whether we emit a WINDOW_UPDATE frame is based on the bytes we have
|
||||
// actually delivered into the pipeline.
|
||||
if case .data(let dataPayload) = frame.payload {
|
||||
self.windowManager.bufferedFrameReceived(size: dataPayload.data.readableBytes)
|
||||
}
|
||||
self.pendingReads.append(message)
|
||||
// Record the size of the frame so that when we receive a window update event our
|
||||
// calculation on whether we emit a WINDOW_UPDATE frame is based on the bytes we have
|
||||
// actually delivered into the pipeline.
|
||||
if case .data(let dataPayload) = frame.payload {
|
||||
self.windowManager.bufferedFrameReceived(size: dataPayload.data.readableBytes)
|
||||
}
|
||||
self.pendingReads.append(message)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -74,6 +74,7 @@ extension HTTP2FramePayloadStreamMultiplexerTests {
|
||||
("testStreamChannelToleratesFailingInitializer", testStreamChannelToleratesFailingInitializer),
|
||||
("testInboundChannelWindowSizeIsCustomisable", testInboundChannelWindowSizeIsCustomisable),
|
||||
("testWeCanCreateFrameAndPayloadBasedStreamsOnAMultiplexer", testWeCanCreateFrameAndPayloadBasedStreamsOnAMultiplexer),
|
||||
("testReadWhenUsingAutoreadOnChildChannel", testReadWhenUsingAutoreadOnChildChannel),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1412,7 +1412,7 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase {
|
||||
self.channel.embeddedEventLoop.run()
|
||||
|
||||
XCTAssertEqual(frameRecorder.receivedFrames.count, 1)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 0)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 1)
|
||||
|
||||
// Now we're going to send 9 data frames.
|
||||
var requestData = self.channel.allocator.buffer(capacity: 1024)
|
||||
@@ -1423,21 +1423,21 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase {
|
||||
self.channel.pipeline.fireChannelRead(NIOAny(frame))
|
||||
}
|
||||
|
||||
// We should have 10 reads, and zero read completes.
|
||||
XCTAssertEqual(frameRecorder.receivedFrames.count, 10)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 0)
|
||||
// We should have 1 read (the HEADERS), and one read complete.
|
||||
XCTAssertEqual(frameRecorder.receivedFrames.count, 1)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 1)
|
||||
|
||||
// Fire read complete on the parent and it'll propagate to the child.
|
||||
// Fire read complete on the parent and it'll propagate to the child. This will trigger the reads.
|
||||
self.channel.pipeline.fireChannelReadComplete()
|
||||
|
||||
// We should have 10 reads, and one read complete.
|
||||
// We should have 10 reads, and two read completes.
|
||||
XCTAssertEqual(frameRecorder.receivedFrames.count, 10)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 1)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 2)
|
||||
|
||||
// If we fire a new read complete on the parent, the child doesn't see it this time, as it received no frames.
|
||||
self.channel.pipeline.fireChannelReadComplete()
|
||||
XCTAssertEqual(frameRecorder.receivedFrames.count, 10)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 1)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 2)
|
||||
}
|
||||
|
||||
func testMultiplexerCorrectlyTellsAllStreamsAboutReadComplete() {
|
||||
@@ -1470,9 +1470,9 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase {
|
||||
self.channel.embeddedEventLoop.run()
|
||||
|
||||
XCTAssertEqual(frameRecorder.receivedFrames.count, 3)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 0)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 3)
|
||||
|
||||
// Firing in readComplete causes a readComplete for each stream.
|
||||
// Firing in readComplete does not cause a second readComplete for each stream, as no frames were delivered.
|
||||
self.channel.pipeline.fireChannelReadComplete()
|
||||
XCTAssertEqual(frameRecorder.receivedFrames.count, 3)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 3)
|
||||
@@ -1483,8 +1483,8 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase {
|
||||
let frame = HTTP2Frame(streamID: 1, payload: .data(.init(data: .byteBuffer(requestData), endStream: false)))
|
||||
self.channel.pipeline.fireChannelRead(NIOAny(frame))
|
||||
|
||||
// We should have 4 reads, and 3 read completes.
|
||||
XCTAssertEqual(frameRecorder.receivedFrames.count, 4)
|
||||
// We should have 3 reads, and 3 read completes. The frame is not delivered as we have no frame fast-path.
|
||||
XCTAssertEqual(frameRecorder.receivedFrames.count, 3)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 3)
|
||||
|
||||
// Fire read complete on the parent and it'll propagate to the child, but only to the one
|
||||
@@ -1762,4 +1762,47 @@ final class HTTP2FramePayloadStreamMultiplexerTests: XCTestCase {
|
||||
frameRecorder.flushedWrites.assertFramesMatch([HTTP2Frame(streamID: 1, payload: headersPayload),
|
||||
HTTP2Frame(streamID: 3, payload: headersPayload)])
|
||||
}
|
||||
|
||||
func testReadWhenUsingAutoreadOnChildChannel() throws {
|
||||
var childChannel: Channel? = nil
|
||||
let readCounter = ReadCounter()
|
||||
let multiplexer = HTTP2StreamMultiplexer(mode: .server, channel: self.channel) { channel -> EventLoopFuture<Void> in
|
||||
childChannel = channel
|
||||
|
||||
// We're going to _enable_ autoRead on this channel.
|
||||
return channel.setOption(ChannelOptions.autoRead, value: true).flatMap {
|
||||
channel.pipeline.addHandler(readCounter)
|
||||
}
|
||||
}
|
||||
XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait())
|
||||
|
||||
XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(unixDomainSocketPath: "/whatever"), promise: nil))
|
||||
|
||||
// Let's open a stream.
|
||||
let streamID = HTTP2StreamID(1)
|
||||
let frame = HTTP2Frame(streamID: streamID, payload: .headers(.init(headers: HPACKHeaders())))
|
||||
XCTAssertNoThrow(try self.channel.writeInbound(frame))
|
||||
self.activateStream(streamID)
|
||||
XCTAssertNotNil(childChannel)
|
||||
|
||||
// There should be two calls to read: the first, when the stream was activated, the second after the HEADERS
|
||||
// frame was delivered.
|
||||
XCTAssertEqual(readCounter.readCount, 2)
|
||||
|
||||
// Now deliver a data frame.
|
||||
var buffer = self.channel.allocator.buffer(capacity: 12)
|
||||
buffer.writeStaticString("Hello, world!")
|
||||
let dataFrame = HTTP2Frame(streamID: streamID, payload: .data(.init(data: .byteBuffer(buffer))))
|
||||
XCTAssertNoThrow(try self.channel.writeInbound(dataFrame))
|
||||
|
||||
// This frame should have been immediately delivered, _and_ a call to read should have happened.
|
||||
XCTAssertEqual(readCounter.readCount, 3)
|
||||
|
||||
// Delivering two more frames causes two more calls to read.
|
||||
XCTAssertNoThrow(try self.channel.writeInbound(dataFrame))
|
||||
XCTAssertNoThrow(try self.channel.writeInbound(dataFrame))
|
||||
XCTAssertEqual(readCounter.readCount, 5)
|
||||
|
||||
XCTAssertNoThrow(try self.channel.finish())
|
||||
}
|
||||
}
|
||||
@@ -112,6 +112,7 @@ final class ReadCounter: ChannelOutboundHandler {
|
||||
|
||||
func read(context: ChannelHandlerContext) {
|
||||
readCount += 1
|
||||
context.read()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1596,7 +1597,7 @@ final class HTTP2StreamMultiplexerTests: XCTestCase {
|
||||
self.channel.embeddedEventLoop.run()
|
||||
|
||||
XCTAssertEqual(frameRecorder.receivedFrames.count, 1)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 0)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 1)
|
||||
|
||||
// Now we're going to send 9 data frames.
|
||||
var requestData = self.channel.allocator.buffer(capacity: 1024)
|
||||
@@ -1607,21 +1608,21 @@ final class HTTP2StreamMultiplexerTests: XCTestCase {
|
||||
self.channel.pipeline.fireChannelRead(NIOAny(frame))
|
||||
}
|
||||
|
||||
// We should have 10 reads, and zero read completes.
|
||||
XCTAssertEqual(frameRecorder.receivedFrames.count, 10)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 0)
|
||||
// We should have 1 read (the HEADERS), and zero read completes.
|
||||
XCTAssertEqual(frameRecorder.receivedFrames.count, 1)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 1)
|
||||
|
||||
// Fire read complete on the parent and it'll propagate to the child.
|
||||
// Fire read complete on the parent and it'll propagate to the child, also firing the reads..
|
||||
self.channel.pipeline.fireChannelReadComplete()
|
||||
|
||||
// We should have 10 reads, and one read complete.
|
||||
XCTAssertEqual(frameRecorder.receivedFrames.count, 10)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 1)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 2)
|
||||
|
||||
// If we fire a new read complete on the parent, the child doesn't see it this time, as it received no frames.
|
||||
self.channel.pipeline.fireChannelReadComplete()
|
||||
XCTAssertEqual(frameRecorder.receivedFrames.count, 10)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 1)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 2)
|
||||
}
|
||||
|
||||
@available(*, deprecated, message: "Deprecated so deprecated functionality can be tested without warnings")
|
||||
@@ -1655,9 +1656,9 @@ final class HTTP2StreamMultiplexerTests: XCTestCase {
|
||||
self.channel.embeddedEventLoop.run()
|
||||
|
||||
XCTAssertEqual(frameRecorder.receivedFrames.count, 3)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 0)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 3)
|
||||
|
||||
// Firing in readComplete causes a readComplete for each stream.
|
||||
// Firing in readComplete does not cause a readComplete, as no new frames were delivered.
|
||||
self.channel.pipeline.fireChannelReadComplete()
|
||||
XCTAssertEqual(frameRecorder.receivedFrames.count, 3)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 3)
|
||||
@@ -1668,8 +1669,9 @@ final class HTTP2StreamMultiplexerTests: XCTestCase {
|
||||
let frame = HTTP2Frame(streamID: 1, payload: .data(.init(data: .byteBuffer(requestData), endStream: false)))
|
||||
self.channel.pipeline.fireChannelRead(NIOAny(frame))
|
||||
|
||||
// We should have 4 reads, and 3 read completes.
|
||||
XCTAssertEqual(frameRecorder.receivedFrames.count, 4)
|
||||
// We should have 3 reads, and 3 read completes. We don't actually get this frame yet, because
|
||||
// we do not do a "fast-delivery" path.
|
||||
XCTAssertEqual(frameRecorder.receivedFrames.count, 3)
|
||||
XCTAssertEqual(readCompleteCounter.readCompleteCount, 3)
|
||||
|
||||
// Fire read complete on the parent and it'll propagate to the child, but only to the one
|
||||
|
||||
Reference in new issue
Block a user