mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-24 04:38:00 +00:00
* Add no-op NIOHTTP2ConnectionDemultiplexer Motivation: This change prepares for future work to embed the connection demultiplexer within the `HTTP2ChannelHandler` which will improve performance by removing the need for passing expensive `InboundUserEvent`s. Modifications: This first step defines a new protocol (`NIOHTTP2ConnectionDemultiplexer`) which will later be used to abstract over the embedded and legacy separate demultiplexer code but at the moment should not change the logic. Result: Behaviour should be unchanged. * review comments * no longer `nil` out demultiplexer on channelInactive * restructure calling `pendingUserEvent` & processing events * demultiplexer funcs use events, not components NIOHTTP2ConnectionDemultiplexer stream events now use event types directly rather than individual parameters which map to the event fields.
91 lines
3.1 KiB
Swift
91 lines
3.1 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2017-2021 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 NIOCore
|
|
|
|
/// A buffer of pending user events.
|
|
///
|
|
/// This buffer is used to ensure that we deliver user events and frames correctly from
|
|
/// the `NIOHTTP2Handler` in the face of reentrancy. Specifically, it is possible that
|
|
/// a re-entrant call will lead to `NIOHTTP2Handler.channelRead` being on the stack twice.
|
|
/// In this case, we do not want to deliver frames or user events out of order. Rather than
|
|
/// force the stack to unwind, we have this temporary storage location where all user events go.
|
|
/// This will be drained both before and after any frame read operation, to ensure that we
|
|
/// have always delivered all pending user events before we deliver a frame.
|
|
/// Deliberately not threadsafe or `Sendable`.
|
|
class InboundEventBuffer {
|
|
fileprivate var buffer: CircularBuffer<BufferedHTTP2UserEvent> = CircularBuffer(initialCapacity: 8)
|
|
|
|
func pendingUserEvent(_ event: NIOHTTP2StreamCreatedEvent) {
|
|
self.buffer.append(.streamCreated(event))
|
|
}
|
|
|
|
func pendingUserEvent(_ event: StreamClosedEvent) {
|
|
self.buffer.append(.streamClosed(event))
|
|
}
|
|
|
|
func pendingUserEvent(_ event: NIOHTTP2WindowUpdatedEvent) {
|
|
self.buffer.append(.streamWindowUpdated(event))
|
|
}
|
|
|
|
func pendingUserEvent(_ event: NIOHTTP2BulkStreamWindowChangeEvent) {
|
|
self.buffer.append(.initialStreamWindowChanged(event))
|
|
}
|
|
|
|
/// Wraps user event types.
|
|
///
|
|
/// This allows us to buffer and pass around the events without making use of an existential.
|
|
enum BufferedHTTP2UserEvent {
|
|
case streamCreated(NIOHTTP2StreamCreatedEvent)
|
|
case streamClosed(StreamClosedEvent)
|
|
case streamWindowUpdated(NIOHTTP2WindowUpdatedEvent)
|
|
case initialStreamWindowChanged(NIOHTTP2BulkStreamWindowChangeEvent)
|
|
}
|
|
}
|
|
|
|
|
|
// MARK:- Sequence conformance
|
|
extension InboundEventBuffer: Sequence {
|
|
typealias Element = BufferedHTTP2UserEvent
|
|
|
|
func makeIterator() -> InboundEventBufferIterator {
|
|
return InboundEventBufferIterator(self)
|
|
}
|
|
|
|
struct InboundEventBufferIterator: IteratorProtocol {
|
|
typealias Element = InboundEventBuffer.Element
|
|
|
|
let inboundBuffer: InboundEventBuffer
|
|
|
|
fileprivate init(_ buffer: InboundEventBuffer) {
|
|
self.inboundBuffer = buffer
|
|
}
|
|
|
|
func next() -> Element? {
|
|
if self.inboundBuffer.buffer.count > 0 {
|
|
return self.inboundBuffer.buffer.removeFirst()
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// MARK:- CustomStringConvertible conformance
|
|
extension InboundEventBuffer: CustomStringConvertible {
|
|
var description: String {
|
|
return "InboundEventBuffer { \(self.buffer) }"
|
|
}
|
|
}
|