Files
swift-nio-http2/Sources/NIOHTTP2/StreamChannelList.swift
T
Cory Benfield 125cab766c Define abstraction for multiplexer holding channel. (#215)
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.
2020-07-28 14:48:46 +01:00

100 lines
2.9 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2019 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
//
//===----------------------------------------------------------------------===//
/// 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: MultiplexerAbstractChannel?
private var tail: MultiplexerAbstractChannel?
}
/// A node for objects stored in an intrusive linked list.
///
/// 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: MultiplexerAbstractChannel?)
case notInList
}
fileprivate var state: ListState = .notInList
internal init() { }
}
extension StreamChannelList {
/// Append an element to the linked list.
mutating func append(_ element: MultiplexerAbstractChannel) {
precondition(!element.inList)
guard case .notInList = element.streamChannelListNode.state else {
preconditionFailure("Appended an element already in a list")
}
element.streamChannelListNode.state = .inList(next: nil)
if let tail = self.tail {
tail.streamChannelListNode.state = .inList(next: element)
self.tail = element
} else {
assert(self.head == nil)
self.head = element
self.tail = element
}
}
mutating func removeFirst() -> MultiplexerAbstractChannel? {
guard let head = self.head else {
assert(self.tail == nil)
return nil
}
guard case .inList(let next) = head.streamChannelListNode.state else {
preconditionFailure("Popped an element not in a list")
}
self.head = next
if self.head == nil {
assert(self.tail == head)
self.tail = nil
}
head.streamChannelListNode = .init()
return head
}
mutating func removeAll() {
while self.removeFirst() != nil { }
}
}
// MARK:- IntrusiveLinkedListElement helpers.
extension HTTP2StreamChannel {
/// Whether this element is currently in a list.
internal var inList: Bool {
switch self.streamChannelListNode.state {
case .inList:
return true
case .notInList:
return false
}
}
}