17 Commits
Author SHA1 Message Date
Aryan Shah 2f58d8ef84 Revert configuration type for HTTP/1 server pipeline setup (#3775)
### Motivation:

We introduced a new configuration type in #3746 to make further
extensions to the server pipeline (#3745 for example) easier to
introduce.

The plan was to merge #3745 soon after #3746 was merged, because #3745
added a breaking change to the new config type.

However, #3745 needs some more consideration. We should therefore revert
the change in #3746 to unblock new releases from being made until we
come up with a better solution.
 
### Modifications:

A clean revert of PR #3746.

### Result:

Unblocks new releases.
2026-11-02 13:00:48 +00:00
Aryan Shah 108daca35d Add configuration type for HTTP/1 server pipeline setup (#3746)
### Motivation:

`NIOHTTP1.HTTPPipelineSetup` contains helper methods which add HTTP/1
client/server handlers to the provided channel pipeline.

Over time, we have had to duplicate these methods when introducing a new
configuration option in order to not break API.

As a result, there is now lots of duplication in
`NIOHTTP1.HTTPPipelineSetup` and introducing more configuration options
is very cumbersome. We should instead add a configuration type to solve
this problem.

### Modifications:

- Added new configuration types named
`NIOHTTPServerPipelineConfiguration`, and
`NIOHTTPServerPipelineSynchronousConfiguration`.
- Added a new pipeline setup helper method on `ChannelPipeline` and
`ChannelPipeline.SynchronousOperations` that requires the configuration
type as an argument.
- Deprecated all existing helper methods on `ChannelPipeline` and
`ChannelPipeline.SynchronousOperations`.

### Result:

Better API.
2026-10-19 13:23:19 +01:00
Fabian FettandCory Benfield f5b34b27cb Allow non-copyable ByteToMessageDecoders and typed throws (#3731)
### Motivation

Decoders are state machines and state machines are a natural fit for
non-copyable types. Today `ByteToMessageDecoder` and
`NIOSingleStepByteToMessageDecoder` require their conformers to be
copyable.

While we are here we also want `NIOSingleStepByteToMessageDecoder` to be
able to declare a concrete error type, so that callers can handle decode
errors without going through an existential.

### Changes
  
- Make `ByteToMessageDecoder` and `NIOSingleStepByteToMessageDecoder`
`~Copyable`.
- Add an `associatedtype DecodeError: Error = any Error` to
`NIOSingleStepByteToMessageDecoder` and spell decode and decodeLast as
`throws(DecodeError)`. Decoders that don't declare it keep behaving
exactly as before.
- Allow `~Copyable` `ByteToMessageDecoder` in `ByteToMessageHandler`.
- Since `~Copyable` types cannot be type erased into an existential
before macOS 15, we have to work around the existing `CanDequeueWrites`
dance: instead of casting `self` to `CanDequeueWrites` in
`handlerAdded`, ByteToMessageHandler gains an optional
`deliverQueuedWrite` closure. It is formed in the constrained
`ChannelOutboundHandler.write`, the only place where `Decoder`'s
`WriteObservingByteToMessageDecoder` conformance is statically known,
the first time a write arrives re-entrantly.
This has to be a closure rather than a protocol requirement on
`ByteToMessageDecoder`. `Decoders` may conform to
`WriteObservingByteToMessageDecoder` conditionally while conforming to
`ByteToMessageDecoder` unconditionally — `HTTPDecoder` does exactly this
— and all generic instantiations share one `ByteToMessageDecoder`
witness table, so such a requirement could only ever be witnessed by a
non-observing default and the write would be silently dropped.
- Split the delivery out of `tryDecodeWrites` into an `@inline(never)
dequeueWrites()`. `decodeLoop` calls `tryDecodeWrites` on every
iteration, so the empty-queue check has to stay cheap; keeping the
delivery inline tripled that function's size (50 → 141 instructions) and
showed up directly on the normal decode path.
- `ByteToMessageDecoderVerifier` allows `~Copyable`
`ByteToMessageDecoder`s.
- Added tests that use the newly lifted `~Copyable` restriction, plus
regression coverage for a decoder whose write-observing conformance is
conditional (the HTTPDecoder shape) and for constructing the handler in
a generic context.

### Result

Decoders may be non-copyable and may declare a concrete error type.
  
The normal decode path does not change performance. The re-entrant write
path costs one extra allocation per handler, but is meaningfully cheaper
in instructions. I'm happy to provide the benchmarks used to generate
those numbers in a separate pr – leaving them out here, as we would end
up with as many benchmarks as we have unit tests sooner or later.
  
Instructions, macOS arm64, per 1000 iterations, min of 5 interleaved
rounds:
  
```
  ┌──────────────────────────────┬────────────┬────────────┬───────────────────────────────────┐
  │                              │    main    │  this PR   │                                   │
  ├──────────────────────────────┼────────────┼────────────┼───────────────────────────────────┤
  │ decode, no write observation │ 9,404,415  │ 9,360,896  │ −0.46% (noise ~0.8%, i.e. parity) │
  ├──────────────────────────────┼────────────┼────────────┼───────────────────────────────────┤
  │ decode + re-entrant write    │ 13,656,063 │ 12,541,951 │ −8.16%                            │
  ├──────────────────────────────┼────────────┼────────────┼───────────────────────────────────┤
  │ handler add / remove         │ 85,983,231 │ 83,361,791 │ −3.05% (noise ~2.2%)              │
  └──────────────────────────────┴────────────┴────────────┴───────────────────────────────────┘
```

Replacing the CanDequeueWrites existential with the closure is what
improves the reentrant write path: delivering a queued write is now a
direct closure call rather than a witness-table dispatch, and
handlerAdded no longer performs a runtime conditional-conformance
lookup.
The saving grows with the number of writes queued per decode — around
−16% with 8 queued writes, though that benchmark is noisier.

Allocations (nio-alloc-counter-tests, Swift 6.2):
  
```
  ┌────────────────────────────┬─────────┬─────────┬────────┐
  │                            │  main   │ this PR │        │
  ├────────────────────────────┼─────────┼─────────┼────────┤
  │ 1000_reqs_1_conn           │ 27,341  │ 27,342  │ +1     │
  ├────────────────────────────┼─────────┼─────────┼────────┤
  │ 1_reqs_1000_conn           │ 368,000 │ 369,000 │ +1,000 │
  ├────────────────────────────┼─────────┼─────────┼────────┤
  │ ping_pong_1000_reqs_1_conn │ 296     │ 296     │ —      │
  ├────────────────────────────┼─────────┼─────────┼────────┤
  │ decode_1000_ws_frames      │ 2,000   │ 2,000   │ —      │
  └────────────────────────────┴─────────┴─────────┴────────┘
```

Forming the closure captures Decoder.OutboundIn's metadata, so it
allocates once per handler that actually queues a re-entrant write —
1_reqs_1000_conn builds 1000 such handlers, hence +1000. main's
existential cast did not allocate, so this is a new cost rather than
staying flat. The 1_reqs_1000_conn threshold is raised by 1000 for
6.1/6.2/6.3; the nightly toolchains are unchanged (10,000 on both — they
specialize the closure away entirely).

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2026-09-17 13:44:37 +01:00
Fabian Fett e8e0774c1a Fix warnings: private setters cannot be referenced from an '@inlinable' function (#3719)
Fix the following warnings:

> setter for property 'xxx' is private and cannot be referenced from an
'@inlinable' function; this will be an error in a future Swift language
mode
2026-09-02 22:45:28 +00:00
Mahdi Bahrami 86c5ead5dd Introduce NIODecodedAsyncSequence for easy decoding of async sequences (#3407)
Previous PR: #3405 

Add an API on top of `AsyncSequence<ByteBuffer>` which can dynamically
decode values.
~~Add an API on top of the new `AsyncSequence<ByteBuffer>` APIs which
splits the file based on its content.~~

### Motivation:

Provides a nice API to decode files, instead of users having to go
though manually handling `BufferedReader.read(while:)`.
I struggled with this, as documented in
https://swift-open-source.slack.com/archives/C9MMT6VGB/p1760115481607159

### Modifications:

Add `NIODecodedAsyncSequence` + functions on `AsyncSequence<ByteBuffer>`
to create such a sequence.
~~Add `NIOSplitMessageDecoder` + stdlib-like functions on
`AsyncSequence<ByteBuffer>` to create such a sequence.~~

### Result:

Users can decode an async sequence of `ByteBuffer`s easier.
~~Users can easily split files based on their content.~~

### Checklist

See this comment for a checklist of the remaining things to do:
https://github.com/apple/swift-nio/pull/3407#issuecomment-3403382404
2025-10-16 09:49:55 +00:00
Rafael Cepeda 02be63c7f4 Fixes all warnings when -require-explicit-sendable flag is enabled (#3320)
Fixes all warnings when `-require-explicit-sendable` flag is enabled and
enables the flag on macOS CI.

### Motivation:

We want to ensure our public API is either explicitly marked as
`Sendable` or not.

### Modifications:

Marked appropriate public types as `Sendable`, or explicitly defined
their conformance to the `Sendable` protocol as unavailable.

### Result:

We can now enable `-require-explicit-sendable` compiler flag in our
codebase.
2025-07-30 11:50:28 +01:00
Rick Newton-Rogers adfd61adc5 Use Swift 6.0 docs pipeline (#2966)
### Motivation:

Documentation checking catches more issues in Swift 6.0.

### Modifications:

Adopt the Swift 6.0 image and fix the errors.

### Result:

More accurate docs.
2024-11-07 13:03:47 +00:00
Rick Newton-Rogers 530aa8d304 NIOSingleStepByteToMessageDecoder reentrancy safety (#2881)
### Motivation:

`NIOSingleStepByteToMessageDecoder` calls out part way through its
processing step to a user-provided closure which can cause re-entrant
behavior which violates the assumption made in the code that if the
buffer is non-empty at the start that will be true later in the method.

### Modifications:

`NIOSingleStepByteToMessageDecoder` no longer assumes a non-empty buffer
in its final phase of buffer management.

Further changes to protect against re-entrancy shouldn't be necessary
because the outside call to `messageReceiver` is the only one which is
permitted to be re-entrant (`decode` and `decodeLast` are not).

### Result:

`NIOSingleStepByteToMessageDecoder` can handle re-entrant processing
calls.
2024-09-13 11:43:44 +01:00
Franz Busch c9756e1083 Adopt swift-format (#2794)
* Apply formatting

* Apply no block comments rule

* Apply OmitExplicitReturns

* Apple OnlyOneTrailingClosureArgument

* Apply NoAssignmentInExpressions

* Fix up DontRepeatTypeInStaticProperties lint errors

* Apply `OrderedImports`

* Apply `ReplaceForEachWithForLoop`

* format file

* Enable the formatting pipeline

* Adopt `AmbiguousTrailingClosureOverload`

* Fix license header

* Fix format check

* Fix `EndOfLineComment`

* Fix CI

* Adapt CI script to check if changes when running formatting

* Separate lint and format into to steps

* Fix format

* Adopt `UseEarlyExits`

* Revert "Adopt `UseEarlyExits`"

This reverts commit d1ac5bbe12.
2024-07-19 11:48:17 +02:00
Johannes Weiss 7948ed2104 ChannelHandler: provide static (un)wrap(In|Out)bound(In|Out) (#2791) 2024-07-18 11:55:48 +01:00
Rick Newton-Rogers 67553a7d6d Bump minimum Swift version to 5.7 (#2524)
* Bump minimum Swift version to 5.7

Motivation:

Now that Swift 5.9 is GM we should update the supported versions and
remove 5.6

Modifications:

* Update `Package.swift`
* Remove `#if swift(>=5.7)` guards
* Delete the 5.6 docker compose file and make a 5.10 one
* Update integration test script
* Update docs

Result:

Remove support for Swift 5.6, add 5.10

* fix indentation issues

* 5.9 docker image use release image
2023-10-02 11:38:07 +01:00
Fabian Fett 546eaa261e Add unprocessedBytes property on NIOSingleStepByteToMessageProcessor (#2419) 2023-05-03 17:19:13 +01:00
David NadobaandCory Benfield 4574bdde82 Adopt Sendable in Codec.swift and SingleStepByteToMessageDecoder.swift (#2149)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-01 08:24:21 -07:00
Max Desiatov 564b29e103 Fix doc comment formatting in SingleStepByteToMessageDecoder.swift (#2076) 2022-04-12 07:17:13 -07:00
Fabian Fett 43901f9f9f Only reclaim once at the end of the loop (#1958)
Same issue as fixed #1733 for ByteToMessageHandler.

### Motivation:

`NIOSingleStepByteToMessageProcessor` called out to the decoder's shouldReclaimBytes method after every parsing attempt, even if the decoder returned a value (which means continue in the `decodeLoop`).

That's quite pointless because we won't add any bytes into the buffer before we're trying the decoder again. Further it is a huge performance penalty for a use-cases in which we only consume small frames from the buffer. (Like reading data rows in a database client)

### Modifications:

- Only ask the decoder if we should reclaim bytes if the decoder actually is not able to process more frames from the input buffer.
2021-09-17 12:03:20 +02:00
Fabian Fett d8e1b1f96f [NIOSingleStepByteToMessageProcessor] Inline process methods (#1959)
* Inline NIOSingleStepByteToMessageProcessor

* Code review
2021-09-17 10:37:34 +01:00
Cory Benfield 8d3d0e901c Move the Codecs to NIOCore (#1927)
Motivation:

Our basic Channel Handlers don't need to be in NIO anymore: they aren't
realistically tied to that code. So we can move them to NIOCore.

Modifications:

- Move Codecs to NIOCore
- Move SingleStepByteToMessageDecoder to NIOCore.

Result:

Even more stuff in NIOCore
2021-08-05 08:59:01 +01:00