Commit Graph
2140 Commits
Author SHA1 Message Date
Franz Busch 37c1a793a4 Remove ProtocolNegotiationHandler protocol (#2519)
# Motivation
When we created the first round of new async bootstrap APIs we added a `ProtocolNegotiationHandler` protocol to identify handlers that are doing protocol negotiation. Since then, we have changed the way how the async bootstraps work and we no longer need this protocol.

# Modification
Remove the `ProtocolNegotiationHandler` protocol
2023-09-20 18:05:50 +01:00
Cory Benfield 3db5c4aeee Support for custom protocols in DatagramBootstrap (#2516)
Motivation

This patch adds support for custom protocol families to
DatagramBootstrap. In most cases this isn't useful, and will fail, but
in many OSes it's a recognised system for non-root processes to get a
way to send ICMP echo requests. This is worth supporting.

Modifications

- Expose the `ProtocolSubtype` type publicly.
- Add a `protocolSubtype` setter on `DatagramBootstrap`
- Write a test that actually does ICMP as non-root

Results

Users can send ICMP echo requests without root privileges.
2023-09-17 08:03:56 +01:00
Alex Taffe e0d85543d7 Simplify header separator and crlf (#2513)
Everywhere else in the file uses `crlf` and `headerSeperator` when writing those static strings, we shouldn't be using string literals here
2023-09-06 14:52:25 +01:00
Johannes Weiss 4e0248fec4 update CNIOLLHTTP to latest nodejs/llhttp (#2512) 2023-09-04 09:01:59 +01:00
Natik Gadzhi 34a7663c05 Make ByteBuffer Codable (#2509)
This commit adds Codable to ByteBuffer. It encodes in Base64 into a singleValue
container.

- Base64 implementation moved into _NIODataStructures
- Base64 now supports decoding.
2023-08-30 10:32:45 +01:00
Cory Benfield 7d9f892d83 Support users opting-out of us setting framing headers. (#2508)
Motivation:

Right now we always set the framing headers in HTTP requests and
responses that we send. This is a good practice for most users, but
it can cause issues, most notably in responses to CONNECT requests
which requires that we do not set framing headers.

Unfortunately, in NIO's current HTTP/1.1 design it is not possible
for us to suppress these headers. This is because the HTTP encoders
come _earlier_ in the pipeline than the decoders, so the HTTP
encoders do not know structurally what requests they are responding
to.

While we could merge the encoders/decoders, that's a fairly
substantial change. A better short-term change is to offer users
the ability to turn off the NIO header manipulation feature. In
this circumstance, users take on full responsibility for
appropriately framing their HTTP messages.

Modifications

- Add config to HTTPRequestEncoder and HTTPResponseEncoder.
- Plumb this config through.
- Add a bunch of tests.

Result

Users have way more control of HTTP/1.1 messages.
2023-08-22 02:51:05 -07:00
omochimetaru 022999ae6d make public NIOThreadPoolError init (#2503) 2023-08-16 23:59:36 -07:00
495edec1dd Docs: explain the basic safe usage of ByteBuffer (#2499)
* Docs: explain the basic safe usage of ByteBuffer

* Apply suggestions from code review

* Adrress review feedback and clean the note block in inline docc comments

---------

Co-authored-by: Si Beaumont <simonjbeaumont@gmail.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2023-08-15 13:06:09 +01:00
cdfbb20e92 Adding ByteBuffer.hexDump in xxd and hexdump -C formats (#2475)
* Initial implementation of hexdump for ByteBuffer

* Put that iff back there

* Reword hexDumpShort(): naming, tests, offset and full length support

* Use writerIndex to figure out the convinience end of useful content in the buffer

* hexDumpLong(offset:) — supports offset, and reads full buffer slice, not just readableBytes

* hexDumpLong() extra line failing test

* Using ByteBuffer(string:) instead of the allocator

* Move hexDumps code into ByteBuffer-hexdump.swift

* Optimized String(byte: poadding:)

* Avoid using storage.dumpBytes in hexDumpShort()

* Implemented a cleaner long hexdump

* Implemented the public hexdump API

* Revert ByteBuffer-core.swift to main

* Addressed review feedback!

* hexdump long format with clipped length

* reserve capacity in hexDumpLines

* Addressing review feedback

* String(_ value, radix, padding) and the final test case

* Make the hexDump(format: .detailed) format cleaner

* Spell out if let for Swift 5.8

* Fixed a formatting bug in multiline maxBytes detailed format

* Apply suggestions from code review

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

* Cleaner way to dump a line in .detailed format

* Cleaner generic argument type for String(_, radix:, padding:)

* Revert _storage.dumpBytes test to match previous format

* Simplify printable bytes closure to a single expression so Swift 5.6 is happy

---------

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
Co-authored-by: George Barnett <gbarnett@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2023-08-15 02:39:53 -07:00
taylorswift 8c2654c9c5 match casing of reference links to the casing of the actual referenced declaration/module (#2500) 2023-08-10 00:47:12 -07:00
Johannes WeissandCory Benfield ac695eb00f EventLoopFuture/Promise: only Sendable if Value is Sendable (#2496)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2023-08-08 03:57:25 -07:00
Franz Busch cf281631ff Avoid using deinit to fulfil the protocol negotiation promise (#2497)
# Motivation

Fixes https://github.com/apple/swift-nio/issues/2494

# Modification
This PR avoids using `deinit` to fulfil the protocol negotiation promise and opts to trap instead when it is being accessed before the handler is added. This allows us to use `handlerAdded` and `handlerRemoved`.

# Result
No more `deinit` usage that can be observed.
2023-08-08 09:17:08 +01:00
Franz Busch 28eb2ac360 AsyncChannel: Provide throwing finish method on test stream (#2493)
# Motivation
We recently provided testing utilities for the `NIOAsyncChannelInboundStream`. On thing that was missing is a way to finish the stream with an error.

# Modification
This PR provides a `finish()` method that takes an error which is thrown from the inbound stream.

# Result
Better way to test code relying on the AsyncChannel work.
2023-08-07 14:21:25 +01:00
finagolfin 1ec71beb8a Get Android building again after #2479 (#2490)
Motivation

NIO wasn't building on Android

Modifications

- Add the right checks for Android

Result

Built and passed all tests on Android AArch64
2023-08-05 07:45:56 +01:00
Si BeaumontandCory Benfield 858cb8927e Add bootstrap APIs for VSOCK sockets (#2479)
* Add bootstrap APIs for vsock sockets

Signed-off-by: Si Beaumont <beaumont@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2023-08-03 15:03:09 +01:00
Franz Busch 84540145ce Fix leaked promise in NIOTypedApplicationProtocolNegotiationHandler (#2489)
# Motivation
We sometimes leaked a promise in the `NIOTypedApplicationProtocolNegotiationHandler` when the handler was created and immediately deinited.

# Modification
This PR makes sure we always complete the negotiation promise of the handler.

# Result
No more leaked promises.
2023-08-02 11:34:59 +01:00
Cory Benfield 5be5ccb5a7 Initial PoC of Custom Actor Executor support (#2388)
* Support NIO Event Loops as actor executors

Motivation

Swift Concurrency supports having actors opt in to executing
within a "custom executor". This requires being able to be strictly
serial, a constraint that NIO's EventLoops natively meet. It is
useful to have actors be able to express a "tie" to an EventLoop
in order to efficiently access objects that are tied to specific ELs.

Modifications

- Extend EventLoop to expose an executor property
- Provide a simple best-effort default using NIODefaultSerialEventLoopExecutor
- Expose NIOSerialEventLoopExecutor protocol that makes it easy
  for adopters to get a better implementation
- Crash if EmbeddedEventLoop is used
- Add tests

Result

Adopters can use ELs as serial executors

* Prefer #if compiler
2023-07-31 07:19:49 -07:00
e20271f4a3 Make System.coreCount aware of cgroup2 (#2394)
Motivation:

The count already appears to be aware of cgroup1
but doesn't have logic to support cgroup2.

Modifications:

Add logic to parse /sys/fs/cgroup/cpu.max
For details see - https://stackoverflow.com/questions/65551215/get-docker-cpu-memory-limit-inside-container/65554131#65554131

Result:

Core count will adapt if constrained by a cgroup on more modern kernels.

---------

Co-authored-by: Max Desiatov <m_desiatov@apple.com>
Co-authored-by: Gwynne Raskind <gwynne@darkrainfall.org>
2023-07-31 05:50:24 -07:00
Cory BenfieldandFranz Busch 30595ad289 Don't lose or delay writes before channelActive (#2486)
* Don't lose or delay writes before channelActive

Motivation:

In rare circumstances, some users have reported hangs, delays, or
lost writes when writing network proxies. These proxies have
typically been derived from our TLSify example:
https://github.com/apple/swift-nio-examples/tree/main/TLSify

This proxy makes its outbound connections in handlerAdded.
Generally this works without issue, as the connection takes
long enough to set up that channelActive fires before the
outbound connection has been made.

However, in some rare circumstances it is possible to both make an
outbound connection, have it succeed, _and_ read from the network
on that connection before NIO activates the inbound channel. This
would cause users to lose writes.

This violates a cardinal NIO rule: writes should never be lost.
If it is practical for us to delay them and apply them later, we
should. This patch addresses several holes in this area to correctly
handle these outbound writes.

The first hole is that we would fail writes if the Channel wasn't
yet active. This constraint was unnecessary: it was added in a
refactor to use an internal state machine, and while we had a test
that this happened, it didn't protect us from anything. We can
safely buffer a write at this point: and indeed, we _do_ buffer
flushes at this point. So the check was unneeded.

The second hole was that, even if we buffered the writes, we
frequently wouldn't unbuffer them again until a flush happened
after channelActive. This didn't happen on _every_ path: in the
rare case that connect() succeeded immediately, we would flush
the writes. But in all other cases, including listening
connections, we needed _another_ flush to happen to get the
writes out.

This fix patches up both issues and reconciles the flows. Now
we _always_ try to push out flushed writes when we activate the
Channel, just as we always try to read.

Modifications:

- Remove a check that failed writes if we weren't active.
- Remove a test that validated that we did that.
- Attempt to emit flushed writes on channelActive.
- Make sure we mark ourselves writable if we couldn't write
  it all.
- Make `BaseSocket.getOption` non-final so we can hook it
  in the SAL.
- Make `AcceptHandler` non-private so we can use it in tests.
- Extend the SAL to support `getOption`
- Write several SAL tests to ensure we manage our writes.

Result:

No lost writes.

* Code review feedback

---------

Co-authored-by: Franz Busch <f.busch@apple.com>
2023-07-31 04:13:33 -07:00
Franz Busch fd3076131f Add testing interfaces for the stream and writer of the NIOAsyncChannel (#2488)
# Motivation
When using the `NIOAsyncChannel` to build higher level abstractions it is hard to test right now since one cannot drive the `NIOAsyncChannelInputStream` nor the `NIOAsyncChannelOutputWriter`.

# Modification
This PR adds new factory methods to both of those types which allow to either drive the input stream or record the outputs written to the output writer.

# Result
One can now successfully test their code written on top of the input stream and output writer.
2023-07-31 03:29:41 -07:00
Franz Busch df49d6bde4 Rename the waitForFinalResult() methods and provide an ELF convenience (#2483)
# Motivation
The name `waitForFinalResult()` is bad in a couple of ways. First, we normally use `wait` for blocking operations. Secondly, `finalResult` is also just strange because the user only wants to know the _result_ and doesn't even know that there might be intermediates.

# Modification
This PR renames the `waitForFinalResult()` method to be a property on `NIOProtocolNegotiationResult`. Additionally, it provides a conditional extension on `ELF` to avoid a `get()`.

# Result
Better naming for our async interfaces
2023-07-31 02:19:00 -07:00
Franz Busch b25bc5838f Add async TCP echo example (#2463)
* Add async TCP echo example

# Motivation
We introduced new async APIs to our bootstraps that make it easy to use NIO from Swift Concurrency. We should provide examples showing how to use those new APIs. In the future, we want to make those examples the primary ones and remove the non-async based ones.

# Modification
This PR introduces two new targets: `NIOTCPEchoServer` and `NIOTCPEchoClient`. Both targets are executable and provide either the server or client piece of the example.

# Result
New and shiny async example

* Code review

* Adopt NIO singletons

* Review

* George review
2023-07-31 01:53:41 -07:00
taylorswift 22ae903797 correct malformed codelink (#2484) 2023-07-28 02:58:35 -07:00
Johannes Weiss 6c21f51ff5 NIOSingletons: Use NIO in easy mode (#2471)
### Motivation:

SwiftNIO allows and encourages to precisely manage all operating system resources like file descriptors & threads. That is a very important property of the system but in many places -- especially since Swift Concurrency arrived -- many simpler SwiftNIO programs only require a single, globally shared EventLoopGroup. Often even with just one thread.

Long story short: Many, probably most users would happily trade precise control over the threads for not having to pass around `EventLoopGroup`s. Today, many of those users resort to creating (and often leaking) threads because it's simpler. Adding a `.globalSingle` static var which _lazily_ provides singleton `EventLoopGroup`s and `NIOThreadPool`s is IMHO a much better answer.

Finally, this aligns SwiftNIO a little more with Apple's SDKs which have a lot of global singletons that hold onto system resources (`Dispatch`'s thread pool, `URLSession.shared`, ...). At least in `Dispatch`'s case the Apple SDKs actually make it impossible to manage the resources, there can only ever be one global pool of threads. That's fine for app development but wouldn't be good enough for certain server use cases, therefore I propose to add `NIOSingleton`s as an _option_ to the user. Confident programmers (especially in libraries) are still free and encouraged to manage all the resources deterministically and explicitly.

Companion PRs: 
 - https://github.com/apple/swift-nio-transport-services/pull/180
 - https://github.com/swift-server/async-http-client/pull/697

### Modifications:

- Add the `NIOSingletons` type
- Add `MultiThreadedEventLoopGroup.singleton`
- Add `NIOThreadPool.singleton`

### Result:

- Easier use of NIO that requires fewer parameters for users who don't require full control.
- Helps with #2142
- Fixes #2472
- Partially addresses #2473
2023-07-27 00:59:47 -07:00
Iceman b0d43ec5b6 Fix huge compile time of setUUIDBytes (#2482) 2023-07-26 01:17:48 -07:00
Franz Busch a2e487b77f Make NIOAsyncChannel.Configuration Sendable (#2466) 2023-07-22 14:40:36 -07:00
Franz Busch e5416f4039 Reduce the amount of bootstrap async methods (#2474)
# Motivation
When adding new async methods for all the bootstrap we had to create 3 sets of method for every bootstrap. This resulted in lots of code and only provided little benefit for users.

# Modification
This PR removes all bind/connect methods except the most generic ones. This leaves it up to the user to wrap their channels in a `NIOAsyncChannel` or to await the protocol negotiation result.

# Result
Less code to maintain on our side.
2023-07-21 11:50:42 +01:00
Si Beaumont 1de708f023 Skip TCP_NODELAY on sockets that NIO doesn't explicitly support (#2476)
* Skip TCP_NODELAY on sockets that NIO doesn't explicitly support

Signed-off-by: Si Beaumont <beaumont@apple.com>

* fixup: Skip IPv6 test if host does not have IPv6

Signed-off-by: Si Beaumont <beaumont@apple.com>

* fixup: Rename test helper function

Signed-off-by: Si Beaumont <beaumont@apple.com>

* fixup: Remove test for unix domain socket

Signed-off-by: Si Beaumont <beaumont@apple.com>

---------

Signed-off-by: Si Beaumont <beaumont@apple.com>
2023-07-21 10:25:05 +01:00
Si Beaumont 83f49cb335 Throw SocketAddressError (cf. fatalError) in SocketAddress.convert for unknown address family (#2477)
* Throw SocketAddressError (cf. fatalError) in SocketAddress.convert for unknown address family

Signed-off-by: Si Beaumont <beaumont@apple.com>

* fixup: Use AF_UNSPEC, not AF_SYSTEM, in tests

Signed-off-by: Si Beaumont <beaumont@apple.com>

* fixup: Use try! instead of catch and fatalError

Signed-off-by: Si Beaumont <beaumont@apple.com>

* fixup: Move address conversion before side effects

Signed-off-by: Si Beaumont <beaumont@apple.com>

* fixup: Propagate error from SocketAddress.convert in DGVRM

Signed-off-by: Si Beaumont <beaumont@apple.com>

* fixup: Add __test_only shim for Swift 5.6

Signed-off-by: Si Beaumont <beaumont@apple.com>

---------

Signed-off-by: Si Beaumont <beaumont@apple.com>
2023-07-20 12:13:07 +01:00
Franz Busch 5f542894dd Add configuration for NIOAsyncChannel (#2464)
# Motivation
While adopting the new `NIOAsyncChannel` type we saw an exploding number of parameters passed to the various connect/bind/configure methods. All these methods had in common that we had to pass configuration for the `NIOAsyncChannel`.

# Modification
This PR introduces a new type `NIOAsyncChannel.Configuration` which groups all four configuration parameters into a struct.

# Result
We can now write more concise methods that use a single configuration object.
2023-07-11 10:36:47 +01:00
Franz Busch 555194ea90 Async methods for NIORawSocketBootstrap (#2460)
* Async methods for `NIORawSocketBootstrap`

# Motivation
We want to support async bootstrapping with all our bootstraps.

# Modification
This PR adds support for the `NIORawSocketBootstrap` and adds the three variants of methods to it (abstract output, `NIOAsyncChannel` based and protocol negotiation based)

# Result
We now support asynchronous interaction with the `NIORawSocketBootstrap`

* Code review and add protocol negotiation tests
2023-07-05 06:01:33 -07:00
Franz Busch 26627161bf Async support NIOPipeBootstrap (#2458)
* Async support NIOPipeBootstrap

# Motivation
We want to support async bootstrapping with all our bootstraps.

# Modification
This PR adds support for the `NIOPipeBootstrap` and adds the three variants of methods to it (abstract output, `NIOAsyncChannel` based and protocol negotiation based)

# Result
We now support asynchronous interaction with the `NIOPipeBootstrap`

* Rename to takingOwnershipOfInputOutputDescriptor

* Rename to takingOwnershipOfDescriptor and takingOwnershipOfDescriptors

* _ prefix

* doc comment
2023-07-05 14:36:53 +02:00
Felix Schlegel 0ec899afc5 Make NIO[Throwing]AsyncSequenceProducer.Source Sendable (#2459) 2023-07-04 10:22:49 +02:00
Gwynne Raskind a408273cb1 Fix several minor typos in comments found in various files (#2455) 2023-06-30 07:59:19 +01:00
Felix SchlegelandCory Benfield 2ab733246c Close Channel when input & output are closed (#2450)
* Close Channel when input & output are closed

Motivation:

Suppose we have a channel whose input and output are closed.
On Linux, this currently results in a full socket closure (which means
TCP `RST` is being sent). This behaviour is currently different
on Darwin because we don't close our socket here when our input and output are
closed.

We should not have two different platform-dependent behaviours here.

As we cannot suppress the TCP `RST` on Linux, we have to align the
Darwin behaviour to the Linux behaviour meaning we close
our entire NIO channel when both the input and the output are closed.

Modifications:

* `BaseStreamSocketChannel`: escalate to full closure when both its
  input and its output are closed
* add new test
  `ChannelTests.testInputAndOutputClosedResultsInFullClosure()`
  verifying the desired behaviour

Result:

`BaseStreamSocketChannel` will now close entirely when both input and
output are closed.

* Review Cory

Modifications:

* `testInputAndOutputClosedResultsInFullClosure`:
    * prefix all server related variables with `server*`
    * assert that `serverConnectionChildChannel` becomes inactive once
      the `clientConnectionChannel` is fully closed

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2023-06-29 02:30:36 -07:00
finagolfin 231902a295 Correct a few Musl additions from #2449 for Android, plus error if libc not found (#2451)
Motivation

Fix build errors on Android

Modifications

- Fix previous Musl modifications that assumed Glibc wasn't imported on Android
- Add errors for all libc imports, so new platform ports error out early

Result

NIO builds natively on Android again, with all the same tests passing
2023-06-26 02:52:18 -07:00
Max Desiatov 43b1f59fde Add support for Musl libc (#2449)
* Add support for Musl libc

Since Musl is sufficiently different from Glibc (see https://wiki.musl-libc.org/functional-differences-from-glibc.html), it requires a different import, which now should be applied to files that have `import Glibc` in them.

* Fix `msghdr` initialization

* Fix msghdr mutability

* Fix `UnsafeMutableRawPointer` type conversions
2023-06-23 08:20:14 +01:00
Franz Busch 8e801f12ff Align ServerBootstrap bind methods with the initializer style (#2443)
* Align ServerBootstrap bind methods with the initializer style

# Motivation

After experimenting with the bind/connect APIs of the other bootstraps, we realized that passing the type of the handler is not working very well and choose to add an initializer closure to all the bind/connect methods which returns a concrete result.

# Modification

This PR aligns the `ServerBootstrap` APIs with the new initializer based approach. To make this work I had to refactor the `NIOAsyncChannel` SPIs a bit. Overall, this is a net reduction in code.

# Result

We now have aligned bootstrap APIs.

* Code review

* Fix CI
2023-06-22 17:43:32 +01:00
Franz Busch e865f6163f Use #if canImport(Darwin) where possible (#2446) 2023-06-22 10:12:01 +01:00
Franz Busch 0cdbb1fb38 Add NIOAsyncChannel based connect and bind methods to DatagramBootstrap (#2439)
* Add NIOAsyncChannel based connect and bind methods to DatagramBootstrap

# Motivation
In my previous PR, I added new `connect` methods to `ClientBootstrap` that vend `NIOAsyncChannel` or support an async protocol negotiation. This PR focuses on adding new `connect` and `bind` methods to `DatagramBootstraps` which offer the same functionality.

# Modification
This PR adds new `connect` and `bind` methods that either vend a `NIOAsyncChannel` or an asynchronous protocol negotiation result.

# Result
We can now bootstrap UDP channels with `NIOAsyncChannel`s

* Code review

* Add self

* Remove so_reuseaddr

* Add sleep between client and server bootstrap
2023-06-19 11:10:21 -07:00
Franz Busch 46c0538253 Add NIOAsyncChannel based connect methods to ClientBootstrap (#2437)
* Add NIOAsyncChannel based connect methods to ClientBootstrap

# Motivation
In my previous PR, I added new `bind` methods to `ServerBootstrap` that vend `NIOAsyncChannel` or support an async protocol negotiation. This PR focuses on adding new `connect` methods to `ClientBootstrap` which offer the same functionality.

# Modification
This PR adds new `connect` methods that either vend a `NIOAsyncChannel` or an asynchronous protocol negotiation result. To make this work I had to change the `HappyEyeballs` resolver so that it can return a generic value on resolving. Lastly, I adapted the bootstrap tests to use the new `ClientBootstrap` capabilities which now demonstrate a client/server protocol negotiation dance.

# Result
We can now bootstrap TCP clients with `NIOAsyncChannel`s

* Reduce code duplication

* Create a new set of APIs to tunnel an arbitrary Sendable payload through the inits

* Pass EL to closure

* Fix documentation
2023-06-06 03:36:53 -07:00
ser 6213ba7a06 Pooled control message storage. (#2422) 2023-05-31 06:06:23 -07:00
George Barnett d5519dba0b Fix happy eyeballs races with custom resolver (#2436)
Motivation:

The HappyEyeballs connector synchronises state on an event loop but
calls out to a 'Resolver' to do DNS lookups. The resolver returns
results as a future which may be on a different loop than the connector.
The connector does not hop back to its own event loop before processing
the results.

For client bootstraps, if no resolver is specified then the default
resolver uses the same event loop as the connector so in many cases this
is not an issue. However, if a custom resolver is used this guarantee is
lost and data races are much more likely.

Modifications:

- Hop back to the connector's event loop after calling the resolver.
- Add a test.

Result:

Fewer data races.
2023-05-31 11:28:51 +01:00
Franz Busch 27620851c4 Add narrative documentation for NIO's concurrency bridges (#2423)
* Add narrative documentation for NIO's concurrency bridges

# Motivation
We started to add bridges from NIO to Swift Concurrency and we should provide some narrative documentation for our users how to adopt them.

# Modification
Adds a new article that explains the NIO concurrency bridges.

* Address smaller review comments

* Rearrange sections and introduce general guidance section

* remove some commas

* Add missing catch
2023-05-22 18:40:16 +01:00
Felix Schlegel 47b6289d93 Embedded: getOption(ChannelOptions.allowRemoteHalfClosure) should not fatalError (#2429)
* Embedded: getOption(.allowRemoteHalfClosure) -> OK

Motivation:

In `swift-nio-ssl`, I am currently working on allowing half-closures
which relies on querying the underlying channel if
`ChannelOptions.Types.AllowRemoteHalfClosureOption` is enabled. As a lot of
`swift-nio-ssl`'s tests rely on `EmbeddedChannel` and it did not support
this option, a lot of the tests failed.

Modifications:

* add a `public var allowRemoteHalfClosure` to `EmbeddedChannel`
* enable setting/getting
  `ChannelOptions.Types.AllowRemoteHalfClosureOption` in
`EmbeddedChannel` (only modifies the `allowRemoteHalfClosure` variable
* add test for new behaviour

* AsyncTestingChannel: getOption(.allowRemoteHalfClosure) -> OK

Motivation:

`AsyncTestingChannel` interface should be in step with `EmbeddedChannel`
interface. Therefore also add support for the
`AllowRemoteHalfClosureOption`

Modifications:

* add a `public var allowRemoteHalfClosure` to `AsyncTestingChannel`
* enable setting/getting
  `ChannelOptions.Types.AllowRemoteHalfClosureOption` in `AsyncTestingChannel`
  (only modifies the `allowRemoteHalfClosure` variable
* add tests for new behaviour

* Synchronize access to allowRemoteHalfClosure

Modifications:

* add `ManagedAtomic` property `_allowRemoteHalfClosure` to
  `EmbeddedChannelCore`
* make sure that access to `allowRemoteHalfClosure` from
  `AsyncTestingChannel` and `EmbeddedChannel` is synchronized by
  accessing underlying atomic value in `channelcore`

* Update allocation limits
2023-05-19 09:47:47 -07:00
Cory Benfield 2d8e6ca36f Tolerate sending data after close(mode: .output) (#2421)
Motivation

We shouldn't crash on somewhat likely user error.

Modifications

Pass on writes after close(mode: .output) instead of crashing.

Result

User code is more robust to weird edge cases.
2023-05-09 10:52:48 +01:00
dkz2 7f4d10bd94 addition of assertSuccess() and assertFailure() on EventLoopFuture (#2417)
* added assertSuccess() and assertFailure()

* test functions for assertSuccess() and assertFailure()

* removed callbacks approach and add precondition variants

* added test for precondition variants

* self.always to handle future w/o creating a promise, added fileID and line for debug

* updated test for asserts on EventLoopFuture
2023-05-04 17:25:18 +01:00
Fabian Fett 546eaa261e Add unprocessedBytes property on NIOSingleStepByteToMessageProcessor (#2419) 2023-05-03 17:19:13 +01:00
Gwynne Raskind 014812aab6 Make ByteBuffer CustomDebugStringConvertible (#2418) 2023-05-03 03:18:34 -07:00
Fabian Fett d1690f8541 NIOThrowingAsyncSequenceProducer throws when cancelled (#2415)
* NIOThrowingAsyncSequenceProducer throws when cancelled

* PR review
2023-04-28 08:21:15 -07:00