diff --git a/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift b/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift index 0c9bca9..746d1c8 100644 --- a/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift +++ b/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift @@ -27,7 +27,7 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun public typealias OutboundIn = HTTP2Frame public typealias OutboundOut = HTTP2Frame - private var streams: [HTTP2StreamID: HTTP2StreamChannel] = [:] + private var streams: [HTTP2StreamID: MultiplexerAbstractChannel] = [:] private let inboundStreamStateInitializer: ((Channel, HTTP2StreamID) -> EventLoopFuture)? private let channel: Channel private var context: ChannelHandlerContext! @@ -75,13 +75,15 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun self.didReadChannels.append(channel) } } else if case .headers = frame.payload { - let channel = HTTP2StreamChannel(allocator: self.channel.allocator, - parent: self.channel, - multiplexer: self, - streamID: streamID, - targetWindowSize: Int32(self.targetWindowSize), - outboundBytesHighWatermark: self.streamChannelOutboundBytesHighWatermark, - outboundBytesLowWatermark: self.streamChannelOutboundBytesLowWatermark) + let channel = MultiplexerAbstractChannel( + allocator: self.channel.allocator, + parent: self.channel, + multiplexer: self, + streamID: streamID, + targetWindowSize: Int32(self.targetWindowSize), + outboundBytesHighWatermark: self.streamChannelOutboundBytesHighWatermark, + outboundBytesLowWatermark: self.streamChannelOutboundBytesLowWatermark + ) self.streams[streamID] = channel channel.configure(initializer: self.inboundStreamStateInitializer, userPromise: nil) channel.receiveInboundFrame(frame) @@ -299,13 +301,15 @@ extension HTTP2StreamMultiplexer { self.channel.eventLoop.execute { let streamID = self.nextOutboundStreamID self.nextOutboundStreamID = HTTP2StreamID(Int32(streamID) + 2) - let channel = HTTP2StreamChannel(allocator: self.channel.allocator, - parent: self.channel, - multiplexer: self, - streamID: streamID, - targetWindowSize: Int32(self.targetWindowSize), - outboundBytesHighWatermark: self.streamChannelOutboundBytesHighWatermark, - outboundBytesLowWatermark: self.streamChannelOutboundBytesLowWatermark) + let channel = MultiplexerAbstractChannel( + allocator: self.channel.allocator, + parent: self.channel, + multiplexer: self, + streamID: streamID, + targetWindowSize: Int32(self.targetWindowSize), + outboundBytesHighWatermark: self.streamChannelOutboundBytesHighWatermark, + outboundBytesLowWatermark: self.streamChannelOutboundBytesLowWatermark + ) self.streams[streamID] = channel channel.configure(initializer: streamStateInitializer, userPromise: promise) } diff --git a/Sources/NIOHTTP2/MultiplexerAbstractChannel.swift b/Sources/NIOHTTP2/MultiplexerAbstractChannel.swift new file mode 100644 index 0000000..b827757 --- /dev/null +++ b/Sources/NIOHTTP2/MultiplexerAbstractChannel.swift @@ -0,0 +1,156 @@ +//===----------------------------------------------------------------------===// +// +// 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)?, userPromise promise: EventLoopPromise?) { + 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)) + } + } +} diff --git a/Sources/NIOHTTP2/StreamChannelList.swift b/Sources/NIOHTTP2/StreamChannelList.swift index f854607..87dae99 100644 --- a/Sources/NIOHTTP2/StreamChannelList.swift +++ b/Sources/NIOHTTP2/StreamChannelList.swift @@ -13,14 +13,14 @@ //===----------------------------------------------------------------------===// -/// A linked list for storing HTTP2StreamChannels. +/// A linked list for storing MultiplexerAbstractChannels. /// /// Note that while this object *could* conform to `Sequence`, there is minimal value in doing /// that here, as it's so single-use. If we find ourselves needing to expand on this data type /// in future we can revisit that idea. struct StreamChannelList { - private var head: HTTP2StreamChannel? - private var tail: HTTP2StreamChannel? + private var head: MultiplexerAbstractChannel? + private var tail: MultiplexerAbstractChannel? } /// A node for objects stored in an intrusive linked list. @@ -28,7 +28,7 @@ struct StreamChannelList { /// Any object that wishes to be stored in a linked list must embed one of these nodes. struct StreamChannelListNode { fileprivate enum ListState { - case inList(next: HTTP2StreamChannel?) + case inList(next: MultiplexerAbstractChannel?) case notInList } @@ -40,7 +40,7 @@ struct StreamChannelListNode { extension StreamChannelList { /// Append an element to the linked list. - mutating func append(_ element: HTTP2StreamChannel) { + mutating func append(_ element: MultiplexerAbstractChannel) { precondition(!element.inList) guard case .notInList = element.streamChannelListNode.state else { @@ -59,7 +59,7 @@ extension StreamChannelList { } } - mutating func removeFirst() -> HTTP2StreamChannel? { + mutating func removeFirst() -> MultiplexerAbstractChannel? { guard let head = self.head else { assert(self.tail == nil) return nil @@ -71,7 +71,7 @@ extension StreamChannelList { self.head = next if self.head == nil { - assert(self.tail === head) + assert(self.tail == head) self.tail = nil }