forked from apple/swift-nio-http2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exert backpressure via stream flow control windows (apple#191)
Motivation: When a window update event is received in the stream channel we notify the inbound window manager. Doing so may emit a WINDOW_UPDATE frame to indicate the remote may send us more DATA. However, this happens even if there are inbound frames waiting to be delivered to the channel. This may lead to the remote sending us more DATA which we have to buffer until it is read. Modifications: - Update the inbound window manager to track the number of bytes buffered in the stream channel - Account for buffered bytes when receiving a new window size event - Possibly emit WINDOW_UPDATE after unbuffering inbound frames into the stream channel's pipeline Result: The timing of WINDOW_UPDATE frames is better aligned with when frames are actually read in a stream.
- Loading branch information
Showing
6 changed files
with
189 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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 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
37 changes: 37 additions & 0 deletions
37
Tests/NIOHTTP2Tests/InboundWindowManagerTests+XCTest.swift
This file contains 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftNIO open source project | ||
// | ||
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// InboundWindowManagerTests+XCTest.swift | ||
// | ||
import XCTest | ||
|
||
/// | ||
/// NOTE: This file was generated by generate_linux_tests.rb | ||
/// | ||
/// Do NOT edit this file directly as it will be regenerated automatically when needed. | ||
/// | ||
|
||
extension InboundWindowManagerTests { | ||
|
||
static var allTests : [(String, (InboundWindowManagerTests) -> () throws -> Void)] { | ||
return [ | ||
("testNewWindowSizeWhenNewSizeIsAtOrAboveTarget", testNewWindowSizeWhenNewSizeIsAtOrAboveTarget), | ||
("testNewWindowSizeWhenNewSizeIsAtLeastHalfTarget", testNewWindowSizeWhenNewSizeIsAtLeastHalfTarget), | ||
("testNewWindowSizeWhenNewSizeIsLessThanOrEqualHalfTarget", testNewWindowSizeWhenNewSizeIsLessThanOrEqualHalfTarget), | ||
("testNewWindowSizeWithBufferedBytes", testNewWindowSizeWithBufferedBytes), | ||
("testInitialWindowSizeChanged", testInitialWindowSizeChanged), | ||
] | ||
} | ||
} | ||
|
This file contains 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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftNIO open source project | ||
// | ||
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@testable import NIOHTTP2 | ||
import XCTest | ||
|
||
class InboundWindowManagerTests: XCTestCase { | ||
|
||
func testNewWindowSizeWhenNewSizeIsAtOrAboveTarget() { | ||
var windowManager = InboundWindowManager(targetSize: 10) | ||
XCTAssertNil(windowManager.newWindowSize(10)) | ||
XCTAssertNil(windowManager.newWindowSize(11)) | ||
} | ||
|
||
func testNewWindowSizeWhenNewSizeIsAtLeastHalfTarget() { | ||
var windowManager = InboundWindowManager(targetSize: 10) | ||
XCTAssertNil(windowManager.newWindowSize(6)) | ||
XCTAssertNil(windowManager.newWindowSize(9)) | ||
} | ||
|
||
func testNewWindowSizeWhenNewSizeIsLessThanOrEqualHalfTarget() { | ||
var windowManager = InboundWindowManager(targetSize: 10) | ||
XCTAssertEqual(windowManager.newWindowSize(0), 10) | ||
XCTAssertEqual(windowManager.newWindowSize(5), 5) | ||
} | ||
|
||
func testNewWindowSizeWithBufferedBytes() { | ||
var windowManager = InboundWindowManager(targetSize: 10) | ||
|
||
// The adjusted newSize is 3 (new size 2, buffered 1) which is less than half the target so | ||
// we need to increment. | ||
windowManager.bufferedFrameReceived(size: 1) | ||
XCTAssertEqual(windowManager.newWindowSize(2), 7) | ||
|
||
// The adjusted newSize is 6 (new size 2, buffered 1+3=4) which is more than half the target | ||
// so no need to increment. | ||
windowManager.bufferedFrameReceived(size: 3) | ||
XCTAssertNil(windowManager.newWindowSize(2)) | ||
|
||
// The adjusted newSize is 10 (new size 2, buffered 4+4=8) which is equal to the target so | ||
// no need to increment. | ||
windowManager.bufferedFrameReceived(size: 4) | ||
XCTAssertNil(windowManager.newWindowSize(2)) | ||
|
||
// The last window size was 2; we're emitting 2 of our 8 buffered bytes so now the adjusted | ||
// size is 8 (last size 2, buffered 6); no need to increment. | ||
XCTAssertNil(windowManager.bufferedFrameEmitted(size: 2)) | ||
|
||
// Emit another 5 bytes: we're down to 3 (last size 2, buffered 1); we need to increment. | ||
XCTAssertEqual(windowManager.bufferedFrameEmitted(size: 5), 7) | ||
} | ||
|
||
func testInitialWindowSizeChanged() { | ||
var windowManager = InboundWindowManager(targetSize: 10) | ||
|
||
// There's no lastWindow size, so no increment. | ||
XCTAssertNil(windowManager.initialWindowSizeChanged(delta: 10)) | ||
|
||
windowManager.bufferedFrameReceived(size: 3) | ||
// adjusted size is 8 (new size 5, buffered 3) which is less than half the target (now 20). | ||
XCTAssertEqual(windowManager.newWindowSize(5), 12) | ||
} | ||
} |
This file contains 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