Files
swift-nio-http2/Sources/NIOHTTP2/HTTP2StreamChannel+OutboundStreamMultiplexer.swift
T
Rick Newton-Rogers 98170fdb0c Add multiplexer protocol abstraction in http2 stream channel (#379)
Motivation:

This change prepares for future work to embed the connection multiplexer within the `HTTP2ChannelHandler` which will improve performance by removing the need for passing expensive `InboundUserEvent`s.

Modifications:

This step defines a new protocol (`HTTP2StreamChannelMultiplexer`; named such to avoid confusion with the legacy handler, `HTTP2StreamMultiplexer`) which will later be used to abstract over the inline and legacy multiplexer code but at the moment should not change the logic.

Result:

Behaviour should be unchanged.
2023-03-06 05:49:13 -08:00

124 lines
4.2 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 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
/// Multiplexes outbound HTTP/2 frames from an HTTP/2 stream into an HTTP/2
/// connection.
internal protocol HTTP2OutboundStreamMultiplexer {
/// Write the frame into the HTTP/2 connection. Stream ID is included in the
/// frame.
func writeFrame(_ frame: HTTP2Frame, promise: EventLoopPromise<Void>?)
/// Flush the stream with the given ID.
func flushStream(_ id: HTTP2StreamID)
/// Request a stream ID for the given channel.
///
/// Required to lazily assign a stream ID to a channel on first write.
func requestStreamID(forChannel: Channel) -> HTTP2StreamID
/// Notify the multiplexer that the channel with the given ID closed.
///
/// Required as a channel may not have a stream ID when it closes.
func streamClosed(channelID: ObjectIdentifier)
/// Notify the multiplexer that the stream with the given ID closed.
func streamClosed(id: HTTP2StreamID)
}
extension HTTP2StreamChannel {
/// Abstracts over the integrated stream multiplexing (new) and the chained channel handler (legacy) multiplexing approaches.
///
/// We use an enum for this purpose since we can't use a generic (for API compatibility reasons) and it allows us to avoid the cost of using an existential.
internal enum OutboundStreamMultiplexer: HTTP2OutboundStreamMultiplexer {
case legacy(LegacyOutboundStreamMultiplexer)
case new
func writeFrame(_ frame: HTTP2Frame, promise: NIOCore.EventLoopPromise<Void>?) {
switch self {
case .legacy(let multiplexer):
multiplexer.writeFrame(frame, promise: promise)
case .new:
fatalError("Not yet implemented.")
}
}
func flushStream(_ id: HTTP2StreamID) {
switch self {
case .legacy(let multiplexer):
multiplexer.flushStream(id)
case .new:
fatalError("Not yet implemented.")
}
}
func requestStreamID(forChannel: NIOCore.Channel) -> HTTP2StreamID {
switch self {
case .legacy(let multiplexer):
return multiplexer.requestStreamID(forChannel: forChannel)
case .new:
fatalError("Not yet implemented")
}
}
func streamClosed(channelID: ObjectIdentifier) {
switch self {
case .legacy(let multiplexer):
multiplexer.streamClosed(channelID: channelID)
case .new:
fatalError("Not yet implemented.")
}
}
func streamClosed(id: HTTP2StreamID) {
switch self {
case .legacy(let multiplexer):
multiplexer.streamClosed(id: id)
case .new:
fatalError("Not yet implemented.")
}
}
}
}
/// Provides a 'multiplexer' interface for legacy compatibility.
///
/// This doesn't actually do any multiplexing but delegates it to the `HTTP2StreamMultiplexer` which does.
internal struct LegacyOutboundStreamMultiplexer {
let multiplexer: HTTP2StreamMultiplexer
}
extension LegacyOutboundStreamMultiplexer: HTTP2OutboundStreamMultiplexer {
func writeFrame(_ frame: HTTP2Frame, promise: NIOCore.EventLoopPromise<Void>?) {
self.multiplexer.childChannelWrite(frame, promise: promise)
}
func flushStream(_ id: HTTP2StreamID) {
self.multiplexer.childChannelFlush()
}
func requestStreamID(forChannel: NIOCore.Channel) -> HTTP2StreamID {
self.multiplexer.requestStreamID(forChannel: forChannel)
}
func streamClosed(channelID: ObjectIdentifier) {
self.multiplexer.childChannelClosed(channelID: channelID)
}
func streamClosed(id: HTTP2StreamID) {
self.multiplexer.childChannelClosed(streamID: id)
}
}