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:
Cory Benfield authored and GitHub committed 2020-08-13 15:01:52 +01:00
1 parent 7026b3bbea
commit d87661d4e8
4 files changed
+91 -45

No files matched your search

+22 -22
View File
@@ -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)
}