The inclusion of `cpp_magic.h` in `CNIOAtomics.h` is unnecessary and pollutes the namespace of anyone else importing the header. NIO now also requires Swift 5, which itself will not be built with a Clang too old to understand `_Nonnull`. Note: Removing the extra import of cpp-magic.h required moving it to the src/ directory to avoid complaints about its inclusion (or lack thereof) in the module's umbrella header.
Motivation:
Everything should compile on as many platforms as possible.
Modifications:
Guard FileManager.temporaryDirectory behind
`#available(macOS 10.12, iOS 10, tvOS 10, watchOS 3, macCatalyst 13, *)`
instead of just macOS 10.12.
Result:
Tests should build on iOS and friends too.
Motivation:
With multiple MultiThreadedEventLoopGroups, the thread names are often
useless because there can be two NIO-ELT-#0s etc.
Modifications:
Add a unique MultiThreadedEventLoopGroup identifier.
Result:
NIO thread names unique. Side benefit: It should be easier to see if
people cycle through MTELGs like crazy.
Motivation:
Although that's a rare usecase, the Bootstraps must support random
EventLoops. That includes the EventLoops they're called on but also the
EventLoops of the futures returned from the channel initializers.
Modifications:
Make sure we're always on the correct EventLoop.
Result:
Fewer crashes and bugs.
The `atomic_flag` wrappers (structure, `create` helper, and `destroy` helper) are not referenced from anywhere else in the NIO source tree; they can not be invoked, and appear to serve no purpose. Under sufficiently strict compiler settings, they even cause errors due to the functions lacking prototype declarations.
Motivation:
Making CircularBuffer conform to ExpressibleByArrayLiteral enables
directly initializing it from an array literal, which can sometimes
make its use simpler and more natural, for instance:
foo.circularBuffer = [1,2,3] // vs `= CircularBuffer([1,2,3])`
Foo(circularBuffer: [a, b, c]) // vs `: CircularBuffer([a, b, c])`
This is consistent with other standard Swift collections, such as
Set, as well as existing NIO types (eg: WebSocketMaskingKey).
Modifications:
Extend CircularBuffer to conform to ExpressibleByArrayLiteral
Result:
CircularBuffer can now be initialized from array literals
Motivation:
EmbeddedChannel used to only support IOData on its outbound path, that
has been fixed in #763.
Modifications:
Remove the incorrectly documented brokenness.
Result:
More correct docs.
Motivation:
In general, Swift collections and sequences automatically conforms to
Equatable and Hashable when their Element conforms to Equatable and
Hashhable, respectively. This wasn't the case of CircularBuffer, which
was unintuitive and could cause useless boilerplate code.
Modifications:
CircularBuffer has been extended so that it conforms to Equatable when
its Element is Equatable, and to Hashable when its element is Hashable.
Corresponding test cases have been added.
Result:
Appropriate conditional conformances of CircularBuffer to Equatable and
Hashable
Motivation:
NIOThreadPool was grossly abusing Dispatch to block a large number of
threads for (usually) blocking IO work.
Modifications:
Back the NIOThreadPool by NIOThreads (pthreads) instead of
DispatchQueues.
Result:
Less abuse.
Motivation:
In #1091 we fixed the most common case of handlerRemoved being called
multiple times. There was still another (less likely) problem left which
is if a manual removal is triggered from _within_ the handlerRemoved
call from a pipeline teardown, then we would still call handlerRemoved
another time.
Modifications:
Properly guard against handlerRemoved being called twice.
Result:
Fewer bugs.
Motivation:
Despite the fact that we already stopped delivering unsolicited
responses for the HTTP response decoder, there was a window where we
would deliver a second .head for a response that come without a request.
Modifications:
Don't deliver a second .head even if send together with a first, legit
response.
Result:
Being a HTTP client using NIO is now easier.
Motivation:
There is currently a pair of windows in which it is possible for the
HTTPServerUpgradeHandler to buffer some data without replaying it. This
can happen if it is reentrantly called just after it finishes
unbuffering data, which is not great.
To avoid this, we move to execute this data delivery while we're
removing ourselves. This ensures that we are confident that we cannot be
reentrantly called after our last outcall, meaning we can be confident
of 100% data delivery.
Modifications:
- Moved delayed data delivery to removeHandler.
Result:
Less chance of quiet data loss
* fix double remove of handlers
Motivation:
Previously, it was possible that a handler was removed twice: Once by
Channel teardown and another time by a user-trigger removal that wasn't
instantaneous. This racy situation can happen in the real world. NIO
behaved wrongly in two ways:
1. we would call `handlerRemoved` twiced
2. ByteToMessageHandler would fail an assertion about the current
removal state
Modifications:
- Only call `handlerRemoved` when the handler actually gets removed.
- Fix the assertion.
Result:
fewer bugs
* Update Sources/NIO/ChannelPipeline.swift
Co-Authored-By: Cory Benfield <lukasa@apple.com>
Motivation:
Without niling out it may linger around once the last test has
completed, and keep some messages in memory which will not be inspected
anymore
Modifications:
nil out the writeRecorder same as all other fields are currently nilled
out
Result:
In case XCTest keeps the class around, not keeping around the messages
stored by the writeRecorder
* Fix hanging thread when shutting down an already closed EventLoopGroup
* Generate Linux tests
* Remove accidental shutdownLock.unlock
* Move dispatching of callbacks out of lock
* Use Lock.withLock instead of manual lock/unlock
* Remove superfluous lock.unlock
Motivation:
To give visibility to recently added examples.
To satisfy issue #465
Modifications:
Added links to examples in readme.
Slightly re-arranged the order so that echo examples are next to each other in the list.
Result:
You can now see the UDP, HTTP and WebSocket client examples from the README file.
Motivation:
Right now NIO clients will crash in the (admittedly unlikely) event that
a HTTP server sends an unsolicited extra response on a connection. This
seems pretty unnecessary, and fairly graceless.
Modifications:
- Replace a forcible pop with an optional one.
- Added NIOHTTPDecoderError type.
Result:
Removed a crash that we definitely don't want.
Motivation:
To provide an example of the usage of the WebSocket client upgrade functionality.
To satisfy issue #465
Modifications:
Added a ‘WebSocketClient’ example with a readme and a target in the package file.
Result:
There is now a WebSocket client example to assist with the creation of a WebSocket client.
Motivation:
All ByteBuffer API that is used to parse potentially untrusted data
should be resilient against negative indices, offsets, and lenghts.
Unfortunately, ByteBuffer.viewBytes(at:length:) was not.
Modifications:
Make ByteBuffer.viewBytes(at:length:) resilient against negative values.
Result:
Fewer crashes.
Motivation:
The HTTPServerPipelineHandler is removed from pipelines in many cases,
but the most common case is over upgrade. While the handler is
removable, it doesn't make any effort to ensure that it leaves the
pipeline in a sensible state, which is pretty awkward.
In particular, there are 3 things the pipeline handler may be holding
on to that can lead to damage. The first is pipelined requests: if there
are any, they should be delivered, as the user may be deliberately
allowing pipelining.
The second thing is read() calls. The HTTPServerPipelineHandler exerts
backpressure on clients that aggressively pipeline by refusing to read
from the socket. If that happens, and then the handler is removed from
the channel, it will "forget" to restart reading from the socket on the
way out. That leaves the channel quietly in a state where no reads will
occur ever again, which is pretty uncool.
The third thing is quiescing. The HTTPServerPipelineHandler catches
quiescing events and allows them to deliver a response before closing a
connection. If that has happened when the pipeline handler is removed,
it should fall back to the behaviour as though it were not there.
Modifications:
- Added a handlerRemoved implementation to play event state that should
be replayed.
- Added a channelInactive implementation to drop data.
Result:
More graceful handler removal.
Motivation:
In NIO2 we tried to adopt the Swift convention of avoiding functions
that differ only by the word of their first label where that label is
describing their type. Sadly, we missed one.
Modifications:
- Wrote setBuffer(_:at:)
- Deprecated set(buffer:at:) in favour of the new function.
Result:
Users will get some deprecation warnings I guess.
Motivation:
Subtracting deadlines should just work, no matter if the result is
positive or negative.
Modifications:
Don't crash on earlierDeadline - laterDeadline.
Result:
- fewer crashes
- fixes https://github.com/swift-server/async-http-client/issues/71
Motivation:
The behaviour of multiple writes followed by each other is important so
we should have a unit test for that.
Modifications:
Add a unit test that shows that we just write to where ever the seek
pointer is.
Result:
More test coverage.
Motivation:
To be able to use SHA sums as git versions, we need to pull all the refs
to we get even the commit SHAs from PRs. That's important so CI can use
this script.
Modifications:
Fetch `+refs/pull/*:refs/remotes/origin/pr/*` refs.
Result:
Should be able to be usable in CI across forks.
Motivation:
Folds often involves chaining multiple futures together. It's
beneficial if you're already on the target EventLoop as it might
vastly reduce the amount of EventLoop hops.
Modifications:
Execute onto the target EventLoop before doing the folds.
Result:
Fixes#1054
Motivation:
As #1054 shows, we have some low-hanging performance fruit regarding
off-EventLoop future reduces. We should quantify those.
Modifications:
Add benchmarks to quantify the potential wins.
Result:
More benchmarks.
Motivation:
swift-api-digester requires to be passed `-sdk /` on Linux.
Modifications:
Pass `-sdk /` on Linux and the SDK directory on Darwin.
Result:
check_no_api_breakages works on Linux.
Motivation:
It's illegal to shut down a SelectableEventLoop because the only valid
owner is a MultiThreadedEventLoopGroup which never uses
shutdownGracefully on its EventLoops.
Modifications:
Fail shutdown attempts instead of running some random, untested code.
Result:
Fail illegal calls rather than running some dangerous code which will
lead to deadlocks further down the line.
Motivation:
Previously we thought that if we have some bytes left that belong to an
upgraded protocol, we should deliver those as an error. This is
implemented on `master` but not in any released NIO version.
However, no other handler sends errors on unclean shutdown, so it feels
wrong to do it in this one very specific case (EOF on inflight upgrade
with data for the upgraded protocol)
Modifications:
Remove the error again.
Result:
Less behaviour change to the last released NIO version.
Motivation:
Do as we documented and invoke the leftovers strategy only when the
handler is removed.
Modifications:
Don't invoke leftovers on EOF (which isn't something the user can
control).
Result:
- Fewer crashes
- should fix https://github.com/IBM-Swift/Kitura-WebSocket-NIO/issues/35
One line description of your change
Motivation:
When using the script ad hoc to verify something you may have forgotten to pull all tags,
the script then fails mysteriously in silence and no output. Detective work can then lead the script mysteriously stopping after a checkout:
```
+ cd /tmp/.check-api_1Q1rvO/repo
+ git checkout 1.1.0
$ ...
```
which reminds you to perform a fetch; it would be nicer if it told us about the issue explicitly though.
Modifications:
Don't silence `git checkout` all error output, though use quiet mode so no progress is printed.
Result:
Script fails nicer and reminds you to perform a git fetch if you forgot to do so.
e.g.
```
[5/5] Merging module Metrics
error: pathspec '1.1.0' did not match any file(s) known to git
```
Motivation:
There is a client protocol upgrader but, unlike the server protocol upgrader, it does not have a WebSocket protocol as part of the project.
Modifications:
Made the magic WebSocket GUID public to the WebSockets project.
Added a NIOWebSocketClientUpgrader.
Added tests for the upgrader.
Updated the Linux test script files.
Result:
The project now has a WebSocket client upgrader to match the WebSocket server upgrader.
* Check jq is available before checking API breakages
Motivation:
If jq wasn't installed the API breakage script would erronesouly say
that there were no breaking API changes. That's pretty bad.
Modifications:
Check jq is available before running the check. Updated the usage
message to specify that a Swift 5.1+ toolchain is required.
Result:
More correctness.
Motivation:
EventLoopFuture and EventLoopPromise are symmetric APIs and for easier
discoverability of being able to complete a promise with a future it
is helpful to have a mirror of future.cascade put onto promise.
Modifications:
- Adds ELP.completeWith(ELF)
Result:
It is now possible to complete a promise with a future,
and not only cascade a future to a promise.
Motivation:
Networking software like SwiftNIO that always has explicit flushes
usually does not benefit from having TCP_NODELAY switched off. The
benefits of having it turned on are usually quite substantial and yet we
forced our users for the longest time to enable it manually.
Quite a bit of engineering time has been lost finding performance
problems and it turns out switching TCP_NODELAY on solves them
magically.
Netty has made the switch to TCP_NODELAY on by default, SwiftNIO should
follow.
Modifications:
Enable TCP_NODELAY by default.
Result:
If the user forgot to enable TCP_NODELAY, their software should now be
faster.
Motivation:
Previously, ByteBuffer tried to never copy the bytes when transferring
to a `Data`. For small copies that's definitely not a good choice
because it means an extra allocation.
Modifications:
- introduce `ByteTransferStrategy` so the user can choose
- make a heuristic: copy is less than 256kB, try not to copy above
Result:
More sensible defaults.
Motivation:
We accidentally removed NIOFileHandle(path:) when we added some
arguments with default values to it. This doesn't normally break source,
but in some weird edge cases it can, and it's bad form to remove it.
Modifications:
- Replace the missing function
Result:
Fewer highly unlikely source breakages.
Motivation:
Mean is not a great aggregation function because it's not resilient
against outliers at all, so let's switch to min.
Modifications:
Switch 'mean' to 'min' for aggregation.
Result:
Aggregation function that's more resilient against outliers.
Motivation:
Darwin does sometimes hand us back an (as of the docs) illegal error
code of EINVAL. So far, NIO has done the non-sensical thing of closing
the server socket as a result.
Modifications:
Just close the accepted socket, leave the server socket open, and fire
the error through the pipeline.
Result:
- fixes#1030
- better behaviour
Motivation:
The current test check that the buffer can be written to and then that it is cleared. They do not check that once the buffer is cleared, it can be written to again.
Modifications:
Added a test for both the inbound and outbound buffer to check multiple writes to each buffer.
Result:
We can not be sure that multiple writes to either the inbound or outbound buffers on the embedded channel works.Updated the embedded channel tests to write to the input and output buffers multiple times.