Commit Graph
19 Commits
Author SHA1 Message Date
George Barnett 61f6aa4142 Add explicit Sendable annotations (#505) 2025-03-27 14:01:05 +00:00
Gus Cairo 1879e72392 Finish multiplexer's inbound streams in more cases (#483)
## Motivation
Currently, the multiplexer's inbound streams stream is finished only
when the channel becomes inactive.
There are some scenarios in which the channel may be closed before it
has a chance to become active, and the stream will never be finished.
This can cause any users iterating over the stream to hang.

## Modifications
This PR finishes the inbound streams stream when a connection error is
fired, and when the handler is removed.

## Result
Fewer bugs.
2024-11-15 10:45:47 +00:00
Peter Adams 1118370ec4 Apply swift-format changes and CI (#472)
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
2024-10-29 13:09:54 +00:00
George Barnett 6693a60432 Propagate ChannelShouldQuiesceEvent to child channels (#464)
Motivation:

NIO has a 'ChannelShouldQuiesceEvent' which channels can listen for in
order to know when they should quiesce. This is typically used to
initiate a graceful shutdown of an HTTP/2 server. However, child
channels aren't notified of this event so HTTP/2 servers must keep track
of streams separately in order to notify them when the server is
quiescing.

Modifications:

- Propagate the `ChannelShouldQuiesceEvent` to child channels

Result:

Child channels can watch for `ChannelShouldQuiesceEvent`s
2024-09-18 07:02:53 +01:00
Gustavo Cairo 79802e0b69 Reduce allocations on InlineStreamMultiplexer/createStreamChannel (#450)
## Motivation
When creating stream channels on the `InlineStreamMultiplexer`, we are currently performing a `flatSubmit` on two different codepaths. This allocates two ELFs: one on `submit`, and one on `flatMap`. This is unnecessary, as we could do with just one allocation.

## Modifications
Create a single ELP and call `execute` on the EL instead of using `flatSubmit`.

## Result
Fewer allocations
2024-07-16 07:29:04 -07:00
Rick Newton-Rogers 285e444b49 Sendability warnings (#417)
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
2023-11-01 14:36:38 +00:00
Rick Newton-Rogers a6c66bcbe7 Mark public generic async methods inlinable (#426)
* Mark generic async methods inlinable

Motivation:

Mark generic async methods inlinable where possible to improve performance.

Modifications:

Multiple methods marked `@inlinable`

Result:

Performance increase
2023-10-30 09:29:37 +00:00
Rick Newton-Rogers 462ded73ad HTTP/2 Async API (#424)
We previously developed an async API for NIO HTTP/2 which was guarded under SPI. Now that the swift-nio async API is released we can reintroduce this code promoted to SPI.

This change introduces:
* `AsyncStreamMultiplexer` - an async variant of the HTTP/2 stream multiplexer which can be used to create outbound streams and provide access to an async sequence (`NIOHTTP2AsyncSequence`) of inbound streams
* New pipeline configuration functions (e.g. `configureAsyncHTTP2Pipeline`) to support the new async mode
2023-10-25 15:49:16 +01:00
Rick Newton-Rogers 2140160e95 Remove dependency on SPI (#419)
Motivation:

Remove dependency on SPI so that we can cut a release without
introducing dependency on code subject to change.

Modifications:

Remove all code which introduces a dependency on the NIOAsyncChannel SPI

Result:

Code inside SPI is removed
2023-10-04 13:21:17 +01:00
Rick Newton-Rogers 8275314739 Async stream initializers order matches existing (#415)
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.
2023-09-11 14:46:40 +01:00
Rick Newton-Rogers 044339de7b HTTP/2 negotiated connection pipeline config (#408)
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.
2023-07-12 11:07:53 +01:00
Rick Newton-Rogers 188fa9ba27 adopt NIOAsyncChannel.Configuration (#409)
Adopt NIOAsyncChannel.Configuration as the NIO SPI changed
2023-07-11 16:43:41 +01:00
Rick Newton-Rogers d5f0715865 Tweak formatting of function DocC comments (#407)
Motivation:

Tweaking the formatting of the DocC comments enables syntax highlighting
for parameter names in Xcode

Modifications:

Whitespace changes, capitalisation changes and added a parameter
description for `indexing` in HOACKHeaders `replaceOrAdd`, `add`.

Result:

More readable DocC comments
2023-07-05 05:35:47 -07:00
Rick Newton-Rogers 6e35856efa Add additional NIOAsyncChannel config (#406)
* Add additional NIOAsyncChannel config

Motivation:

Recent additions to pipeline configuration did not expose some of the
`NIOAsyncChannel` configuration.

Modifications:

Expose `backpressureStrategy`, `isOutboundHalfClosureEnabled`.

I think specific naming and object encapsulation for these parameters should be discussed
later before SPI is removed and we review the API as a whole.

Result:

Pipelie helpers for `NIOAsyncChannel` expose `backpressureStrategy`, `isOutboundHalfClosureEnabled`.

* review comments, re-order parameters

* review comments

* param reordering, missing docs comments
2023-07-04 17:24:08 +01:00
Rick Newton-RogersandGeorge Barnett e0ffed790a HTTP/2 pipeline NIOAsyncChannel pipeline config (#403)
* HTTP/2 pipeline NIOAsyncChannel pipeline config

Motivation:

This continues the work to expose functionality which allows users to interact
with HTTP/2 connections via async abstractions and using structured
concurrency.

We build atop of previous work to configure pipelines to deal with
HTTP/2 (with no protocol negotiation) and to wrap connection/stream
channels with `NIOAsyncChannel`s. This will allow users to iterate over
streams and HTTP2Frames.

Modifications:

Provide functions which configure channels and pipelines with the HTTP2
handler, expose a multiplexer for dealing with streams and wrap
connection and stream channels with `NIOAsyncChannel`s.

Result:

Users will be able to create and interact with HTTP/2 connections via
`NIOAsyncChannel`s. Because HTTP/2 is a negotiated protocol and we do
not yet handle it, this is of limited utility.

* review comments

* Update Sources/NIOHTTP2/HTTP2ChannelHandler+InlineStreamMultiplexer.swift

Co-authored-by: George Barnett <gbarnett@apple.com>

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
2023-07-04 06:21:57 +01:00
Rick Newton-Rogers 83c04dbddb Generic helpers for HTTP/2 async pipelines (#401)
* 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.
2023-07-03 11:19:04 +01:00
Rick Newton-Rogers ec955b609b Inline multiplexer calls writability mgr directly (#386)
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`
2023-04-11 16:40:37 +01:00
Rick Newton-Rogers 795c1d4a48 Expose a functional NIOHTTP2StreamDelegate (#382)
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.
2023-03-31 09:14:18 +01:00
Rick Newton-Rogers befe813575 Implement direct stream (de)multiplexing in NIOHTTP2ChannelHandler (#380)
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.
2023-03-30 15:45:20 +01:00