Commit Graph
2140 Commits
Author SHA1 Message Date
Johannes Weiss ff31b3135d improve B2MD EOF handling (#831)
Motivation:

Previously B2MDs didn't really have defined semantics regarding EOFs and
we didn't tell them if there was an EOF. Also `decodeLast` was optional
and all that was bad.

Modifications:

- require `decodeLast`
- add a `seenEOF: Bool` parameter to `decodeLast` which tells the
  decoder if an EOF has been seen

Result:

- clearer semantics
- more information
2019-02-21 14:14:50 +00:00
Johannes Weiss 46ffd630de ChannelPipeline: addHandler and removeHandler instead of add/remove (#817)
Motivation:

- `ChannelPipeline.add(name:handler:...)` had a strange order of arguments
- `remove(handler:)` and `remove(ctx:)` both remove `ChannelHandler`s
  but they read like they remove different things

So let's just fix the argument order and name them `addHandler` and
`removeHandler` making clear what they do.

Modifications:

- rename all `ChannelPipeline.add(name:handler:...)`s to `ChannelPipeline.addHandler(_:name:...)`
- rename all `ChannelPipeline.remove(...)`s to `ChannelPipeline.removeHandler(...)`

Result:

more readable and consistent code
2019-02-21 11:46:54 +00:00
Cory Benfield 5d27e59bd9 Clean up some broken shims. (#830)
Motivation:

A few shims were missing public access modifiers, and at least one
should have been static but wasn't. These would lead to hard compile
errors.

Modifications:

Threw the words "public" and "static" around. Like Java!

Result:

Fewer compile errors, more compile warnings.
2019-02-20 18:45:08 +00:00
Johannes Weiss 5143f0bc17 opaque indices for (Marked)CircularBuffer (#826)
Motivation:

Opaque indices are a good idea for collections, (Marked)CircularBuffer
should use those too.

Modifications:

Cheapest possible implementation for opaque indices, could be much
improved. This is the cheapest possible implementation for opaque
indices and this could be much improved later, see #682 but the
improvements won't be SemVer major :).

Result:

first stab at #682
2019-02-19 18:46:01 +00:00
Johannes Weiss bf3b87cb4e simplify ChannelOptions (#828)
Motivation:

ChannelOptions used a too complicated mechanism for what they are.

Modifications:

- change the ChannelOptions to a much simpler mechanism
- rename the generic parameters from T to Option

Result:

code easier to read and understand
2019-02-19 18:31:26 +00:00
Johannes Weiss 3d3cc6d685 rename FileHandle to NIOFileHandle (#818)
Motivation:

Collisions with Foundation and other standard libraries is painful.

Modifications:

- rename `FileHandle` to `NIOFileHandle`
- rename `Thread` to `NIOThread` (only exposed in docs)

Result:

fewer collisions
2019-02-19 16:52:00 +00:00
Johannes Weiss 877f0ebc12 require ChannelOptions to be Equatable (#823)
Motivation:

ChannelOptions should've always been Equatable and so far we've hacked
around them not being Equatable when we wanted to compare them.

Modifications:

make all ChannelOptions Equtable

Result:

- ChannelOption comparison actually works
- fixes #598
- make more ChannelOptions stuff inlinable to not regress allocations
2019-02-19 12:30:26 +00:00
Johannes Weiss 03aa0bd43e rename Channel._unsafe to Channel._channelCore (#820)
Motivation:

_unsafe is non-descriptive

Modifications:

- rename Channel._unsafe to Channel._channelCore

Result:

clearer API
2019-02-14 14:14:11 +00:00
Johannes Weiss 7ded801710 Thread's == was public by accident (#819)
Motivation:

Thread's equality function was public by accident.

Modifications:

make Thread's equality function internal

Result:

Thread remains internal
2019-02-14 13:25:49 +00:00
Johannes Weiss 0ac7e2266b remove extension public/internal modifiers (#822)
Motivation:

public/internal modifiers for `extension`s are confusing as a
`func foo` is public if within a `public extension`.

Modifications:

remove all `internal` (redundant) and `public` (dangerous) modifiers for
`extensions`.

Result:

- code easier to read
- code much easier to review in a small diff
2019-02-14 13:25:30 +00:00
John Holdsworth f84b5b8087 Accept dictionary literals for HTTPHeaders (#824)
### Motivation:
A convenience/shorthand for developers.

### Modifications:
Add conformance to ExpressibleByDictionaryLiteral along with implementation.

### Result:
The following is would then be legal in Vapor:

```
        return HTTPResponse(status: .ok, headers: ["Content-Type": "text/html"],
                            body: "<table border=1><tr><td><i>Hello</i>, plugin!")
```
2019-02-14 09:03:31 +00:00
Nathan Harris 9e73705fd4 Update ELF parameter labels to be more Swifty (#809)
Motivation:

As part of NIO2 - much work has been done to round out the API of EventLoopFuture. This finishes existing public API parameters to be more in line with Swift API guidelines.

Modifications:

Methods in EventLoopFuture that had `eventLoop` as a parameter label have been changed to `on`

Results:

EventLoopFuture public API is more "Swifty" with calls such as `future.hop(to: eventLoop)` rather than `future.hopTo(eventLoop: eventLoop)`
2019-02-12 21:40:23 +01:00
Johannes Weiss 6e7a3de8e8 make all removals go through the RemovableChannelHandler API (#815)
Motivation:

Some of the ChannelPipeline removal methods did not go through the
RemovableChannelHandler API.

Modifications:

make all removal APIs go through the RemovableChannelHandler API

Result:

fewer bugs
2019-02-12 19:06:39 +01:00
Johannes Weiss c6c1f84459 CircularBuffer: speedup removeAll (#816)
Motivation:

Previously CircularBuffer.removeAll would always allocate (a new backing
store), even when `keepingCapacity: true`.

Modifications:

Don't make CircularBuffer allocate unnecessarily

Result:

faster CircularBuffer.removeAll
2019-02-12 18:52:29 +01:00
Johannes Weiss 1198931823 ByteBuffer: rename set(<type>:, ...) to set<Type>(...) (#812)
Motivation:

ByteBuffer methods like `set(string:)` never felt very Swift-like and
also didn't look the same as their counterparts like `getString(...)`.

Modifications:

- rename all `ByteBuffer.set/write(<type>:,...)` methods to
  `ByteBuffer.set/write<Type>(...)`
- polyfill the old spellings in `_NIO1APIShims`

Result:

code more Swift-like
2019-02-12 11:11:45 +00:00
Johannes Weiss d938264fcb better ChannelHandler removal API (#767)
Motivation:

If ChannelHandler removal worked correctly, it was often either by
accident or by intricate knowledge about the implementation of the
ChannelHandler that is to be removed. Especially when it comes to
re-entrancy it mostly didn't work correctly.

Modifications:

- introduce a `RemovableChannelHandler` API
- raise allocation limit per HTTP connection by 1
  (https://bugs.swift.org/browse/SR-9905)

Result:

Make things work by contruction rather than accident
2019-02-12 11:27:48 +01:00
Cory Benfield 2738b6d54f Add TCP_INFO/TCP_CONNECTION_INFO queries. (#811)
Motivation:

Many users may wish to see the underlying TCP connection state for
important calculations, such as for BDP or RTT numbers. We can make this
available easily enough.

Modifications:

- Added a function to get TCP_INFO on Linux/FreeBSD.
- Added a function to get TCP_CONNECTION_INFO on Darwin.

Result:

Better connection stats
2019-02-11 12:42:37 +01:00
Gwynne Raskind 642076e9f8 Fix a race condition when remote closes connection after accept() but before fcntl() on macOS. (#807)
Motivation:

A race condition occurs when a socket is closed from the remote end after `accept()` returns it, but before `fcntl(F_SETFL, F_SETNOSIGPIPE)` is invoked. This causes the `fcntl()` call to fail with `EINVAL` ("The socket has been shut down."). This currently triggers an assertion failure. As the race depends on the remote and can be easily demonstrated (using `nmap` is a good way to set it off), the assertion is semantically invalid (`EINVAL` is not programmer error). Additionally, errors reported by `fcntl()` in general are being ignored when not asserted.

Modifications:

The race is avoided by treating `EINVAL` as a normal error while retaining the existing behavior for all other error cases. Both uses of `fcntl()` to disable `SIGPIPE` now properly report errors.

Result:

`Posix.accept()` and `Posix.socket()` may now potentially throw additional errors. It is no longer possible to crash a debug server by running `nmap` against it.
2019-02-06 17:08:00 +00:00
George Barnett 7f68ff36d3 Remove outdated documentation (#808) 2019-02-06 14:02:12 +00:00
Nathan Harris 06fb45d07e Rename ELF.andAll(_:eventLoop:) to andAllSucceed(_🔛) and add ELF.andAllComplete (#803)
Motiviation:

After adding `ELF.whenAllComplete` the concept of "fail fast" and "fail slow" for reducing an array of future results together was introduced.

This commit adds that concepts with the `andAll* methods that act as simple completion notifications.

Modifications:

Rename `EventLoopFuture.andAll(_:eventLoop:)` to `andAllSucceed(_🔛)` to denote its "fail fast" nature, and to match Swift API guidelines.

Add new `EventLoopFuture.andAllComplete(_🔛)` for a "fail slow" companion.

Shift implementation of `whenAllComplete(_🔛)` to be usable without unnecessary allocations in `andAllComplete`

Result:

EventLoopFuture now has two methods for "flattening" arrays of EventLoopFuture into a single notification ELF
2019-02-06 09:16:48 +00:00
Trevör Anne Denise c6c137cde4 Remove useless comments (#806)
Now that flush() doesn't take a promise anymore, this comment isn't
needed.
2019-02-05 16:06:08 +00:00
Nathan Harris caf9a3d8da standardize ELF.cascade* collection of methods (#802)
Motivation:

The `ELF.cascade` methods have a parameter label `promise` that does not match Swift API Guidelines, and a way to cascade just successes is not available - while for failures there is.

Modifications:

`ELF.cascade*` methods that already exist have had their `promise` label renamed to `to`, and a new `ELF.cascadeSuccess` method has been added.

Result:

EventLoopFuture now has the cascade methods `ELF.cascade(to:)`, `ELF.cascadeFailure(to:)`, and `ELF.cascadeSuccess(to:)`
2019-02-05 09:01:48 +00:00
Nathan Harris 9b9dcebecf add ELF.whenAllSucceed method to act as "fail fast" counter-part to ELF.whenAllComplete (#804)
Motivation:

`ELF.whenAllComplete` is a "fail slow" method that returns an `EventLoopFuture` that always succeeds after all provided futures complete - fulfilling with all the results of the futures.

`ELF.whenAllSucceed` is a "fail fast" method that returns an `EventLoopFuture` that succeeds if all of the futures succeed - and returning the fulfilled values.

This new method acts as a counterpart to both `ELF.whenAllComplete` and `andAll`

Modifications:

Add `ELF.whenAllSucceed` that returns an EventLoopFuture with all of the values provided from the futures being awaited.

Rename `ELF.whenAllComplete(_:eventLoop:)` to `ELF.whenAllComplete(_🔛)` to match new `whenAllSucceed` and Swift API guidelines

Result:

New method for flattening multiple futures into a single EventLoopFuture that fulfills with an array of their success values if they all succeed.
2019-02-04 17:43:01 +00:00
Alex Reilly 6ca8c0e189 Documentation grammar fixes (#805) 2019-02-03 09:15:32 +00:00
Johannes Weiss 2392a0775d enable HTTP1 error handling by default (#801)
Motivation:

For compatibility reasons, we used to disable HTTP1 error handling by
default to not break SemVer.

Modifications:

enable HTTP1 error handling by default

Result:

HTTP1 protocol errors now handled automatically by default
2019-02-01 18:02:47 +00:00
George Barnett 6050c9c864 Fix documenation markup (#799)
Motivation:

Quick Documentation was not rendering some parameter documentation
correctly as it was over-indented.

Modifications:

Reduced indentation for over-indented parameters.

Result:

Quick Documentation renders correctly.
2019-02-01 15:34:59 +00:00
Johannes Weiss 89a4eaa807 fix #657 (#795)
Motivation:

testCloseInReadTriggeredByDrainingTheReceiveBufferBecauseOfWriteError
expected (without knowing it) that networking on localhost is
synchronous enough that a succeeded write means that it has
simultaneously arrived in the reader's read buffer.

On macOS that's not necessarily true, so this test failed very seldom
seemingly more often if you had more physical cores.

Modifications:

Introduce a nasty blocking `poll` call in the test that waits until we
have bytes in the read buffer.

Result:

- fixes #657
2019-01-31 16:41:55 +00:00
Moritz Lang 5c2de043b3 Make use of dropFirst(_:) in example code (#794)
Motivation:

While getting into NIO through the examples I stumbled
accross some chained dropFirst() calls, which can be
simplified by using dropFirst(_:) instead.

Modifications:

I did a quick project-wide search of chained dropFirst()
usage and modified every line to use dropFirst(_:) instead.

Result:

This will only effect the readability of the code,
not its functionality.
2019-01-31 14:54:20 +00:00
Tanner 0154604408 rename HTTPProtocolUpgrader to HTTPServerProtocolUpgrader (#750)
* rename HTTPProtocolUpgrader to HTTPServerProtocolUpgrader
2019-01-31 11:36:55 +00:00
JovanMilenkovic cf56bfd119 Remove EmbeddedChannel restriction to IOData (#763)
Remove EmbeddedChannel restriction to IOData

Motivation:

You should be able to read any type from the outbound buffer.

Modifications:

Change Embedded channel outboundBuffer and pendingOutboundBuffer to store NIOAny instead of IOData and refactor test code accordingly.

Result:

- Users can test what went through the pipeline without serializing everything and using "if case let" syntax everywhere.
- fixes #551
2019-01-30 19:24:59 +00:00
Johannes Weiss 70c714a54d address Norman's B2MD feedback (#792)
Motivation:

Norman gave feedback on the new B2MD but after it was already merged.

Modifications:

address feedback

Result:

better code
2019-01-29 16:27:00 +00:00
Gwynne Raskind 8222af4aad Add cancel notification to RepeatedTask (#783)
* Add cancel notification to RepeatedTask

Motivation:

When cancelling a RepeatedTask, it is often desirable to
wait until any still in-progress execution of the task is
finished - for example, to know when a periodic state save
is done before shutting down. The current API provides no
easy way to perform such a wait. With this addition,
repeating tasks can report completion of a cancellation
request regardless of in what state the task was in when the
request was issued.

Modifications:

A default-nil parameter is added to EventLoop.scheduleRepeatedTask(),
to which a promise can be provided; the promise will be
fulfilled at the time of the task's cancellation, plus any
time required for the task to complete running if it was
cancelled during execution. The promise is signaled even if
the cancellation is the result of the task throwing an error.
A default-nil paramter is also added to RepeatedTask.cancel(),
to which a second promise can be passed. This promise is
fulfilled under the same conditions as the first, even if the
first was not given; neither of the promises is dependent
upon the other and both are fulfilled at the same time if
provided. Further calls to cancel() with different promises
will result in fulfillment of each of those promises as well.

Result:

The observed behavior of RepeatedTask.cancel() now optionally
includes deterministic knowledge of when the task is no longer
executing or scheduled to be executed. The API changes are
designed to be additive in form, but the function signatures
do change. Source compatibility is maintained; binary
compatibility is not.
2019-01-29 13:19:12 +00:00
Johannes Weiss 2ad1e21512 CircularBuffer.init: rename initialRingCapacity to initialCapacity
Motivation:

initialRingCapacity is not a great name, initialCapacity is much better

Modifications:

rename CircularBuffer(initialRingCapacity:) to initialCapacity

Result:

more consistent code
2019-01-29 12:29:04 +00:00
Johannes Weiss 58a58b9680 CI: use DEVELOPMENT-SNAPSHOT-2019-01-23-a (#780)
Motivation:

Newer Swift versions are good and we should test them, this one threw a few curveballs at us:
- started using `posix_memalign` which needed fixing in our alloc tests (fixed in #782)
- the optimiser decided not to inline a function that needs to be inlined

Modifications
- `@inline(__always)` to help the optimiser
- update Swift version

Result:
- fixes #727
- fixes #726
2019-01-25 23:50:01 +00:00
David Skrundz 0eb85a4478 Add deadlines as an alternative to timeouts (#770)
Add deadlines as an alternative to timeouts

Motivation:

Address #603

Modifications:

- Add NIODeadline type
- Add func scheduleTask<T>(deadline: NIODeadline, _ task: @escaping () throws -> T) -> Scheduled<T>
- Reduce the use of DispatchTime in favor of Time
- Replace timeout calls with deadline calls where it makes sense

Result:

Tasks can be scheduled after an amount of time has passed, or at a certain time.
2019-01-25 19:32:55 +00:00
Nathan Harris 30f1a544d0 Add whenAllComplete EventLoopFuture method (#773)
* Add `whenAllComplete` method to group a homogenous array of future results into a single ELF
2019-01-25 10:03:11 +00:00
Johannes Weiss 3563b9d795 provide ELF.flatMapResult (#778)
Motivation:

ELFs and the stdlib's Result should be as compatible as possible so a
flatMapResult function which lifts and combines a result into an
EventLoopFuture sounds like a good idea.

Modifications:

add `flatMapResult` which does what it says.

Result:

more interop between `Result` and `EventLoopFuture`.
2019-01-24 20:11:00 +00:00
Johannes Weiss f3294dd803 rename ELF.mapIfError to ELF.recover (#777)
Motivation:

The name `mapIfError` implied that you can transform the error value but
really that's never what it did. Instead, it allowed you to recover
from a failure.

Modifications:

- rename `mapIfError` to `recover`

Result:

cleaner method names
2019-01-24 17:09:13 +00:00
Johannes Weiss c4ff795dda remove ELP.succeed/fail's labels (#776)
Motivation:

EventLoopPromise.succeed/fail has extraneous labels that don't add
anything but noise.

Modifications:

remove extraneous labels

Result:

less noise in the code
2019-01-24 16:42:53 +00:00
Johannes Weiss c741ec1f9b NIO1 API Shims: Forgotten deprecation warning (#771)
Motivation:

It's important that in NIO1 API Shims everything that's defined is also
deprecated. Unfortunately I forgot one.

Modifications:

- deprecate thenIfErrorThrowing

Result:

- Xcode will correctly suggest changing thenIfErrorThrowing to flatMapErrorThrowing
2019-01-22 15:53:46 +00:00
Jari (LotU) 313ddabb48 Allow setting of RemoveAfterUpgradeStrategy (#766)
Allow to set RemoveAfterUpgradeStrategy on HTTPResoonseSecoder
2019-01-21 22:12:24 +00:00
Johannes Weiss f4b185675f speed up getBytes (#761)
Motivation:

Due to https://bugs.swift.org/browse/SR-9604, ByteBuffer.getBytes
seriously regressed (factor 10x) since NIO 1.11 . This works around it
until the issue is fixed.

Modifications:

bind the memory to UInt8s to get an UnsafeBufferPointer<UInt8> rather
than an UnsafeRawBufferPointer.

Result:

- getBytes 10x faster
- make @John-Connolly happy, thanks for reporting!
2019-01-21 21:26:56 +00:00
Johannes Weiss 5619395fa0 provide NIO1 API shim for ELF.then* (#768)
Motivation:

The switch from `ELF.then*` to `ELF.flatMap*` is pretty big so we should
provide NIO1 API shims.

Modifications:

add API shims for ELF.then*

Result:

easier for people to migrate from NIO1 to NIO2.
2019-01-21 20:19:12 +00:00
Johannes Weiss 3e7d6a7bfd rename ELF.then to ELF.flatMap (#760)
Motivation:

ELF's API should be as close as possible to the new Result's API.
Therefore, we should rename `then` to `flatMap`

Modifications:

- renamed `then` to `flatMap`
- renamed `thenIfError` to `flatMapError`
- renamed ELF's generic parameter from `T` to `Value`

Result:

- more like Result
- fixes #688
2019-01-21 16:41:04 +00:00
Johannes Weiss 9033aa6eff provide _NIO1APIShims (#758)
Motivation:

For package authors, the NIO1 to 2 transition can be taxing because we
keep changing things. So far, the community has done an incredible job
keeping up with our changes but we'd like to make it as easy as possible
continuing to do so.

Modifications:

- add a `_NIO1APIShims` module that when imported tries to provide some
  of the NIO1 API in NIO2 with helpful fixup messages

Result:

easier transition from NIO1 to NIO2
2019-01-21 15:51:12 +00:00
Cory Benfield 7a98195524 Actually remove promise logging info. (#752)
Motivation:

While it's really a good idea to keep track of where promises get
allocated, once the promise has been fulfilled we probably don't
need to keep track of it any more.

Happily this leak only affects debug mode.

Modifications:

- Actually removed the element from the promise log when we
  said we would.

Result:

Memory usage doesn't get totally out of hand in debug mode.
2019-01-19 14:15:41 +00:00
David Skrundz f7357e66ab make ELF.cascade accept an optional promise (#757)
* Make EventLoopFuture.cascade and cascadeFailure accept an optional promise

Motivation:

fixes #756

Modifications:

Change EventLoopFuture.cascade to func cascade(promise: EventLoopPromise<T>?)

Result:

EventLoopFuture.cascade can be called without needing to check if the promise is nil
2019-01-19 13:59:12 +00:00
JovanMilenkovic f50230aeba Fix NIOHTTP1Server handleJustWrite always using response status "ok" (#755)
Motivation:

Addressing the https://github.com/apple/swift-nio/issues/713

Modifications:

Forward the statusCode from NIOHTTP1Server main handleJustWrite to ctx.writeAndFlush instead of always using "ok"

Result:

ChannelHandlerContext will write the correct status code even when it's not "ok"
2019-01-18 08:57:27 +00:00
Kevin Sweeney 8f3ac9ac1a Docs: move defer shutdown next to EventLoop creation (#754)
See #753 for rationale.
2019-01-17 17:24:06 +00:00
Kevin Sweeney 794b7b7f02 Docs: move defer closer to the group instantiation (#753)
This makes the boot/shutdown pair visually closer, reinforcing that you shouldn't forget to shutdown the EventLoopGroup.
2019-01-17 11:28:30 +00:00