From c8f952dbc37fe60def17eb15e2c90787ce6ee78a Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Mon, 1 Jun 2020 09:29:36 +0100 Subject: [PATCH] Respect target window size on inbound channels. (#205) Motivation: As part of #202 we added support for configuring the target window size of stream channels. That was plumbed through into outbound channels, but we missed the inbound ones, meaning that this only really worked for clients, not servers. That hardly seems fair! Modifications: Pass target window size to inbound channels too. Result: We can control target window size of inbound channels. --- Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift | 2 +- .../HTTP2StreamMultiplexerTests+XCTest.swift | 1 + .../HTTP2StreamMultiplexerTests.swift | 34 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift b/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift index 62799cb..0c9bca9 100644 --- a/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift +++ b/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift @@ -79,7 +79,7 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun parent: self.channel, multiplexer: self, streamID: streamID, - targetWindowSize: 65535, + targetWindowSize: Int32(self.targetWindowSize), outboundBytesHighWatermark: self.streamChannelOutboundBytesHighWatermark, outboundBytesLowWatermark: self.streamChannelOutboundBytesLowWatermark) self.streams[streamID] = channel diff --git a/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests+XCTest.swift b/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests+XCTest.swift index 4262f47..36fe30b 100644 --- a/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests+XCTest.swift +++ b/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests+XCTest.swift @@ -71,6 +71,7 @@ extension HTTP2StreamMultiplexerTests { ("testMultiplexerModifiesStreamChannelWritabilityBasedOnParentChannelWritability", testMultiplexerModifiesStreamChannelWritabilityBasedOnParentChannelWritability), ("testMultiplexerModifiesStreamChannelWritabilityBasedOnFixedSizeTokensAndChannelWritability", testMultiplexerModifiesStreamChannelWritabilityBasedOnFixedSizeTokensAndChannelWritability), ("testStreamChannelToleratesFailingInitialier", testStreamChannelToleratesFailingInitialier), + ("testInboundChannelWindowSizeIsCustomisable", testInboundChannelWindowSizeIsCustomisable), ] } } diff --git a/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests.swift b/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests.swift index 8667513..a15f976 100644 --- a/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests.swift +++ b/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests.swift @@ -1794,4 +1794,38 @@ final class HTTP2StreamMultiplexerTests: XCTestCase { self.channel.embeddedEventLoop.run() } + func testInboundChannelWindowSizeIsCustomisable() throws { + let targetWindowSize = 1 << 18 + + let multiplexer = HTTP2StreamMultiplexer(mode: .client, + channel: self.channel, + targetWindowSize: targetWindowSize) { (channel, streamID) in + return channel.eventLoop.makeSucceededFuture(()) + } + + XCTAssertNoThrow(try self.channel.pipeline.addHandler(multiplexer).wait()) + XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "1.2.3.4", port: 5)).wait()) + + // Ok, create an inbound channel. + let frame = HTTP2Frame(streamID: HTTP2StreamID(1), payload: .headers(.init(headers: HPACKHeaders()))) + XCTAssertNoThrow(try self.channel.writeInbound(frame)) + + // Now claim that the window got consumed. Nothing happens. + XCTAssertNoThrow(try XCTAssertNil(self.channel.readOutbound(as: HTTP2Frame.self))) + var windowEvent = NIOHTTP2WindowUpdatedEvent(streamID: 1, inboundWindowSize: (targetWindowSize / 2) + 1, outboundWindowSize: nil) + channel.pipeline.fireUserInboundEventTriggered(windowEvent) + XCTAssertNoThrow(try XCTAssertNil(self.channel.readOutbound(as: HTTP2Frame.self))) + + // Consume the last byte. + windowEvent = NIOHTTP2WindowUpdatedEvent(streamID: 1, inboundWindowSize: (targetWindowSize / 2), outboundWindowSize: nil) + channel.pipeline.fireUserInboundEventTriggered(windowEvent) + guard let receivedFrame = try assertNoThrowWithValue(self.channel.readOutbound(as: HTTP2Frame.self)) else { + XCTFail("No frame received") + return + } + + receivedFrame.assertWindowUpdateFrame(streamID: 1, windowIncrement: targetWindowSize / 2) + XCTAssertNoThrow(try channel.finish(acceptAlreadyClosed: false)) + } + }