mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-27 05:04:23 +00:00
Motivation: As part of the work in #214 we need a way for the HTTP2StreamMultiplexer to hold multiple kinds of channels. It would be best if doing so didn't require substantial changes to HTTP2StreamMultiplexer, which is difficult because right now it is tightly coupled to HTTP2StreamChannel. There are a few ways we could do this. The most obvious option to most Swift programmers would be to define a protocol that covers the surface area we want to use, and then hold that. This works fairly well, but the downside of it is that it will force all calls from HTTP2StreamMultiplexer to become dynamically dispatched. These indirect calls are optimisation boundaries and so provide some performance costs. Another option would be to create a subclassing relationship. This is ok, but it also forces us into the realm of dynamic dispatch on some of these calls, and also provides general difficulty if we ever want to provide a backing channel that isn't easily made into a subclass. The remaining option is to provide an abstract object that uses an `enum` to store the kinds of channel we know about, and jump through it. This works really well for small numbers of channel types (1 and 2 channels being the best outcomes), as it introduces very few extra branches which are highly predictable, as well as allowing direct function calls. This is great: one predictable branch that cannot possibly miss cache, followed by a direct function call. Joy of joys! However, it would be best if we didn't tie ourselves into this implementation strategy. After all, down the road we could well end up with more kinds of channels, and at a certain point the cost of the indirect function call becomes _lower_ than the cost of jumping through the enumeration. For that reason, we should define an abstraction. Enter: `MultiplexerAbstractChannel`. This concrete type defines the interface that `HTTP2StreamMultiplexer` will use to interact with stream channels. As it's internal, the abstraction is totally porous: Swift is capable of optimising it away to nothing. This type is explicitly a reference type, maintaining the semantic expected by `HTTP2StreamMultiplexer`. This provides a nice encapsulation over our abstraction. `HTTP2StreamMultiplexer` never needs to know about how we abstract over multiple channel types: the entire abstraction lives in a single, simple file, and can easily be rewritten without difficulty. Modifications: - Define `MultiplexerAbstractChannel` and the interface expected by `HTTP2StreamMultiplexer`. - Define a `BaseChannel` `enum` that will be used to dispatch to multiple `Channel` types. - Rewrite `HTTP2StreamMultiplexer` to use this new abstraction. Result: A customisation point for new channels will exist.
157 lines
4.9 KiB
Swift
157 lines
4.9 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2020 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
|
|
|
|
/// This structure defines an abstraction over `HTTP2StreamChannel` that is used
|
|
/// by the `HTTP2StreamMultiplexer`.
|
|
///
|
|
/// The goal of this type is to help reduce the coupling between
|
|
/// `HTTP2StreamMultiplexer` and `HTTP2Channel`. By providing this abstraction layer,
|
|
/// it makes it easier for us to change the potential backing implementation without
|
|
/// having to modify `HTTP2StreamMultiplexer`.
|
|
///
|
|
/// Note that while this is a `struct`, this `struct` has _reference semantics_.
|
|
/// The implementation of `Equatable` & `Hashable` on this type reinforces that requirement.
|
|
struct MultiplexerAbstractChannel {
|
|
private var baseChannel: BaseChannel
|
|
|
|
init(allocator: ByteBufferAllocator,
|
|
parent: Channel,
|
|
multiplexer: HTTP2StreamMultiplexer,
|
|
streamID: HTTP2StreamID,
|
|
targetWindowSize: Int32,
|
|
outboundBytesHighWatermark: Int,
|
|
outboundBytesLowWatermark: Int) {
|
|
self.baseChannel = .frameBased(.init(allocator: allocator,
|
|
parent: parent,
|
|
multiplexer: multiplexer,
|
|
streamID: streamID,
|
|
targetWindowSize: targetWindowSize,
|
|
outboundBytesHighWatermark: outboundBytesHighWatermark,
|
|
outboundBytesLowWatermark: outboundBytesLowWatermark))
|
|
}
|
|
}
|
|
|
|
extension MultiplexerAbstractChannel {
|
|
enum BaseChannel {
|
|
case frameBased(HTTP2StreamChannel)
|
|
}
|
|
}
|
|
|
|
// MARK: API for HTTP2StreamMultiplexer
|
|
extension MultiplexerAbstractChannel {
|
|
var inList: Bool {
|
|
switch self.baseChannel {
|
|
case .frameBased(let base):
|
|
return base.inList
|
|
}
|
|
}
|
|
|
|
var streamChannelListNode: StreamChannelListNode {
|
|
get {
|
|
switch self.baseChannel {
|
|
case .frameBased(let base):
|
|
return base.streamChannelListNode
|
|
}
|
|
}
|
|
nonmutating set {
|
|
switch self.baseChannel {
|
|
case .frameBased(let base):
|
|
base.streamChannelListNode = newValue
|
|
}
|
|
}
|
|
}
|
|
|
|
func configure(initializer: ((Channel, HTTP2StreamID) -> EventLoopFuture<Void>)?, userPromise promise: EventLoopPromise<Channel>?) {
|
|
switch self.baseChannel {
|
|
case .frameBased(let base):
|
|
base.configure(initializer: initializer, userPromise: promise)
|
|
}
|
|
}
|
|
|
|
func performActivation() {
|
|
switch self.baseChannel {
|
|
case .frameBased(let base):
|
|
base.performActivation()
|
|
}
|
|
}
|
|
|
|
func networkActivationReceived() {
|
|
switch self.baseChannel {
|
|
case .frameBased(let base):
|
|
base.networkActivationReceived()
|
|
}
|
|
}
|
|
|
|
func receiveInboundFrame(_ frame: HTTP2Frame) {
|
|
switch self.baseChannel {
|
|
case .frameBased(let base):
|
|
base.receiveInboundFrame(frame)
|
|
}
|
|
}
|
|
|
|
func receiveParentChannelReadComplete() {
|
|
switch self.baseChannel {
|
|
case .frameBased(let base):
|
|
base.receiveParentChannelReadComplete()
|
|
}
|
|
}
|
|
|
|
func initialWindowSizeChanged(delta: Int) {
|
|
switch self.baseChannel {
|
|
case .frameBased(let base):
|
|
base.initialWindowSizeChanged(delta: delta)
|
|
}
|
|
}
|
|
|
|
func receiveWindowUpdatedEvent(_ windowSize: Int) {
|
|
switch self.baseChannel {
|
|
case .frameBased(let base):
|
|
base.receiveWindowUpdatedEvent(windowSize)
|
|
}
|
|
}
|
|
|
|
func parentChannelWritabilityChanged(newValue: Bool) {
|
|
switch self.baseChannel {
|
|
case .frameBased(let base):
|
|
base.parentChannelWritabilityChanged(newValue: newValue)
|
|
}
|
|
}
|
|
|
|
func receiveStreamClosed(_ reason: HTTP2ErrorCode?) {
|
|
switch self.baseChannel {
|
|
case .frameBased(let base):
|
|
base.receiveStreamClosed(reason)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension MultiplexerAbstractChannel: Equatable {
|
|
static func ==(lhs: MultiplexerAbstractChannel, rhs: MultiplexerAbstractChannel) -> Bool {
|
|
switch (lhs.baseChannel, rhs.baseChannel) {
|
|
case (.frameBased(let lhs), .frameBased(let rhs)):
|
|
return lhs === rhs
|
|
}
|
|
}
|
|
}
|
|
|
|
extension MultiplexerAbstractChannel: Hashable {
|
|
func hash(into hasher: inout Hasher) {
|
|
switch self.baseChannel {
|
|
case .frameBased(let base):
|
|
hasher.combine(ObjectIdentifier(base))
|
|
}
|
|
}
|
|
}
|