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.
SwiftNIO HTTP/2
This project contains HTTP/2 support for Swift projects using SwiftNIO. To get started, check the API docs.
Building
swift-nio-http2 is a SwiftPM project and can be built and tested very simply:
$ swift build
$ swift test
Versions
Just like the rest of the SwiftNIO family, swift-nio-http2 follows SemVer 2.0.0 with a separate document declaring SwiftNIO's Public API.
swift-nio-http2 1.x
swift-nio-http2 versions 1.x are a pure-Swift implementation of the HTTP/2 protocol for SwiftNIO. It's part of the SwiftNIO 2 family of repositories and does not have any dependencies besides swift-nio and Swift 5. As the latest version, it lives on the master branch.
To depend on swift-nio-http2, put the following in the dependencies of your Package.swift:
.package(url: "https://github.com/apple/swift-nio-http2.git", from: "1.5.0"),
swift-nio-http2 0.x
The legacy swift-nio-http 0.x is part of the SwiftNIO 1 family of repositories and works on Swift 4.1 and newer but requires nghttp2 to be installed on your system. The source code can be found on the nghttp2-support-branch.
Developing SwiftNIO HTTP/2
For the most part, SwiftNIO development is as straightforward as any other SwiftPM project. With that said, we do have a few processes that are worth understanding before you contribute. For details, please see CONTRIBUTING.md in this repository.