Commit Graph

1385 Commits

Author SHA1 Message Date
Johannes Weiss e102aa9ae9
add PipeChannel (#1138)
Motivation:

There are use-cases for SwiftNIO where there are no actual sockets but
rather two pipe file descriptors - one for input, one for output.
There's no real reason why SwiftNIO shouldn't work for those.

Modifications:

Add a PipeChannel.

Result:

More use-cases for SwiftNIO.
2019-10-16 22:06:55 -07:00
Cory Benfield a796af1a96
Reduce allocations in WebSocketFrameEncoder. (#1161)
Motivation:

The WebSocketFrameEncoder naively allocated a new block to write the
frame header into every time it wrote. This is excessive: in many cases
it would be able to re-use the same buffer as last time.

Modifications:

- Attempt to re-use the buffer we used for the last header.

Result:

Fewer allocations in some applications.
2019-10-15 15:45:26 -07:00
Cory Benfield 55ed652a78
Save the performance tests from the OOM killer (#1164)
Motivation:

A dark force walks abroad our lands. In a place where well-behaved
processes close their blinds, lock their doors, and try not to make
noise late at night, processes that shine just a little too brightly,
consume just a little too much RAM, attract the attention of a dark
force whose name is spoken only in whispers. Some call this force a
myth, but others say that the deniers of this force have whispered dark
incantations, invoking the power word "oom_adj".

It is time we intervened to keep the law-abiding processes in our CI
systems safe.

Modifications:

- Prevent the websocket performance tests comnsuming 4GB of RAM.

Result:

Peace will reign.
2019-10-15 14:03:27 +01:00
Johannes Weiss e540583b70 IntegrationTests: support more curl versions (#1163)
Motivation:

Some curl versions need a hostname even when connecting to UDS.

Modifications:

Always add a host name.

Result:

More compatibility.
2019-10-13 11:28:09 +01:00
Johannes Weiss 3c9bab0f50 additional ByteBuffer(Allocator) test (#1162)
Motivation:

Whilst debugging an async-http-client test bug, I wrote two tests for
ByteBuffer.

Modifications:

Add two tests.

Result:

More tests.
2019-10-11 19:50:59 +01:00
Cory Benfield 37873bb265 Add some websocket encode benchmarks. (#1159)
Motivation:

To improve the performance of a given component it is very helpful to
know how it performs! This patch adds benchmarks and allocation counter
tests to NIO for websocket frame encoding.

Modifications:

- Wrote some benchmarks for WebSocket frame encoding.
- Wrote some allocation counter tests for WebSocket frame encoding.
- Extend the allocation counter tests to properly account for sub-tests

Result:

Better insight
2019-10-11 16:04:08 +01:00
Konrad `ktoso` Malawski 7e16856aff Prefer handlerAdded/handlerRemoved in websocket example (#1158)
Motivation:

Cory recently mentioned that

> handlerAdded is generally a better choice for a "setup" function than
> channelRegistered, as a handler is always guaranteed to receive
> handlerAdded, but is not guaranteed to receive channelRegistered
> (depending on specifics of pipeline setup).

Thus using the better way in examples is good.

Modifications:

- change `channelRegistered` to `handlerAdded`

Result:

- More idiomatic example app
2019-10-10 09:43:43 +01:00
Mario Sangiorgio 23303e7ce7 NIOHTTP1TestServer (#1152)
* NIOHTTP1TestServer implementation

* Test simple request and two concurrent requests

* Accepting multiple connections but keeping only one active at a time

* Linux tests

* Addressed some PR comments
2019-10-02 12:10:09 +01:00
tomer doron b9293596bc
assert EventLoopGroup::syncShutdownGracefully is not called on the event loop (#1151)
motivation: calling syncShutdownGracefully on the event loop can lead to deadlock

changes: assert before calling syncShutdownGracefully that no eventloop context exists
2019-10-01 10:08:35 -07:00
tomer doron f548a29a27
swift 5.1 docker setup (#1146)
motivation: support swift 5.1 release

changes:
* update docker file to use official docker images
* update swift 5.0 and swift 5.1 docker-compose setup to use the official docker images
2019-09-24 12:38:39 -07:00
Johannes Weiss 7767c4f6af
integration tests: remove python dependency (#1145)
Motivation:

The integration tests depended on python in order to print 80,000 x
characters.

Modifications:

Remove the python dependency and express the same with `dd` & `tr`.

Result:

Fewer dependencies.
2019-09-24 10:51:39 +01:00
Johannes Weiss 43413cefac fix thread-safety of BaseSocketChannel's description (#1142)
Motivation:

BaseSocketChannel's description method accessed the (mutable) socket
property of the channel which must only be accessed on the EventLoop.
Description however call be called on any thread.

Modifications:

Don't access the socket from description.

Result:

- more thread-safety
- fixes #1141
2019-09-23 16:14:26 +01:00
Johannes Weiss 8edcd39018 fix one EventLoopGroup leak in tests (#1143)
Motivation:

One test was leaking an ELG.

Modifications:

Don't leak it.

Result:

Fewer leaks in tests.
2019-09-20 19:23:42 +01:00
George Barnett a545d59c47 Fix creating HTTPResponseStatus from 418 (#1140)
Motivation:

I ran across a case in a test where `HTTPResponseStatus(statusCode: 418)`
unexpectedly returned a `.custom` response status.

Modifications:

- Add a missing `case` for creating a status from `418`
- Shuffled some of cases around so that they are correctly listed in the
  correct 2xx, 3xx, 4xx etc. sections

Result:

- `HTTPResponseStatus(statusCode: 418)` now returns `.imATeapot`
2019-09-13 15:28:40 +01:00
Johannes Weiss 8967e0470c README: recommend using Xcode package support (#1137)
Motivation:

Now that Xcode 11 has GM'd, we should recommend the best workflow which
very clearly is Xcode 11's new package support.

Modifications:

Edit README to explain how to use Xcode's package support.

Result:

Happier users.
2019-09-11 06:59:49 +01:00
George Barnett 9201908b54 Allow promises to be completed with a `Result<Value, Error>` (#1124)
Motivation:

When dealing with `Result` types and promises it is inconvenient having to
switch over the result in order to succeed or fail a promise.

Modifications:

- Allow promises to be completed with a `Result<Value, Error>` directly.
- Added tests.

Result:

Promises are easier to fulfill with `Result` types.
2019-09-10 09:25:08 +01:00
Johannes Weiss a22389aa4a
fix NIODeadline/TimeAmount maths (#1136)
Motivation:

Previously the maths on NIODeadline/TimeAmount could sometimes crash and
weren't always right.

Modifications:

Fix the maths and add tests.

Result:

Fewer crashes, more happy times.
2019-09-09 20:24:08 +01:00
Johannes Weiss ab66c1099d
update contributors (#1127) 2019-09-09 18:58:38 +01:00
Johannes Weiss f411d158ce
ChannelPipeline: fail double removal of handler (#1133) 2019-09-09 18:57:57 +01:00
Johannes Weiss 8aa84453d4 HTTPServerUpgradeHandler: Tolerate futures from other ELs (#1134) 2019-09-09 18:03:40 +01:00
Johannes Weiss fbb977d5c2 DEVELOPMENT-SNAPSHOT-2019-09-05-a (#1132) 2019-09-09 10:12:16 +01:00
Johannes Weiss cee3db7072 Lock: guarantee crash instead of undefined behaviour (#1131)
Motivation:

NIO's Lock currently just deadlocks if a thread which already holds a
lock tries to reacquire it. This however isn't even really defined
behaviour.

Modifications:

Switch us to a guaranteed crash from a unguarnateed hang.

Result:

Easier to debug deadlocks with NIO's lock.
2019-09-09 09:50:03 +01:00
Johannes Weiss b6067bd531 enable TSan (#1130)
Motivation:

TSan is now supported in Swift 5.1 for Linux.

Modifications:

Enable TSan on 5.1.

Result:

More validation in the tests.
2019-09-09 09:27:56 +01:00
Konrad `ktoso` Malawski dc92174424 Avoid having `cascade` be title of all methods in jazzy docs (#1129)
Motivation:

Documentation in jazzy looks misleading, listing all methods under
"cascade" since no new MARKs are defined

Modifications:

declare more MARKs, it's repetetive but not wrong now

Result:

Nicer docs
2019-09-04 18:10:48 +01:00
Johannes Weiss 4e1db157cc
DEVELOPMENT-SNAPSHOT-2019-08-29-a (#1125) 2019-09-03 16:55:24 +01:00
Tom Counsell 7cd55dfb69 Fix doc typo: before possible -> are possible (#1122) 2019-08-27 14:38:25 +01:00
Johannes Weiss 32760eae40
BaseSocketChannel: accept immediately closed socket (#1121)
Motivation:

In the grpc-swift test suite, we saw a case where the server would
always immediately close the accepted socket. This lead NIO to misbehave
badly because kqueue would send us the `readEOF` before the `writable`
event that finishes an asynchronous `connect`.

What happened is that we just dropped the `readEOF` on the floor so we
would never actually tell the user if the channel ever went away.

Modifications:

Only register for `readEOF` after becoming active.

Result:

- we're happy with servers that immediately close the socket
2019-08-24 12:23:16 +01:00
tomer doron ae8e2919b6 update conduct email group (#1120)
motivation: use consistent email across swift server projects 

changes: set conduct email address to swift-server-conduct@group.apple.com
2019-08-20 20:29:12 +01:00
Konrad `ktoso` Malawski 0c75974564 Fix dead link to process_test_files.rb in NOTICE.txt (#1119)
Previous link is dead, so we might want to point to a specific commit that introduced what our scripts are based on.
2019-08-20 14:56:34 +01:00
Johannes Weiss e38f1b695a apply suggestions from #1117 (#1118)
Motivation:

@ravikandhadai suggested some minor changes in #1117 which we should
apply.

Modifications:

Apply all suggested changes all over the code base.

Result:

- fixes #1117
2019-08-20 09:24:58 +01:00
Johannes Weiss 5224cfbfd7
DEVELOPMENT-SNAPSHOT-2019-08-15-a (#1113) 2019-08-16 19:05:29 +01:00
Gwynne Raskind 790827800d Remove `cpp_magic.h` import and ancient Clang compatibility hack from `CNIOAtomics.h` (#1111)
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.
2019-08-15 10:54:43 +01:00
Johannes Weiss 415b48e216
more compatibility with other platforms (#1110)
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.
2019-08-14 18:49:13 +01:00
Johannes Weiss 04831e62c5 MTELG: Add EventLoopGroup identifier (#1109)
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.
2019-08-14 10:07:07 +01:00
Johannes Weiss eb5d26b595
bootstraps: tolerate random EventLoops (#1108)
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.
2019-08-14 08:49:48 +01:00
Gwynne Raskind 584e107928 Rename `BOOL` to `BOOLIFY` (#1107)
Prevents conflicts when this header is included in a project using Objective-C.
2019-08-13 09:46:51 +01:00
Gwynne Raskind ab9b8eb701 Remove unused wrapper functions for atomic_flag (#1106)
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.
2019-08-13 09:17:40 +01:00
Trevör a28a8668b5 Conform CircularBuffer to ExpressibleByArrayLiteral (#1102)
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
2019-08-12 15:44:34 +01:00
Johannes Weiss dcb3099b77 docs: EmbeddedChannel isn't as broken as it claims (#1103)
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.
2019-08-12 12:20:47 +01:00
Trevör d30aa6a9f1 Add conditional conformances of CircularBuffer to Equatable and Hashable (#1101)
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
2019-08-11 17:06:07 +01:00
Johannes Weiss 8c6c83c05d NIOThreadPool: backed by NIOThreads (pthreads) instead of DispatchQueues (#1100)
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.
2019-08-09 15:20:53 +01:00
Will Lisac 26586835fd Fail promise in MessageToByteHandler write error cases (#1096)
* Fail promise in MessageToByteHandler write error cases

* Add test case for MessageToByteHandler failing write promises
2019-08-08 10:33:00 +01:00
Johannes Weiss b564a726a9 really no double handlerRemoved calls (#1094)
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.
2019-08-06 18:10:53 +01:00
Johannes Weiss f98ed180e5
HTTPDecoder: don't deliver unsolicited responses (#1093)
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.
2019-08-06 17:39:38 +01:00
Cory Benfield 5d54458488 Deliver HTTPServerUpgradeHandler data on removal. (#1092)
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
2019-08-05 16:07:19 +01:00
Johannes Weiss b07efecaea fix double remove of handlers (#1091)
* 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>
2019-07-31 16:18:15 +01:00
Konrad `ktoso` Malawski 0225a2bf22 nil out writeRecorder, same as all other fields in teardown (#1090)
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
2019-07-31 09:34:06 +01:00
Dario Rexin 5cd9a7faa8 Add comments to the handling in MultiThreadedEventLoopGroup.shutdownGracefully (#1089) 2019-07-30 17:52:42 +01:00
Dario Rexin 0dc794498f Fix hanging thread when shutting down an already closed EventLoopGroup (#1085)
* 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
2019-07-30 17:18:10 +01:00
Liam Flynn 4fa0921e61 Update the README file to include some recently added examples. (#1087)
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.
2019-07-30 11:31:13 +01:00