Correctly notify StreamMultiplexer of SETTINGS changes. (#67)

Motivation:

The StreamMultiplexer and StreamChannels are responsible for sending
WINDOW_UPDATE frames, but they were never told if the user changed the
value of SETTINGS_INITIAL_WINDOW_SIZE. They should be.

Modifications:

- Send user events when SETTINGS_INITIAL_WINDOW_SIZE changes.
- Use those window events to change the target window size in
    HTTP2StreamChannel.
- Keep track of the last stream window size we knew.

Result:

Less chance of flow control mess.
This commit is contained in:
Cory Benfield authored and GitHub committed 2019-03-15 17:37:00 +00:00
1 parent 1138881931
commit 55daa4cd4e
9 files changed
+155 -8

No files matched your search

@@ -37,6 +37,8 @@ extension HasLocalSettings {
swap(&temporarySettings, &self.localSettings)
}
var effect = NIOHTTP2ConnectionStateChange.LocalSettingsChanged()
do {
try temporarySettings.receiveSettingsAck { (setting, originalValue, newValue) in
switch setting {
@@ -56,6 +58,10 @@ extension HasLocalSettings {
try self.streamState.forAllStreams {
try $0.localInitialWindowSizeChanged(by: delta)
}
// We do a += here because the value may change multiple times in one settings block. This way, we correctly
// respect that possibility.
effect.streamWindowSizeChange += Int(delta)
case .maxFrameSize:
// TODO(cory): Implement!
break
@@ -64,7 +70,7 @@ extension HasLocalSettings {
return
}
}
return .init(result: .succeed, effect: nil)
return .init(result: .succeed, effect: .localSettingsChanged(effect))
} catch {
return .init(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
}
@@ -464,6 +464,10 @@ extension NIOHTTP2Handler {
if settingsChange.streamWindowSizeChange != 0 {
self.outboundBuffer.initialWindowSizeChanged(settingsChange.streamWindowSizeChange)
}
case .localSettingsChanged(let settingsChange):
if settingsChange.streamWindowSizeChange != 0 {
self.inboundEventBuffer.pendingUserEvent(NIOHTTP2BulkStreamWindowChangeEvent(delta: settingsChange.streamWindowSizeChange))
}
}
}
@@ -39,6 +39,9 @@ internal enum NIOHTTP2ConnectionStateChange: Hashable {
/// The remote peer's settings have been changed.
case remoteSettingsChanged(RemoteSettingsChanged)
/// The local peer's settings have been changed.
case localSettingsChanged(LocalSettingsChanged)
/// A stream has been created.
internal struct StreamCreated: Hashable {
internal var streamID: HTTP2StreamID
@@ -144,6 +147,14 @@ internal enum NIOHTTP2ConnectionStateChange: Hashable {
internal struct RemoteSettingsChanged: Hashable {
internal var streamWindowSizeChange: Int = 0
}
/// The local peer's settings have changed in a way that is not trivial to decode.
///
/// This object keeps track of the change on all stream window sizes via
/// SETTINGS frame.
internal struct LocalSettingsChanged: Hashable {
internal var streamWindowSizeChange: Int = 0
}
}
+10 -1
View File
@@ -231,7 +231,7 @@ final class HTTP2StreamChannel: Channel, ChannelCore {
private var state: StreamChannelState
private let windowManager: InboundWindowManager
private var windowManager: InboundWindowManager
/// If close0 was called but the stream could not synchronously close (because it's currently
/// active), the promise is stored here until it can be fulfilled.
@@ -512,5 +512,14 @@ internal extension HTTP2StreamChannel {
self.parent?.flush()
}
}
func initialWindowSizeChanged(delta: Int) {
if let increment = self.windowManager.initialWindowSizeChanged(delta: delta) {
let frame = HTTP2Frame(streamID: self.streamID, payload: .windowUpdate(windowSizeIncrement: increment))
self.receiveOutboundFrame(frame, promise: nil)
// This flush should really go away, but we need it for now until we sort out window management.
self.parent?.flush()
}
}
}
@@ -31,7 +31,7 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun
private let inboundStreamStateInitializer: ((Channel, HTTP2StreamID) -> EventLoopFuture<Void>)?
private let channel: Channel
private var nextOutboundStreamID: HTTP2StreamID
private let connectionFlowControlManager: InboundWindowManager
private var connectionFlowControlManager: InboundWindowManager
public func handlerAdded(context: ChannelHandlerContext) {
// We now need to check that we're on the same event loop as the one we were originally given.
@@ -61,7 +61,7 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun
let channel = HTTP2StreamChannel(allocator: self.channel.allocator,
parent: self.channel,
streamID: streamID,
targetWindowSize: 65535, // TODO: make configurable
targetWindowSize: 65535,
initiatedRemotely: true)
self.streams[streamID] = channel
channel.configure(initializer: self.inboundStreamStateInitializer)
@@ -106,6 +106,13 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun
if let channel = self.streams[evt.streamID], let windowSize = evt.inboundWindowSize {
channel.receiveWindowUpdatedEvent(windowSize)
}
case let evt as NIOHTTP2BulkStreamWindowChangeEvent:
// Here we need to pull the channels out so we aren't holding the streams dict mutably. This is because it
// will trigger an overlapping access violation if we do.
let channels = self.streams.values
for channel in channels {
channel.initialWindowSizeChanged(delta: evt.delta)
}
default:
break
}
+18 -3
View File
@@ -33,7 +33,7 @@ public struct StreamClosedEvent {
}
}
extension StreamClosedEvent: Equatable { }
extension StreamClosedEvent: Hashable { }
/// A `NIOHTTP2WindowUpdatedEvent` is fired whenever a flow control window is changed.
@@ -63,7 +63,7 @@ public struct NIOHTTP2WindowUpdatedEvent {
}
}
extension NIOHTTP2WindowUpdatedEvent: Equatable { }
extension NIOHTTP2WindowUpdatedEvent: Hashable { }
/// A `NIOHTTP2StreamCreatedEvent` is fired whenever a HTTP/2 stream is created.
@@ -83,4 +83,19 @@ public struct NIOHTTP2StreamCreatedEvent {
}
}
extension NIOHTTP2StreamCreatedEvent: Equatable { }
extension NIOHTTP2StreamCreatedEvent: Hashable { }
/// A `NIOHTTP2BulkStreamWindowChangeEvent` is fired whenever all of the remote flow control windows for a given stream have been changed.
///
/// This occurs when an ACK to a SETTINGS frame is received that changes the value of SETTINGS_INITIAL_WINDOW_SIZE. This is only fired
/// when the local peer has changed its settings.
public struct NIOHTTP2BulkStreamWindowChangeEvent {
/// The change in the remote stream window sizes.
public let delta: Int
public init(delta: Int) {
self.delta = delta
}
}
extension NIOHTTP2BulkStreamWindowChangeEvent: Hashable { }
+17 -1
View File
@@ -19,6 +19,9 @@
struct InboundWindowManager {
private var targetWindowSize: Int32
// The last window size we were told about. Used when we get changes to SETTINGS_INITIAL_WINDOW_SIZE.
private var lastWindowSize: Int?
init(targetSize: Int32) {
assert(targetSize <= HTTP2FlowControlWindow.maxSize)
assert(targetSize >= 0)
@@ -26,7 +29,9 @@ struct InboundWindowManager {
self.targetWindowSize = targetSize
}
func newWindowSize(_ newSize: Int) -> Int? {
mutating func newWindowSize(_ newSize: Int) -> Int? {
self.lastWindowSize = newSize
// All math here happens on 64-bit ints, as it avoids overflow problems.
// The simplest case is where newSize >= targetWindowSize. In that case, we do nothing.
@@ -51,4 +56,15 @@ struct InboundWindowManager {
return Int(increment)
}
}
mutating func initialWindowSizeChanged(delta: Int) -> Int? {
self.targetWindowSize += Int32(delta)
if let lastWindowSize = self.lastWindowSize {
// The delta applies to the current window size as well.
return self.newWindowSize(lastWindowSize + delta)
} else {
return nil
}
}
}