Commit Graph

2169 Commits

Author SHA1 Message Date
Franz Busch 2c453d6e49
Small changes for the `NIOAsyncSequenceProducer` (#2254)
* Small changes for the `NIOAsyncSequenceProducer`

# Motivation
In the PR for the `NIOAsyncWriter`, a couple of comments around naming of `private` properties that needed to be `internal` due to inlinability and other smaller nits came up.

# Modification
This PR includes two things:
1. Fixing up of the small nits like using `_` or getting the imports inside the `#if` checks
2. Changing the public API of the `makeSequence` to be aligned across the throwing and non-throwing one.

# Result
Cleaner code and alinged APIs.

* Fix refactoring left-overs
2022-08-30 16:53:00 +01:00
carolinacass c7bfda0cff
Add EventLoopFuture.makeCompletedFuture(withResultOf:) (#2253)
* Add EventLoopFuture.makeCompletedFuture(withResultOf:)

* add linux tests

Co-authored-by: Carolina Cassedy <ccassedy@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-08-30 04:07:27 -07:00
David Nadoba 1100054107
Functions passed to non-`Sendable` `ChannelHandler`s do *not* need to be `Sendable` (#2249) 2022-08-26 17:59:34 +02:00
David Nadoba 9d6a041b12
Define `Array` element type explicitly to fix nightly CI (#2250)
Fixes a new warning with the swift `main` toolchain: empty collection literal requires an explicit type
2022-08-24 17:48:42 +01:00
David Nadoba 2636425120
Conform `NIOTooManyBytesError` to `Hashable` (#2246)
Makes it easier to use in tests
2022-08-17 02:06:29 -07:00
Cory Benfield 901e9692b9
Validate missing imports in CI (#2245) 2022-08-15 10:26:34 -07:00
Cory Benfield f5448fbbc2
Provide NIOAsyncTestingChannel (#2238)
Motivation

Testing versions of NIO code that involve interfacing with Swift
Concurrency is currently a difficult business. In particular,
EmbeddedChannel is not available in Swift concurrency, making it
difficult to write tests where you fully control the I/O.

To that end, we should provide a variation of EmbeddedChannel that makes
testing these things possible.

Modifications

Provide an implementation of NIOAsyncTestingChannel.

Results

Users can write tests confidently with async/await.
2022-08-10 11:09:56 +01:00
Franz Busch ff19f496bd
Add throwing version of `NIOAsyncSequenceProducer` (#2237)
* Add throwing version of `NIOAsyncSequenceProducer`

# Motivation
We recently introduced a `NIOAsyncSequenceProducer` to bridge a stream of elements from the NIO world into the async world. The introduced type was a non-throwing `AsyncSequence`. To support all use-cases we also need to offer a throwing variant of the type.

# Modification
- Introduce a new `NIOThrowingAsyncSequenceProducer` that is identical to the `NIOAsyncSequenceProducer` except that it has a `Failure` generic parameter and that the `next()` method is throwing.
- Extract the `StateMachine` from both `AsyncSequenceProducer`s and unify them.
- There is one modification in behaviour: `didTerminate` is now only called after `nil` or the error has been consumed from the sequence.

# Result
We now have a throwing variant of the `NIOAsyncSequenceProducer`.

* Code review and fix CI

* Remove duplicated code

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-08-09 23:34:17 -07:00
tomer doron 92e1e06303
Update soundness.sh (#2240)
motivation: correct exception list to be bash safer

changes: quote expulsion pof custom Package.swift files
2022-08-09 08:16:04 +01:00
tomer doron 712e207f26
initial adoption of DocC based documentaiton (#2235)
* initial adoption of DocC based documentaiton

motivation: use DocC for docs

change:
* add DocC catalog to NIO target, as the "landing page"
* remove jazzy doc generation script
* add Package.swift with tools-version 5.6 so that DocC can be used, and add a dependency on the DocC plugin
* udpate soundness script

* * add packge.swift files for older vrsions
* adjust waning-as-error and enable-test-discovery to the different permutations

* fixup

* remove jazzy

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-08-08 03:55:18 -07:00
George Barnett 6b3f44a43f
Add availability requirements to NIOAsyncSequenceProducer extension (#2236)
Motivation:

One of the `NIOAsyncSequenceProducer` extensions was missing a
availability requirements.

Modifications:

Add missing requirement.

Result:

Fewer bugs.
2022-08-08 11:05:58 +01:00
Franz Busch dd40215fd4
Implement a back-pressure aware `AsyncSequence` source (#2230)
* Implement a back-pressure aware `AsyncSequence` source

# Motivation
We ran into multiple use-cases (https://github.com/apple/swift-nio/pull/2067, https://github.com/grpc/grpc-swift/blob/main/Sources/GRPC/AsyncAwaitSupport/PassthroughMessageSource.swift) already where we want to vend an `AsyncSequence` where elements are produced from the sync world while the consumer is in the async world. Furthermore, we need the `AsyncSequence` to properly support back-pressure.
Since we already identified that this is something fundamental for our ecosystem and that current `AsyncSequence` sources are not providing the proper semantics or performance, it would be great to find a single solution that we can use everywhere.

Before diving into the code, I think it is good to understand the goals of this `AsyncSequence`:
- The `AsyncSequence` should support a single unicast `Subscriber`
- The `AsyncSequence` should allow a pluggable back-pressure strategy
- The `AsyncSequence` should allow to yield a sequence of elements to avoid aquiring the lock for every element.
- We should make sure to do as few thread hops as possible to signal the producer to demand more elements.

# Modification
This PR introduces a new `AsyncSequence` called `NIOBackPressuredAsyncSequence`. The goal of that sequence to enable sync to async element streaming with back-pressure support.

# Result
We can now power our sync to async use-cases with this new `AsyncSequence`.

# Future work
There are couple of things left that I wanna land in a follow up PR:
1. An adaptive back-pressure strategy that grows and shrinks depending on the speed of consumption
2. A throwing version of this sequence
3. Potentially an async version that suspends on `yield()` and resumes when more elements should be demanded.

* Fix cancellation handling

* Review

* Add init helper

* Add return types to help the type checker

* Fix 5.7 CI

* Rename delegate method and update docs

* Review

* Switch to Deque, rename the type and change the behaviour of yielding an empty sequence

* Review comments from George

* Review comments by Konrad

* Review and add tests for the high low strategy

* Fix 5.4 tests

* Code review

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-08-04 15:58:28 +01:00
David Nadoba b4e0a274f7
Fix compilation with Swift 5.5.0 and 5.5.1 (#2234) 2022-08-04 02:22:47 -07:00
David Nadoba ece5057615
Fix strict concurrency checking diagnostics in `MTELG` (#2229) 2022-07-28 11:57:02 +01:00
Cory Benfield bedb9fe613
Enhance and rename AsyncEmbeddedEventLoop (#2224)
* Enhance and rename AsyncEmbeddedEventLoop

Motivation

AsyncEmbeddedEventLoop is an important part of our ongoing testing story
for NIO. However, it suffers from two problems.

The first is a usability one. As we discovered during the original
implementation, following EmbeddedEventLoop's pattern of not having any
EventLoop "Tasks" execute until run() meant that simple constructs like
`EventLoopFuture.wait` and `EventLoopFuture.get` didn't work at all,
forcing us to add an annoying `awaitFuture` method.

When playing with implementing EmbeddedChannel, this got worse, as I/O
methods also don't run immediately if called from the testing thread. We
couldn't easily work around this issue, and it meant that common
patterns (like calling `channel.writeAndFlush`) would deadlock!

This is unacceptable, so a change had to be made.

While we're here, we received feedback that the name is unclear to
users. Given that this particular event loop is in no sense "embedded",
we no longer need the name, so we can take this opportunity to use a
better one.

Modifications

Changed `func execute` to immediately execute its task body, and to
dequeue all pending tasks at this time. Essentially, it's the equivalent
to run(). This is a major change in its behaviour.

Renamed the loop to `NIOAsyncTestingEventLoop`.

Result

Better names, easier to use.

* Make soundness happy

* Remove awaitFuture
2022-07-27 08:03:06 +01:00
Anish Aggarwal d05d2fd125
Added test for BufferView custom contains function (#2227)
* Added test for BufferView custom contains function

* Fixed test case

* testBufferViewContains fixed
2022-07-26 17:04:54 +01:00
Anish Aggarwal dd3b25449a
NIOCore: Implemented all three variants of _failEarlyRangeCheck methods for ByteBufferView (#2226)
* NIOCore: Implemented all three variants of _failEarlyRangeCheck methods for ByteBufferView

* _failEarlyRangeCheck is now no-op
2022-07-25 10:07:40 -07:00
David Nadoba b99da5d3fe
Workaround `Sendable` warning from Swift 5.7 (#2225)
Workaround for https://github.com/apple/swift/issues/59911
```swift
/src/Tests/NIOHTTP1Tests/HTTPTest.swift:148:21: warning: 'buf' mutated after capture by sendable closure
                buf.writeString("\(c)")
```
The variable `buf` is mutated but only before being capture by the sendable closure.

All other warnings/errors are fixed with the latest Swift 5.7 toolchain
2022-07-21 18:20:49 +01:00
Saleem Abdulrasool 48916a49af
NIOCore: handle error description on Windows (#2219)
We would previously attempt to convert an error to a description without
consideration for the provenance on Windows.  This would result in a
failure if the error code was not from `errno`.  Account for the source
of the error and translate it appropriately.  We can not retrieve
descriptions on errors that may originate from Windows or WinSock as
well as the C library.
2022-07-07 13:07:34 +01:00
David Nadoba 8b160c3681
Adopt `Sendable` for `NIOChatServer` and `MIOMuliticastChat` examples (#2220) 2022-07-06 16:30:24 +01:00
David Nadoba f1d62128c4
Adopt `Sendable` for Closures in `NIOWebSocket` (#2218)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-07-05 04:07:44 -07:00
David Nadoba fbe7ef484a
Adopt `Sendable` for Types in `NIOWebSocket` (#2217) 2022-07-05 11:50:55 +01:00
David Nadoba 927b91a1e7
Adopt `Sendable` for closures in `HTTPClientUpgradeHandler.swift` and `HTTPServerUpgradeHandler.swift` (#2216)
* Adopt `Sendable` for closures in `HTTPPipelineSetup.swift`

* make `UnsafeTransfer` and `UnsafeMutableTransferBox` available in Swift 5.4 too

* fix code duplication

* Copy `UnsafeTransfer` to `NIOHTTP1Tests` to be able to remove `@testable` from `NIOCore` import

* Adopt `Sendable` for closures in `HTTPServerUpgradeHandler.swift`

* Adopt `Sendable` for closures in `HTTPClientUpgradeHandler.swift`
2022-07-05 02:42:29 -07:00
David Nadoba 8c922223db
Adopt `Sendable` for closures in `HTTPPipelineSetup.swift` (#2214)
* Adopt `Sendable` for closures in `HTTPPipelineSetup.swift`

* make `UnsafeTransfer` and `UnsafeMutableTransferBox` available in Swift 5.4 too

* fix code duplication

* Copy `UnsafeTransfer` to `NIOHTTP1Tests` to be able to remove `@testable` from `NIOCore` import
2022-07-05 10:01:04 +01:00
David Nadoba 0abf7eb929
Adopt `Sendable` for types in `NIOHTTP1` (#2213) 2022-07-04 00:37:23 -07:00
Saleem Abdulrasool f4b0d661c1
NIOCore: add missing import on Windows (#2215)
We did not import `WinSDK` which provides the socketing interfaces.
This allows building `NIOCore` on Windows again.
2022-07-01 19:01:04 +01:00
David Nadoba 33fc191001
Adopt `Sendable` for `MultiThreadedEventLoopGroup` (#2211)
* Adopt `Sendable` for `MultiThreadedEventLoopGroup`

* remove `@Sendable` from `withCurrentThreadAsEventLoop(_:)`

* Remove trailing whitespace

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-07-01 06:05:54 -07:00
David Nadoba 6c54e11eaa
Adopt `Sendable` in `NonBlockingFileIO` (#2212)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-07-01 04:06:41 -07:00
David Nadoba d6f5e38af2
Adopt `Sendable` for Bootstraps (#2209)
* Adopt `Sendable` for Bootstraps

* fix swift nightly

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-07-01 03:45:07 -07:00
YR Chen a501353ef6
Deprecate `NIOAtomics` in favor of `Atomics` (#2204)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-07-01 02:31:14 -07:00
David Nadoba adda7374af
Adopt `Sendable` for `NIOThreadPool` (#2210)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-30 08:38:09 -07:00
David Nadoba 427d358d03
Adopt `Sendable` for types in `NIOPosix` (#2208) 2022-06-30 16:21:50 +01:00
David Nadoba e650ac1279
Adopt `noasync` for `ThreadSpecificVariable` (#2206) 2022-06-30 11:11:42 +02:00
David Nadoba e2c7fa4d4b
Adopt `Sendable` in `NIOTLS` (#2202) 2022-06-21 14:13:06 +01:00
David Nadoba 48f4e2978f
Adopt `Sendable` in `NIOFoundationCompat` (#2203) 2022-06-21 09:33:04 +01:00
David Nadoba b73fc4e90d
Adopt `Sendable` in `NIOTestUtils` (#2199)
* Adopting `Sendable` in `NIOTestUtils`

The parameter `decoderFactory` of the static methods `ByteToMessageDecoderVerifier.verifyDecoder` do not need to be `@escaping`. I have made them non-escaping as part of `Sendable` adoption because we would otherwise need to think about if they should be `@Sendable` too.

`VerificationError` is interesting, see the code comment for more information.

* Adopting `Sendable` in `NIOTestUtils`

The parameter `decoderFactory` of the static methods `ByteToMessageDecoderVerifier.verifyDecoder` do not need to be `@escaping`. I have made them non-escaping as part of `Sendable` adoption because we would otherwise need to think about if they should be `@Sendable` too.

`VerificationError` is interesting, see the code comment for more information.

* Clarify the reason `VerificationError` already conforms to `Sendable`
2022-06-20 13:23:14 +01:00
David Nadoba f554552edf
Adopt `Sendable` in `NIOEmbedded` (#2195)
`EmbeddedChannel` and `EmbeddedEventLoop` should not be `Sendable`. However, they inherit from `Channel` and `EventLoop` respectively. Therfore, they need to be `Sendable` (and are already implcitily `Sendable`. Given these constraints, we can’t mark `EmbeddedChannel` and `EmbeddedEventLoop` as non-Sendable.
2022-06-17 06:38:06 -07:00
Saleem Abdulrasool f7c3c45df4
NIOPosix: disable `timespec` extension on Windows (#2194)
This extension does not port cleanly to Windows as the time structures
on Windows are different.  This happens to be unused, so simply remove
the extension on Windows.
2022-06-15 10:37:30 +01:00
Si Beaumont 4588691a1a
Provide conversion APIs between TimeAmount and Swift.Duration (#2191)
* Provide conversion APIs between TimeAmount and Swift.Duration

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

* fixup: Remove unnecessary overflow check

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

* fixup: Move into new _NIOBeta57 module

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

* fixup: Rename _NIOBeta57 module to _NIOBeta and move tests to own target

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

* soundness: copyright years for some reason

Signed-off-by: Si Beaumont <beaumont@apple.com>
2022-06-14 14:38:17 +01:00
Si Beaumont 69bb75df32
Throw fatalError when scheduling on shutdown EL if SWIFTNIO_STRICT is set (#2190)
* Throw fatalError when scheduling on shutdown EL if SWIFTNIO_STRICT is set

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

* Add CrashTest for SWIFTNIO_STRICT crash

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

* fixup: Extract env var parsing to static let

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

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-14 05:43:48 -07:00
Saleem Abdulrasool b393d39603
NIOPosix: add missing import for Windows (#2188)
Add an import of `NIOCore` on Windows which mirrors the other platforms.
This greatly reduces the noise in the error list.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-14 04:37:27 -07:00
Cory Benfield 7fbaed6f4a
Use correct alloc limits for 5.7 (#2193) 2022-06-14 11:57:26 +01:00
Saleem Abdulrasool 59cd8ff038
NIOPosix: import additional interfaces from WinSDK (#2189)
Import additional interfaces from WinSDK to enable additional paths to
build for Windows.
2022-06-14 10:56:44 +01:00
Stepan Ulyanin c0911e1b3c
Implement the _failEarlyRangeCheck methods as no-ops (#2161)
* implement the _failEarlyRangeCheck methods as no-ops

* mark _failEarlyRangeCheck with @inlinable

* add a comment on why the _failEarlyRangeCheck methods are implemented as no-ops

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-13 06:10:37 -07:00
Saleem Abdulrasool 3c3c2b8f41
NIOPosix: add missing `CNIOWindows` import (#2184)
Add a missing import to match the other platforms.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-13 05:53:07 -07:00
Cory Benfield ec4072c925
Use 5.7 nightlies (#2186) 2022-06-13 03:06:35 -07:00
Saleem Abdulrasool e0df87c5c1
NIOPosix: adjust precondition for Windows (#2183)
The member names are not identical across platforms.  Add a case to
handle the name difference on Windows.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-10 09:14:27 -07:00
Saleem Abdulrasool 01b13a5675
NIOPosix: reorganise packet info constants (#2182)
Co-locate the packet info constants and add Windows values.
2022-06-10 16:55:51 +01:00
Saleem Abdulrasool 03fa431216
NIOPosix: port file validation to Windows (#2179)
Use Win32 APIs to properly validate if a file is a pipe on Windows.
This enables providing the same semantics without leaking additional
Windows specifics.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-09 23:27:14 -07:00
Saleem Abdulrasool 57d41b502c
NIOPosix: reorganise ECN constants, define for Windows (#2181)
Reorganise the ECN constants to colocate the definitions for the
different platforms.  Define the constants for Windows as the platform
does not provide them in the system headers.
2022-06-10 07:12:26 +01:00