Commit Graph
2945 Commits
Author SHA1 Message Date
Peter AdamsandCory Benfield 5779ddd54b Extra information to contributors (#1491)
Motivation:

The more contributors know what's expected of them upfront the less likely they are to consume reviewer time and experience a harsh review.

Modifications:

Add some extra commentary to CONTRIBUTORS.md
Add a docker compose example with specific ubuntu/swift versions to README.md

Result:

Contributors will be more informed.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-05-16 11:02:27 +01:00
Gautier DelormeandJohannes Weiss 0695662d7d fix cores count in containers (#1518)
Motivation:

The current System.coreCount implementation relies on _SC_NPROCESSORS_ONLN so isn't cgroups aware which might have a bad impact for apps runnings in containers (e.g. Docker, Kubernetes, Amazon ECS...).

Modifications:

- Changed System.coreCount on Linux only to make it read from CFS quotas and cpusets when present.
- Removed incorrect precondition in Sources/NIO/LinuxCPUSet.swift

Result:

System.coreCount returns correct values when apps run in containers.

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-05-15 10:09:35 +01:00
Johannes Weiss 36bf78a1e9 fix linker check test (#1519)
Motivation:

The linker test script that checks what libraries we link was totally
broken.

Modifications:

Fix the linker test script.

Result:

We can correctly determine what we link and what not.
2020-05-13 20:48:57 +01:00
George Barnett 90f8e73d9b Add a note about failing child channel initializers (#1516)
Motivation:

It's not obvious what happens when `childChannelInitializer` returns a
failed future.

Modifications:

Add a note to the `childChannelInitializer` documentation.

Result:

Better docs.
2020-05-13 18:29:29 +01:00
Johannes Weiss 1df9ad3402 survive having disableSIGPIPE fail (#1515)
Motivation:

We know that on Darwin (eg. when the other end of a TCP connection reset
_really_ early on) F_SETNOSIGPIPE sometimes fails. We thought we could
handle it but we weren't able to handle everywhere.

Modifications:

- make us survive it everywhere
- tests

Result:

- Fewer crashes with ClientBootstrap.withConnectedSocket
2020-05-13 13:07:35 +01:00
Peter Adams c5fa0b4565 Allocation diffing script no longer drops data. (#1513)
Allocation diffing script no longer drops data.

Motivation:

Looking at allocations is hard enough without the script getting the allocation counts wrong.

Modifications:

Where multiple allocations resolved to the same "key" only the last allocation was accounted for. I have changed the script to store all allocations - still keyed in the same way. When diffing the total number of allocations is used - if they are different the total and all contributing stack traces are output.

Added a total for all allocations everywhere at the end together with a difference number.

Output all stack traces before and after contributing to a detected diff.

Update the documentation to reflect changes.

Change the threshold for reporting diffs from > 1000 to >= 1000. This means if allocations go from 1000 to 2000 they are reported as a difference rather than a new allocation.

Result:

It is now easier - if somewhat more verbose to compare allocations.
2.17.0
2020-05-12 09:11:56 +01:00
ac55c57408 Add flatScheduledTask() methods to EventLoop (#1497)
Motivation:

Currently the best, or at least simplest, way to schedule a delayed asynchronous task and wait on its complete result is something like `eventLoop.scheduleTask(in: ...) { return somethingAsync(...) }.futureResult.flatMap { $0 }`. This is not only unobvious but also a bit ugly.

Modifications:

I have added methods which correspond to scheduleTask() in the same way flatSubmit() does to submit(). The returned Scheduled<T> object allows cancellation of the asynchronous task's execution in the same way that is possible for synchronous scheduled tasks, and the futureResult does not complete until the entire asynchronous task does. Callers wishing to wait only for submission of the delayed task, rather than its overall completion, may continue to use the existing scheduleTask() APIs to do so. Tests have been added to verify that the behavior of the new methods is as expected.

These changes are purely additive.

Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-05-11 15:47:05 +01:00
Johannes WeissandCory Benfield 5821c53ee8 allow completely single-threaded NIO programs (#1499)
Motivation:

Usually, you spawn a new MultiThreadedEventLoopGroup to run NIO
programs. But if you're a very simple command line utility, that may not
be necessary, so why not just re-use the main thread?

Modifications:

Allow taking over threads and making them EventLoops.

Result:

Fewer threads if you want.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-05-11 15:33:02 +01:00
Cory Benfield 30265d69a8 Adjust names of BSDSocket option values. (#1510)
Motivation:

The names of most of the BSD socket option values were brought over into
their NIO helper properties, with their namespace removed. This vastly
increases the risk of collision, particularly for things like TCP_INFO,
which just became .info.

Modifications:

- Added the prefixes back, e.g. `.info` is now `.tcp_info`.
- Renamed socket type `.dgram` to `.datagram`, as this is the nice clean
  API and we should use nice clean names.

Result:

Better APIs
2020-05-11 15:13:58 +01:00
Gwynne Raskind add3e402b5 Add inverse of preconditionInEventLoop() and ability to add messages to the assertion (#1508)
Motivation:

It is valuable to be able to provide additional context for any kind of assertion failure, such as obvious points of API misuse or when the purpose of the assertion itself is unclear.

Modifications:

Added an overload of preconditionInEventLoop() that accepts a message autoclosure parameter in the same fashion as `Swift.precondition(_:file:line:)`. Default the implementation of the original version to calling the new one with an empty message. Add defaulted implementation of the new version which forwards the message closure to precondition(). Add the new message parameter directly to assertInEventLoop() without a forwarder since it is not a customization point.

Result:

It is now possible to optionally provide explanatory messages to be included when the "in event loop" precondition fails, as with any other precondition.
2020-05-11 14:07:56 +01:00
eb14015938 Create new write repeating byte method (#1500)
* Enable fast writing of repeated bytes

Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: George Barnett <gbrntt@gmail.com>
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-05-07 13:14:35 +01:00
Shekhar Rajak 71f7a0d172 code reuse for HTTPReq/Res isKnownUniquelyReferenced checks (#1507)
Motivation:

Fixes: #1504

Modifications:

Refactor: private mutate func copyStorageIfNotUniquelyReferenced is defined.

Result:

Refactored.
2020-05-07 12:51:03 +01:00
Peter AdamsandCory Benfield c8e253206b Add Optional ECN MetaData to Addressed Envelope (#1502)
Add Optional ECN MetaData to Addressed Envelope

Motivation:

This is a very small step along the path to #1472

Modifications:

Add an optional metadata field to addressed envelope. Currently the only possible data is ECN information. This is not currently populated anywhere.

Result:

Metadata in place for future work.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-05-05 10:30:17 +01:00
Johannes Weiss 79f0d16004 update contributors (#1505) 2020-05-04 16:06:32 +01:00
9afac5ecda UDP based allocation tests (#1496)
UDP based allocation tests (#1496)

Motivation:

With future work being considered in the UDP space it will be useful to have a baseline measure of allocations.

Modifications:

New allocation tests for UDP -
Ideas inspired by the TCP allocation tests.
Code a modified version of UDPEchoServer and UDPEchoClient examples.

Result:

There are UDP based allocation tests.

Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-05-04 11:36:23 +01:00
Peter Adams c30cf9c50f close channel after data is received. (#1501) 2020-05-04 11:13:00 +01:00
David Evans 8da802c5eb Add 5.3 CI (#1498) 2020-05-01 19:17:44 +01:00
Cory Benfield d536e6619c Make some vars lets. (#1493)
Motivation:

Newer Swift 5.3 snapshots have spotted more vars that could be lets.

Modifications:

Made two vars lets.

Result:

The word `let` appears more frequently; the word `var`, less so.
2020-04-30 15:24:47 +01:00
Johannes Weiss 197664726f link swift-nio-extras in docs (#1483) 2020-04-17 18:14:15 +01:00
Johannes Weiss 397343b6a0 add forgotten universal bootstrap docs (#1482) 2020-04-17 18:13:49 +01:00
Peter Adams e8ec02602c Remove a warning from the performance test build. (#1481) 2020-04-16 18:22:53 +01:00
Johannes Weiss bf48c2beee make the allocation counter tests more stable (#1476)
Motivation:

The core problem is that we hook the system's allocator (malloc, free,
and friends) to get the most accurate allocation counts. This however
also means that all threads count into our limits which inherently means
we can't be 100% stable always.

Previously, we just waited a fixed 100ms and expected everything to be
as quiesced as it'll get. But we can do better.

Modifications:

- discard runs with negative remaining allocations (clearly bogus,
  quiescing not long enough in a previous run)
- instead of waiting a fixed amount of time, wait until the results
  appear quiesced (stable (for first run) or zero (for subsequent runs)
  remaining allocation counts.

Result:

Allocation counter tests should be much more stable on busier machines.
2020-04-08 14:00:26 +01:00
Peter Adams a91c87f1c2 Correct doc comment on writeEOF (#1474)
Motivation:

Make the documentation correct.

Modifications:

Changed the word read to write in comments.

Result:

Doc comment only change to be correct.
2020-04-08 07:42:38 +01:00
Saleem Abdulrasool c7ab09013d Add BSDSocket.AddressFamily (#1469)
This finishes up the encapsulation that is required to ensure that we
are not leaking libc interfaces to clients but instead vend an
enumeration that they can use.  The enumeration is open so although we
may not have the full interface enumerated, users will not be prevented
from using alternate values.
2020-04-07 22:17:30 +01:00
Saleem Abdulrasool 2e2dcc25ca Add BSDSocket.ProtocolFamily (#1468)
This encapsulates the enumeration of protocol families rather than
relying on the underlying C library to provide the value.
2020-04-07 11:45:20 +01:00
Johannes Weiss 438d6436fd fix accidental deprecation (#1470)
Motivation:

In one of the previous patches, we accidentally merged the deprecation
of `ChannelOptions.Types.SocketOption.init` which will break a lot of
NIO programs.

Modifications:

Undo the deprecation.

Result:

NIOTS builds without warnings.
2020-04-04 17:01:08 +01:00
Saleem Abdulrasool 37b48094ba Add BSDSocket.SocketType to represent the socket type (#1467)
This adds a new enumeration which encapsulates the socket type values.
This enables ports to provide the same surface with a different name as
necessary.
2020-04-03 17:16:48 +01:00
Saleem AbdulrasoolandCory Benfield bcc180dab6 Add new BSDSocket namespace (#1461)
* NIO: Add new `BSDSocket` namespace

This starts the split of the POSIX/Linux/Darwin/BSD interfaces and the
BSD Socket interfaces in order to support Windows.  The constants on
Windows are not part of the C standard library and need to be explicitly
prefixed.  Use the import by name to avoid the `#if` conditions on the
wrapped versions.  This will allow us to cleanly cleave the dependency
on the underlying C library.

This change adds a `BSDSocket.OptionLevel` and `BSDSocket.Option` types
which allow us to get proper enumerations for these values and pipe them
throughout the NIO codebase.

* Apply suggestions from code review

Co-Authored-By: Cory Benfield <lukasa@apple.com>
2020-04-02 19:06:48 +01:00
Johannes Weiss e876fb3741 bootstraps: offer ELG validation (#1464)
Motivation:

Today, we just expect the ELGs passed to the bootstraps to be the
correct ones, if not, we crash.

Modifications:

Offer an alternative `validatingGroup:` `init` that just returns `nil`
if the ELGs are of the wrong types.

Result:

Easier to work with multi-stack systems for example when the user might
pass an ELG for either NIO on Sockets or NIO on Network.framework.
2.16.0
2020-04-01 13:57:48 +01:00
Johannes Weiss b2d937b22f shorten NIOThreadPool's thread names (#1466)
Motivation:

Previously, NIOThreadPool would use super long thread names. Linux
doesn't allow more than 15 bytes (+ \0 char) so in the end they would
never get set at all.

Modifications:

- Shorten NIOThreadPool's thread names
- Only take the first 15 characters of thread names in general on Linux

Result:

We don't lose thread names on Linux.
2020-03-31 20:01:56 +01:00
David Evans 6b0ed24408 Convenient initialisers from ByteBuffer (#1457) 2020-03-31 09:23:25 +01:00
Saleem AbdulrasoolandJohannes Weiss 6cac95501b Selector: further default to Linux behaviour (#1447)
This furthers the behaviour uniformity of special casing behaviour for
other platforms (e.g. Darwin, Windows) and defaulting to the Linux
behaviour as the rest of the code base assumes that the Linux behaviour
is a reasonable fallback.  Ensuring that the paths taken are always
uniformly taken helps identify the changes necessary for a port and make
it easier to reason about the changes.

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-03-26 08:16:28 +00:00
Saleem Abdulrasool 5270fa81df IO: extend IOError for Windows (#1460)
There are three different error domains that we deal with in NIO, which
need to be differentiated.  Store the error with the domain to ensure
that we can identify the error domain.
2020-03-23 16:29:14 +00:00
Saleem Abdulrasool 91bde186b2 CNIOWindows: add paranoid preprocessing (NFC) (#1459)
Ensure that this file is only compiled for Windows targets.
2020-03-21 18:20:14 +00:00
Saleem AbdulrasoolandCory Benfield ad419b56d4 NIO: add module constructor for Windows (#1450)
This adds the necessary code to add a module constructor for NIO.  On
Windows, the socketing subsystem (WinSock) must be explicitly
initialized unlike Unices.  Add a module constructor that will be
handled by the C runtime to initialize the subsystem before the module
code is executed so that the user does not need to worry about this.

Since there is no way to inject this in Swift, write a tiny helper in C
and link that into the final NIO binary.

Since swift-package-manager does not support mixed-language directories,
add this to `CNIOWindows` which will need to be statically linked and
must always be imported as `@_implementationOnly` as this cannot be
linked into any user libraries.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-03-21 17:51:16 +00:00
Johannes Weiss a27a07719c testSafeToExecute*: fix threading issues (#1455)
Motivation:

testSafeToExecuteTrue/False accessed an internal property of the event
loop without synchronisation.

Modifications:

Fix the threading issue.

Result:

Tests with TSan happy.
2.15.0
2020-03-18 22:36:08 +00:00
Johannes Weiss bd4df32a26 downgrade 'schedule after shutdown' to printing error (#1454)
Motivation:

Since #1395, we crash when something is scheduled to an already shutdown
EventLoop. That's a good intent (#1383) but we feel it's a little too
risky right now.

Modifications:

Print an ERROR instead of just crashing.

Result:

Fewer crashes.
2020-03-18 18:20:35 +00:00
Johannes WeissandCory Benfield 2ad77f9a9b WIP: universal bootstrap (#1323)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-03-18 16:20:19 +00:00
Saleem Abdulrasool 834a5f2185 NIO: split out FileDescriptor and Selectable (#1444)
A `FileDescriptor` is not necessarily selectable (e.g. Windows), and a
selectable is not necessarily a `FileDescriptor` (e.g. Windows).

This will allow a basic `Selector` on Windows which will use `WSASelect`
to select on sockets for some basic functionality even though
IOCompletionPorts are preferred.
2020-03-18 15:48:40 +00:00
Saleem Abdulrasool 2673be6ae3 NIO: move comment to right location (NFC) (#1452)
Move the block comment to the right location before the function it
describes.
2020-03-17 09:31:00 +00:00
d3d7589a29 NIO: implement core counting on Windows (#1448)
* NIO: implement core counting on Windows

Implement the core counting logic for Windows, which does not support
`sysconf`.  It uses the Windows SDK to get the processor topology and
computes the core count from the topology.

Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-03-16 18:41:58 +00:00
Ludovic DewaillyandLudovic Dewailly fefd0c854f Provides a means to control the underlying header storage capacity. (#1451)
Motivation:

It's not always possible to initialize `HTTPHeaders` with all the headers one needs to set. In such case, the underlying array will be expanded as and when needed upon headers being appended. `Array` provides the means to reserve capacity to optimize such use case. This PR exposes this functionality in `HTTPHeaders`.

Modifications:

Expose method `reserveCapacity(_ minimumCapacity: Int)` and computed variable `capacity: Int` in `HTTPHeaders` and added some tests.

Result:

Developers will be able to control the capacity of the underlying headers array as such:

```
var headers = HTTPHeaders()
headers.reserveCapacity(5)
```

Co-authored-by: Ludovic Dewailly <ldewailly@apple.com>
2020-03-16 17:47:34 +00:00
Saleem Abdulrasool e5298263ec SelectableEventLoop: make Darwin the special case (#1449)
Treat the `autoreleasepool` case for Darwin as the special case rather
than Linux as the special case.  This is following the case throughout
the rest of the code base.  This is also needed for the Windows port.
2020-03-16 09:14:21 +00:00
Johannes Weiss ad0c671acf make-single-file-spm: better directory name (#1446)
Motivation:

The make-single-file-spm script used to use a hidden folder in /tmp
which isn't very helpful when tar'ing up the result for a bug report.

Modifications:

Switch to a non-hidden folder.

Result:

Easier bugs.swift.org reports.
2020-03-13 17:01:37 +00:00
Saleem AbdulrasoolandCory Benfield 648a6712ff NIO: unify the syscall wrapper (#1423)
The path was duplicated because the compiler was not previously able to
optimize with the constant value and would generate poor code.  This has
now been addressed, so unify the blocking and non-blocking paths.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-03-12 15:11:12 +00:00
Saleem Abdulrasool 790698cd54 NIO: swap selector implementations (NFC) (#1443)
In most of the code, Linux is treated as the fallback case.  This
adjusts the selector implementation to follow the same pattern, checking
if on Darwin and using kqueue (which would also be available on F/BSD)
and otherwise falling back to `epoll` (assuming Linux).  This
homogeneous behaviour makes it easier to reason about when trying to add
support for additional platforms.
2020-03-12 07:43:33 +00:00
Saleem Abdulrasool f44d524d56 NIO: use Posix "namespace" more aggressively (#1439)
Convert many more cases of the raw address family constant to the
definition in the System version.
2020-03-11 17:31:53 +00:00
Kaitlin Mahar a8e08d9757 BlockingIOThreadPool -> NIOThreadPool (#1438)
Update NonBlockingFileIO.init documentation to match the new type name.
2020-03-11 08:35:39 +00:00
Saleem Abdulrasool 7103febd74 NIO: undo exclusion of UDS on Windows (#1437)
This is in-scope for Windows now.
2020-03-09 17:38:10 +00:00
35078406c7 CNIOSHA1: make the code more portable (#1399)
Replace the non-portable `u_*_t` types with the C standard `u*_t` types
instead.  Remove the Unix headers as the only headers really needed are
the C standard headers.  Provide shims for deprecated `strings.h`
functions in terms of "modern" (C89+) functions from `string.h`.  Move
an android only include into the implementation from the interface.

This enables building this library on Windows.

Co-Authored-By: George Barnett <gbrntt@gmail.com>
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-03-09 16:15:38 +00:00