Motivation:
Standardised formatting makes the project cleaner to look at and work
on.
Modifications:
Setup swift-format with the same settings as Swift-NIO
Result:
Common formatting throughout
Motivation:
To address warnings revealed with
`.enableExperimentalFeature("StrictConcurrency=complete"),` in Swift 5.8
Modifications:
* Numerous protections of state in tests
* Make the multiplexer instances explicitly loop bound
* Update channel initializers to be marked as Sendable
* Mark error backing storage types as unchecked sendable
Result:
Better thread safety
* Mark generic async methods inlinable
Motivation:
Mark generic async methods inlinable where possible to improve performance.
Modifications:
Multiple methods marked `@inlinable`
Result:
Performance increase
Motivation:
When adding in the SPI methods for exposing async sequences of HTTP/2
streams we moved the stream initialization to a subtly different
location so that it was easier to exfiltrate the outputs of those
initialization functions (such as protocol negotiation outputs).
In some cases this broke ordering expectations.
Modifications:
Yield the (optionally wrapped) channels as initializer outputs to the async stream
Result:
Changes only exist within SPI. Async inbound stream channel
initialization now matches previous behavior.
Motivation:
This continues the work to expose functionality which allows users to interact
with HTTP/2 connections via async abstractions and using structured
concurrency.
This work enables protocol negotiation between HTTP/1.1 and HTTP/2 in its most generic form.
Modifications:
Enable protocol negotiation between HTTP/1.1 and HTTP/2 using typed
negotiation results (`NIOProtocolNegotiationResult`) from the
`NIOTypedApplicationProtocolNegotiationHandler`.
In the HTTP/2 case the negotiation result will return the HTTP/2 connection
channel and the `NIOHTTP2Handler.AsyncStreamMultiplexer` which exposes
inbound streams as an iterable async stream.
Result:
Users will be able to set up negotiated HTTP/1.1 / HTTP/2 connections and
iterate over inbound streams.
* Store HTTP/2 inbound streams as async sequence
Motivation:
In preparation for exposing APIs which surface HTTP/2 connections and
streams using structured concurrency this PR introduces a store for
inbound HTTP/2 streams.
Modifications:
Define types and methods for storing generic types which wrap `Channels`
corresponding to inbound HTTP/2 streams in an async sequence.
Result:
The new types are not yet exposed, this work introduces part of the
framework for future functionality.
* Expose (as spi) helpers for H2 async pipelines
Motivation:
This PR is the first step in exposing APIs which surface HTTP/2 connections and
streams using structured concurrency. This PR exposes the most abstract
spelling of this concept, assuming no particular types/forms for the
types involved in the stream channel types.
Modifications:
* Store a generic `streamInitializer` on the common inbound initializer
which is used to initialize inbound streams before yielding them to
the continuation of streams.
* Expose pipeline configuration functions which assume HTTP/2 but
nothing else about sream channel types.
* Provide internal functions for creating streams without configuring
them. Configuring leads to activation so it can be helpful to allow
that step to be performed manually once any provided initialization
closures have beenn executed.
Result:
Adopters of the new SPI should be able to create outbound and deal with
inbound HTTP/2 stream channels using async streams.
Outside of SPI there should be no changes.
Motivation:
The `HTTP2StreamChannel` keeps track of how many bytes have been written
and how many are buffered for use in evaluating the 'writability' of the
stream channel. Bytes are considered 'written' or at least no longer
buffered when the write is completed (successfully or errored). In order to make the
determination of when writes are complete across channel handlers we attach a promise to all
writes and deduct adjust the byte counts on completion. Creating and
attaching these promises requires costly allocations.
Modifications:
We can remove the need to create these promises for the new inline
stream multiplexer because the multiplexer and man HTTP2 channel
operations are now all within the `HTTP2ChannelHandler`. This change
calls the `writabilityManager` directly when writes are completed.
Result:
* Fewer allocations are required for HTTP2 writes.
* Writes are considered 'complete' when they leave the `HTTP2ChannelHandler`
Motivation:
Allow handlers in the pipeline to be notified when an HTTP2 stream is created or
closed.
Modifications:
Store an instance of a `NIOHTTP2StreamDelegate` on the `NIOHTTP2Handler`
and `InlineStreamMultiplexer` so that it may be called when streams are
created or closed.
Added tests.
Result:
`NIOHTTP2Handler`s init'd with a `streamDelegate` will now call into
that delegate's `streamCreated` and `streamClosed` functions.
Motivation:
Less expensive stream multiplexing. To implement direct stream multiplexing (with an incomplete public API) to break up delivery and allow testing.
Modifications:
* Factor out common inbound stream multiplexer functionality from `HTTP2StreamMultiplexer` into `CommonInboundStreamMultiplexer` (first commit).
* Direct stream multiplexing is implemented as `InlineStreamMultiplexer`. It implements both the `HTTP2InboundStreamMultiplexer` and `HTTP2OutboundStreamMultiplexer` protocols.
* We expose `NIOHTTP2Hander.StreamMultiplexer` as a `public` view over the new multiplexer to allow stream creation.
* Adds various `propagate...` functions to relay signals such as read complete to the child channels held by the inline stream multiplexer.
* Adds a stub for `HTTP2StreamDelegate` which will be completed in an upcoming PR. It will be responsible for surfacing stream open/close information.
* Adds conveniences to `NIOHTTP2Handler` for setting up a handler using the inline multiplexer
* Duplicate existing tests and modify to use the inline stream multiplexer where appropriate. `SimpleClientServerFramePayloadStreamTests.swift` and `HTTP2FramePayloadStreamMultiplexerTests.swift` were copied and modified
Result:
Default behavior should be unchanged at this time.
Motivation:
When we hit a stream error on an outbound frame, we don't currently
distinguish between them and channel errors. This makes it impossible
for anyone to tell them apart. It also makes it impossible for the
multiplexer to deliver these errors into the relevant stream channels
that triggered them, making it essentially certain they'll be lost.
Better diagnostics would be good, so we are providing them.
Modifications:
- Add new StreamError type that carries a stream ID and an underlying
error.
- Emit this StreamError type for outbound stream errors.
- Deliver this StreamError type into the relevant child channels when
possible.
Result:
Better diagnostics for stream errors.
Motivation:
With NIO 2.32.0 we broke the core NIO module up into modules that split
apart the POSIX layer and the core abstractions. As a result, this
package no longer needs to express a hard dependency on the POSIX layer.
Modifications:
- Rewrote imports of NIO to NIOCore.
- Added NIOEmbedded imports where necessary in tests.
- Note that the main modules still depend on NIO, which is necessary
for backwards-compatibility reasons. This dependency is unused.
Result:
No need to use NIOPosix.
Motivation:
As part of the work done for #214 we made `HTTP2StreamChannel` generic
over the type of data it reads and writes. Unfortunately this cost us a
non-trivial amount of performance.
Modifications:
- Remove the genericism from 'HTTP2StreamChannel' by adding a mode for the
stream data type the channel is operating on (frames or frame
payloads), wrapping and unwrapping the data where necessary.
- Simplify the implementation of 'MultiplexerAbstractChannel'
- Remove 'HTTP2FrameConvertible' since it is no longer required
Result:
A perf win, hopefully.
Motivation:
To lift the requirement that stream creation order must match the order
of the first write on those channels we added a payload-based stream
channel. We need a way for users to create these channels and initialze
these channels when they are inbound.
Modifications:
- Adds a `createStreamChannel` using a stream initialzer without a
stream ID
- Adds a new multiplexer `init` where the inbound stream initialzer does
not use stream IDs
- Adds an additional 'configure' to MultiplexerAbstractChannel
- Replaces the `init` in `MultiplexerAbstractChannel` with two `static func`s:
we can't switch on an optional stream ID since inbound streams always
have a stream ID; we need to be be able to explicitly support choosing
the type of channel we want.
- Minor refactoring in `HTTP2StreamChannel` to avoid duplication between the
`configure` functions
Result:
- The multiplexer can create payload based streams manually and when a
new stream is inbound.
Co-authored-by: Cory Benfield <lukasa@apple.com>
Motivation:
As part of #214 we need to make `HTTP2StreamChannel` generic over the type
of message it expects to read and write. This allows us to define an
`HTTP2StreamChannel` which uses `HTTP2Frame.FramePayload` as its
currency type, rather than `HTTP2Frame`.
Modifications:
- Make `HTTP2StreamChannel` generic over `Message` which is constrained
by conformance to `HTTP2FrameConvertible` and
`HTTP2FramePayloadConvertible`
- `HTTP2StreamChannel` now expects to read in `Message`s (as opposed to
`HTTP2Frame`s) and have `Message`s written in to it.
- Added typealiases for frame and payload based stream channels.
- Added support for the payload based channel in
`MultiplexerAbstractChannel` (although one can't be created yet).
Result:
We can create payload based stream channels.
Co-authored-by: Cory Benfield <lukasa@apple.com>
Motivation:
As part of the work in #214, HTTP2StreamID will need to be able to
tolerate not knowing its stream ID. For now this is not a problem, but
we should think about everywhere we use the stream ID and make sure we
have a solid principle in mind. This lays the groundwork for future
enhancements.
Modifications:
- Update HTTP2StreamChannel to hold an optional stream ID.
- Update interface to HTTP2StreamMultiplexer to tolerate closing
channels without stream IDs (we'll need it later).
- Move the "get a new stream ID" functionality to a method on
HTTP2StreamMultiplexer.
- Validate we behave correctly with optional stream IDs in the channel.
Result:
We can tolerate stream IDs being not present.
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.