mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-26 04:57:50 +00:00
Motivation: When working with HTTP/2 we frequently have a need to store "per-stream" state. This is state that is independently reproduced once per stream. We often need to look up this state by stream ID, and so we are motivated to store this state as efficiently as possible. Naively, Dictionary seems like a useful data type for this. However, Dictionaries are not free. The cost of hashing in order to index into a Dictionary can be substantial. This is both because the hashing function itself is branchy and complex, but also because the dictionary access is very unfriendly to the branch predictor and the memory caching systems. We can take advantage of the fact that if you split streams into client and server namespaces, stream IDs are strictly ordered. That is, streams are created in order of increasing stream ID. While streams are _retired_ out of order, at no point will a CircularBuffer of stream IDs ever be unordered. This unlocks a very powerful data structure for us: the ordered "Array" (in our case actually CircularBuffer). An ordered Array supports two powerful lookup tools. Firstly, it supports the mighty binary search. This means the absolute worst-case searching performance for an ordered Array is log(n) in the size of the Array. Finding a stream by stream ID in an Array of one million streams would take around 20 steps: extremely cheap. For smaller sizes, however, we unlock the one true searching function: linear scan. On modern CPUs, performing simple computations (such as checking if one number equals another) is orders of magnitude faster than looking data up in memory. More importantly, modern CPUs can often effectively compute multiple lookups at once thanks to their out-of-order superscalar pipelines. As a result, on any modern CPU (including all mobile CPUs) a linear search outperforms almost any other searching algorithm for surprisingly large Array sizes! Given that stream IDs are intrinsically sorted, and thus all per-stream-data is intrinsically sorted by stream ID, we have the ability to use either of the above strategies. More importantly for us, we have the ability to be _adaptive_ to those strategies, and swap between them as needed. Thus we deploy the StreamMap. This data structure stores per-stream data in a pair of circular buffers, keyed by stream ID. For smallish numbers of streams we just search these buffers linearly. For larger numbers, we flip over to binary search. Why circular buffers? Because we avoid a compaction problem. While in general removal of stream IDs is done in arbitrary order, in _general_ the mostly likely streams to end at any given time are the oldest and the youngest. Circular buffers make removing streams at those ends very cheap, as compaction can be done easily. This, along with avoiding new allocations, makes circular buffers a perfect data structure for this strategy. Modifications: - Implemented StreamMap - Replaced Dictionary Results: Meaningful performance gains on all benchmarks. 1k requests with 1 concurrent stream sees about a .1% performance improvement. 1k requests with 100 concurrent streams sees a more like 15% performance improvement. Co-authored-by: George Barnett <gbarnett@apple.com>
342 lines
16 KiB
Swift
342 lines
16 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
import NIO
|
|
|
|
|
|
/// A representation of the state of the HTTP/2 streams in a single HTTP/2 connection.
|
|
struct ConnectionStreamState {
|
|
/// The "safe" default value of SETTINGS_MAX_CONCURRENT_STREAMS.
|
|
static let defaultMaxConcurrentStreams: UInt32 = 100
|
|
|
|
fileprivate static let emptyStreamMap = StreamMap<HTTP2StreamStateMachine>.empty()
|
|
|
|
/// The underlying data storage for the HTTP/2 stream state.
|
|
private var activeStreams: StreamMap<HTTP2StreamStateMachine>
|
|
|
|
/// A collection of recently reset streams.
|
|
///
|
|
/// The recently closed streams are stored to provide better resilience against synchronization errors between
|
|
/// the local and remote sides of the connection. Specifically, if a stream was recently closed, frames may have
|
|
/// been in flight that should not be considered errors. We maintain a small amount of state to protect against
|
|
/// this case.
|
|
private var recentlyResetStreams: CircularBuffer<HTTP2StreamID>
|
|
|
|
/// The maximum number of reset streams we'll persist.
|
|
///
|
|
/// TODO (cory): Make this configurable!
|
|
private let maxResetStreams: Int = 32
|
|
|
|
/// The current number of streams that are active and that were initiated by the client.
|
|
private var clientStreamCount: UInt32 = 0
|
|
|
|
/// The current number of streams that are active and that were initiated by the server.
|
|
private var serverStreamCount: UInt32 = 0
|
|
|
|
/// The highest stream ID opened or reserved by the client.
|
|
private var lastClientStreamID: HTTP2StreamID = .rootStream
|
|
|
|
/// The highest stream ID opened or reserved by the server.
|
|
private var lastServerStreamID: HTTP2StreamID = .rootStream
|
|
|
|
/// The maximum number of streams that may be active at once, initiated by the client.
|
|
///
|
|
/// Corresponds to the value of SETTINGS_MAX_CONCURRENT_STREAMS set by the client.
|
|
var maxClientInitiatedStreams: UInt32 = ConnectionStreamState.defaultMaxConcurrentStreams
|
|
|
|
/// The maximum number of streams that may be active at once, initiated by the server.
|
|
///
|
|
/// Corresponds to the value of SETTINGS_MAX_CONCURRENT_STREAMS set by the server.
|
|
var maxServerInitiatedStreams: UInt32 = ConnectionStreamState.defaultMaxConcurrentStreams
|
|
|
|
/// The total number of streams currently active.
|
|
var openStreams: Int {
|
|
return Int(self.clientStreamCount) + Int(self.serverStreamCount)
|
|
}
|
|
|
|
init() {
|
|
self.activeStreams = StreamMap()
|
|
self.recentlyResetStreams = CircularBuffer(initialCapacity: self.maxResetStreams)
|
|
}
|
|
|
|
/// Create stream state for a remotely pushed stream.
|
|
///
|
|
/// Unlike with idle streams, which are served by `modifyStreamStateCreateIfNeeded`, for pushed streams we do not
|
|
/// have to perform a modification operation. For this reason, we can use a simpler control flow.
|
|
///
|
|
/// - parameters:
|
|
/// - streamID: The ID of the pushed stream.
|
|
/// - remoteInitialWindowSize: The initial window size of the remote peer.
|
|
/// - throws: If the stream ID is invalid.
|
|
mutating func createRemotelyPushedStream(streamID: HTTP2StreamID, remoteInitialWindowSize: UInt32) throws {
|
|
try self.reserveServerStreamID(streamID)
|
|
let streamState = HTTP2StreamStateMachine(receivedPushPromiseCreatingStreamID: streamID, remoteInitialWindowSize: remoteInitialWindowSize)
|
|
self.activeStreams.insert(streamState)
|
|
}
|
|
|
|
/// Create stream state for a locally pushed stream.
|
|
///
|
|
/// Unlike with idle streams, which are served by `modifyStreamStateCreateIfNeeded`, for pushed streams we do not
|
|
/// have to perform a modification operation. For this reason, we can use a simpler control flow.
|
|
///
|
|
/// - parameters:
|
|
/// - streamID: The ID of the pushed stream.
|
|
/// - localInitialWindowSize: Our initial window size..
|
|
/// - throws: If the stream ID is invalid.
|
|
mutating func createLocallyPushedStream(streamID: HTTP2StreamID, localInitialWindowSize: UInt32) throws {
|
|
try self.reserveServerStreamID(streamID)
|
|
let streamState = HTTP2StreamStateMachine(sentPushPromiseCreatingStreamID: streamID, localInitialWindowSize: localInitialWindowSize)
|
|
self.activeStreams.insert(streamState)
|
|
}
|
|
|
|
/// Obtains a stream state machine in order to modify its state, potentially creating it if necessary.
|
|
///
|
|
/// The `creator` block will be called if the stream does not exist already. The `modifier` block will be called
|
|
/// if the stream was created, or if it was found in the map.
|
|
///
|
|
/// - parameters:
|
|
/// - streamID: The ID of the stream to modify.
|
|
/// - localRole: The connection role of the local peer.
|
|
/// - localInitialWindowSize: The initial size of the local flow control window for new streams.
|
|
/// - remoteInitialWindowSize: The initial size of the remote flow control window for new streams.
|
|
/// - modifier: A block that will be invoked to modify the stream state, if present.
|
|
/// - throws: Any errors thrown from the creator.
|
|
/// - returns: The result of the state modification, as well as any state change that occurred to the stream.
|
|
mutating func modifyStreamStateCreateIfNeeded(streamID: HTTP2StreamID,
|
|
localRole: HTTP2StreamStateMachine.StreamRole,
|
|
localInitialWindowSize: UInt32,
|
|
remoteInitialWindowSize: UInt32,
|
|
modifier: (inout HTTP2StreamStateMachine) -> StateMachineResultWithStreamEffect) throws -> StateMachineResultWithStreamEffect {
|
|
func creator() throws -> HTTP2StreamStateMachine {
|
|
try self.reserveClientStreamID(streamID)
|
|
let initialValue = HTTP2StreamStateMachine(streamID: streamID,
|
|
localRole: localRole,
|
|
localInitialWindowSize: localInitialWindowSize,
|
|
remoteInitialWindowSize: remoteInitialWindowSize)
|
|
return initialValue
|
|
}
|
|
|
|
// FIXME(cory): This isn't ideal, but it's necessary to avoid issues with overlapping accesses on the activeStreams
|
|
// map. The above closure takes a mutable copy of self, which is a big issue, so we should investigate whether
|
|
// it's possible for me to be smarter here.
|
|
var activeStreams = ConnectionStreamState.emptyStreamMap
|
|
swap(&activeStreams, &self.activeStreams)
|
|
defer {
|
|
swap(&activeStreams, &self.activeStreams)
|
|
}
|
|
|
|
guard let result = try activeStreams.transformOrCreateAutoClose(streamID: streamID, creator, modifier) else {
|
|
preconditionFailure("Stream was missing even though we should have created it!")
|
|
}
|
|
|
|
if let effect = result.effect, effect.closedStream {
|
|
self.streamClosed(streamID)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
/// Obtains a stream state machine in order to modify its state.
|
|
///
|
|
/// The block will be called so long as the stream exists in the currently active streams. If it does not, we will check
|
|
/// whether the stream has been closed already.
|
|
///
|
|
/// - parameters:
|
|
/// - streamID: The ID of the stream to modify.
|
|
/// - ignoreRecentlyReset: Whether a recently reset stream should be ignored. Should be set to `true` when receiving frames.
|
|
/// - ignoreClosed: Whether a closed stream should be ignored. Should be set to `true` when receiving window update or reset stream frames.
|
|
/// - modifier: A block that will be invoked to modify the stream state, if present.
|
|
/// - returns: The result of the state modification, as well as any state change that occurred to the stream.
|
|
mutating func modifyStreamState(streamID: HTTP2StreamID,
|
|
ignoreRecentlyReset: Bool,
|
|
ignoreClosed: Bool = false,
|
|
_ modifier: (inout HTTP2StreamStateMachine) -> StateMachineResultWithStreamEffect) -> StateMachineResultWithStreamEffect {
|
|
guard let result = self.activeStreams.autoClosingTransform(streamID: streamID, modifier) else {
|
|
return StateMachineResultWithStreamEffect(result: self.streamMissing(streamID: streamID, ignoreRecentlyReset: ignoreRecentlyReset, ignoreClosed: ignoreClosed), effect: nil)
|
|
}
|
|
|
|
if let effect = result.effect, effect.closedStream {
|
|
self.streamClosed(streamID)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
/// Obtains a stream state machine in order to modify its state due to a stream reset initiated locally.
|
|
///
|
|
/// The block will be called so long as the stream exists in the currently active streams. If it does not, we will check
|
|
/// whether the stream has been closed already.
|
|
///
|
|
/// This block must close the stream. Failing to do so is a programming error.
|
|
///
|
|
/// - parameters:
|
|
/// - streamID: The ID of the stream to modify.
|
|
/// - modifier: A block that will be invoked to modify the stream state, if present.
|
|
/// - returns: The result of the state modification, as well as any state change that occurred to the stream.
|
|
@inline(__always)
|
|
mutating func locallyResetStreamState(streamID: HTTP2StreamID,
|
|
_ modifier: (inout HTTP2StreamStateMachine) -> StateMachineResultWithStreamEffect) -> StateMachineResultWithStreamEffect {
|
|
guard let result = self.activeStreams.autoClosingTransform(streamID: streamID, modifier) else {
|
|
// We never ignore recently reset streams here, as this should only ever be used when *sending* frames.
|
|
return StateMachineResultWithStreamEffect(result: self.streamMissing(streamID: streamID, ignoreRecentlyReset: false, ignoreClosed: false), effect: nil)
|
|
}
|
|
|
|
|
|
guard let effect = result.effect, effect.closedStream else {
|
|
preconditionFailure("Locally resetting stream state did not close it!")
|
|
}
|
|
self.recentlyResetStreams.prependWithoutExpanding(streamID)
|
|
self.streamClosed(streamID)
|
|
|
|
return result
|
|
}
|
|
|
|
/// Performs a state-modifying operation on all streams.
|
|
///
|
|
/// As with the other block-taking functions in this module, this is @inline(__always) to ensure
|
|
/// that we don't end up actually heap-allocating a closure here. We're sorry about it!
|
|
@inline(__always)
|
|
mutating func forAllStreams(_ body: (inout HTTP2StreamStateMachine) throws -> Void) rethrows {
|
|
try self.activeStreams.mutatingForEachValue(body)
|
|
}
|
|
|
|
/// Adjusts the stream state to reserve a client stream ID.
|
|
mutating func reserveClientStreamID(_ streamID: HTTP2StreamID) throws {
|
|
guard self.clientStreamCount < self.maxClientInitiatedStreams else {
|
|
throw NIOHTTP2Errors.maxStreamsViolation()
|
|
}
|
|
|
|
guard streamID > self.lastClientStreamID else {
|
|
throw NIOHTTP2Errors.streamIDTooSmall()
|
|
}
|
|
|
|
guard streamID.mayBeInitiatedBy(.client) else {
|
|
throw NIOHTTP2Errors.invalidStreamIDForPeer()
|
|
}
|
|
|
|
self.lastClientStreamID = streamID
|
|
self.clientStreamCount += 1
|
|
}
|
|
|
|
/// Adjusts the stream state to reserve a server stream ID.
|
|
mutating func reserveServerStreamID(_ streamID: HTTP2StreamID) throws {
|
|
guard self.serverStreamCount < self.maxServerInitiatedStreams else {
|
|
throw NIOHTTP2Errors.maxStreamsViolation()
|
|
}
|
|
|
|
guard streamID > self.lastServerStreamID else {
|
|
throw NIOHTTP2Errors.streamIDTooSmall()
|
|
}
|
|
|
|
guard streamID.mayBeInitiatedBy(.server) else {
|
|
throw NIOHTTP2Errors.invalidStreamIDForPeer()
|
|
}
|
|
|
|
self.lastServerStreamID = streamID
|
|
self.serverStreamCount += 1
|
|
}
|
|
|
|
/// Drop all streams with stream IDs larger than the given stream ID that were initiated by the given role.
|
|
///
|
|
/// - parameters:
|
|
/// - streamID: The last stream ID the remote peer is promising to handle.
|
|
/// - droppedLocally: Whether this drop was caused by sending a GOAWAY frame or receiving it.
|
|
/// - initiator: The peer that sent the GOAWAY frame.
|
|
/// - returns: the stream IDs closed by this operation.
|
|
mutating func dropAllStreamsWithIDHigherThan(_ streamID: HTTP2StreamID,
|
|
droppedLocally: Bool,
|
|
initiatedBy initiator: HTTP2ConnectionStateMachine.ConnectionRole) -> [HTTP2StreamID]? {
|
|
let idsToDrop = self.activeStreams.elements(initiatedBy: initiator).drop(while: {$0.streamID <= streamID }).map { $0.streamID }
|
|
guard idsToDrop.count > 0 else {
|
|
return nil
|
|
}
|
|
|
|
for closingStreamID in idsToDrop {
|
|
self.activeStreams.removeValue(forStreamID: closingStreamID)
|
|
|
|
if droppedLocally {
|
|
self.recentlyResetStreams.prependWithoutExpanding(closingStreamID)
|
|
}
|
|
}
|
|
|
|
switch initiator {
|
|
case .client:
|
|
self.clientStreamCount -= UInt32(idsToDrop.count)
|
|
case .server:
|
|
self.serverStreamCount -= UInt32(idsToDrop.count)
|
|
}
|
|
|
|
return idsToDrop
|
|
}
|
|
|
|
/// Determines the state machine result to generate when we've been asked to modify a missing stream.
|
|
///
|
|
/// - parameters:
|
|
/// - streamID: The ID of the missing stream.
|
|
/// - ignoreRecentlyReset: Whether a recently reset stream should be ignored.
|
|
/// - ignoreClosed: Whether a closed stream should be ignored.
|
|
/// - returns: A `StateMachineResult` for this frame error.
|
|
private func streamMissing(streamID: HTTP2StreamID, ignoreRecentlyReset: Bool, ignoreClosed: Bool) -> StateMachineResult {
|
|
if ignoreRecentlyReset && self.recentlyResetStreams.contains(streamID) {
|
|
return .ignoreFrame
|
|
}
|
|
|
|
switch streamID.mayBeInitiatedBy(.client) {
|
|
case true where streamID > self.lastClientStreamID,
|
|
false where streamID > self.lastServerStreamID:
|
|
// The stream in question is idle.
|
|
return .connectionError(underlyingError: NIOHTTP2Errors.noSuchStream(streamID: streamID), type: .protocolError)
|
|
default:
|
|
// This stream must have already been closed.
|
|
if ignoreClosed {
|
|
return .ignoreFrame
|
|
} else {
|
|
return .connectionError(underlyingError: NIOHTTP2Errors.noSuchStream(streamID: streamID), type: .streamClosed)
|
|
}
|
|
}
|
|
}
|
|
|
|
private mutating func streamClosed(_ streamID: HTTP2StreamID) {
|
|
assert(!self.activeStreams.contains(streamID: streamID))
|
|
if streamID.mayBeInitiatedBy(.client) {
|
|
self.clientStreamCount -= 1
|
|
} else {
|
|
self.serverStreamCount -= 1
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private extension CircularBuffer {
|
|
/// Prepends `element` without expanding the capacity, by dropping the
|
|
/// element at the end if necessary.
|
|
mutating func prependWithoutExpanding(_ element: Element) {
|
|
if self.capacity == self.count {
|
|
self.removeLast()
|
|
}
|
|
self.prepend(element)
|
|
}
|
|
}
|
|
|
|
|
|
extension StreamStateChange {
|
|
fileprivate var closedStream: Bool {
|
|
switch self {
|
|
case .streamClosed, .streamCreatedAndClosed:
|
|
return true
|
|
case .streamCreated, .windowSizeChange:
|
|
return false
|
|
}
|
|
}
|
|
}
|