Files
swift-nio-http2/Sources/NIOHTTP2/ConnectionStateMachine/HasLocalSettings.swift
T
Cory Benfield 55daa4cd4e 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.
2019-03-15 17:37:00 +00:00

79 lines
3.4 KiB
Swift

//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
/// A protocol implemented by HTTP/2 connection state machine states with local settings.
///
/// This protocol provides implementations that can apply changes to the local settings.
protocol HasLocalSettings {
var role: HTTP2ConnectionStateMachine.ConnectionRole { get }
var localSettings: HTTP2SettingsState { get set }
var streamState: ConnectionStreamState { get set }
var inboundFlowControlWindow: HTTP2FlowControlWindow { get set }
}
extension HasLocalSettings {
mutating func receiveSettingsAck(frameEncoder: inout HTTP2FrameEncoder) -> StateMachineResultWithEffect {
// We do a little switcheroo here to avoid problems with overlapping accesses to
// self. It's a little more complex than normal because HTTP2SettingsState has
// two CoWable objects, and we don't want to CoW either of them, so we shove a dummy
// value in `self` to avoid that.
var temporarySettings = HTTP2SettingsState.dummyValue()
swap(&temporarySettings, &self.localSettings)
defer {
swap(&temporarySettings, &self.localSettings)
}
var effect = NIOHTTP2ConnectionStateChange.LocalSettingsChanged()
do {
try temporarySettings.receiveSettingsAck { (setting, originalValue, newValue) in
switch setting {
case .maxConcurrentStreams:
if self.role == .client {
self.streamState.maxServerInitiatedStreams = newValue
} else {
self.streamState.maxClientInitiatedStreams = newValue
}
case .headerTableSize:
frameEncoder.headerEncoder.maximumDynamicTableSize = Int(newValue)
case .initialWindowSize:
// We default the value of SETTINGS_INITIAL_WINDOW_SIZE, so originalValue mustn't be nil.
// The max value of SETTINGS_INITIAL_WINDOW_SIZE is Int32.max, so we can safely fit it into that here.
let delta = Int32(newValue) - Int32(originalValue!)
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
default:
// No operation required
return
}
}
return .init(result: .succeed, effect: .localSettingsChanged(effect))
} catch {
return .init(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
}
}
}