Commit Graph
906 Commits
Author SHA1 Message Date
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
Johannes Weiss f38984d80e remove bogus 'is active' assertion in channelRead0 (#739)
Motivation:

BaseSocketChannel used to assert that `channelRead0` is only called when
the channel is active. That's bogus and it's trivial to hit this issue
when calling `fireChannelRead` in the last `ChannelHandler` when the
channel has gone inactive.

Modifications:

remove assertion, add comment

Result:

fewer crashes
2019-01-07 14:18:48 +00:00
Johannes Weiss fa2ebe4a56 remove backports of newer Swift functionality (#738)
Motivation:

We now depend on Swift 5.0, so we can remove all the backporting of
functionality.

Modifications:

deleted all backported Swift functionality

Result:

less code
2019-01-07 12:32:58 +00:00
Johannes Weiss 95b290a51f use automatic Equatable conformances (#737)
Motivation:

No code is the best code, let's have the compiler generate more
Equatable instances for us.

Modifications:

remove some hand-written Equatable conformances

Result:

less code, possibly fewer bugs
2019-01-07 11:52:22 +00:00
Johannes Weiss 684cad331c EventLoopFuture: use Result type (#734)
Motivation:

Now that the stdlib has introduced the Result type, we can use it in the
implementation (and the whenComplete) function of EventLoopFuture

Modifications:

- replace EventLoopValue with Result
- make whenComplete provide the Result

Result:

use the new shiny stuff
2019-01-05 08:08:47 +00:00
Johannes Weiss 08eb3259ce better DispatchData support (#732)
Motivation:

DispatchData's APIs are less than optimal and the current one we used
did allocate at least on Linux to copy bytes out.

Modifications:

introduce ByteBuffer.write/read/set/getDispatchData

Result:

when Dispatch gets fixed, lower allocations
2019-01-03 17:45:45 +00:00
Johannes Weiss 695b1ebe13 prefer non-deprecated API for Data/Range (#733)
Motivation:

Deprecated API sucks.

Modifications:

Use non-deprecated API for Data/Range stuff

Result:

better code
2019-01-03 16:00:16 +00:00
Cory Benfield 0db974bdbe Fix: BlockingIOThreadPool not releasing the latest WorkItem (#716) (#730)
BUGFIX: BlockingIOThreadPool not releasing the latest WorkItem

Fix is to clear the latest work item just before waiting for
the next one to be submitted.

Added two tests to validate the fix: submitting only one work item
to an IO thread pool, and submitting two consecutive work items.

Thanks to Gwynne Raskind for help troubleshooting & fix

(cherry picked from commit 8df414b716)
2019-01-03 10:58:15 +00:00
Ilya Puchka 0db96bf744 [NIOHTTP1Server] Use fixed default string to write in response and do not hardcode content length (#718)
* Use fixed default string to write in response and do not hardcode content length

Motivation:

Improve example code, resolve #714

Modifications:

Move hardcoded "Hello world" string to a constant property and removed hardcoding of content length header value

Result:

In example code content length will be written correctly when default response changes
2019-01-03 10:09:13 +00:00
Ilya Puchka b878f06b20 override content encoding header value in compressor (#728)
override content encoding header value in compressor
2019-01-03 09:57:57 +00:00
Sergej Jaskiewicz 161eaa760e [Gardening] Use multiline string literals instead of concatenation (#719)
Motivation:

The rest of the code in NIO uses multiline literals.

Modifications:

Converted string concatenations to multiline literals.

Result:

NFC, prettier code.
2019-01-02 10:36:04 +00:00
Johannes Weiss 531745d71a remove ContiguousCollection (#709)
Motivation:

ContiguousCollection is no longer necessary because Sequence gained
withContiguousStorageIfAvailable.

Modifications:

Removed ContiguousCollection and switch to withContiguousStorageIfAvailable.

Result:

less code
2018-12-18 09:20:37 +00:00
Johannes Weiss e415d654a5 internalise things made public by accident (#463)
Motivation:

A few things are public by accident, this makes those internal for NIO
2.0.0.

Modifications:

internalise a few things

Result:

less public API
2018-12-17 19:19:40 +00:00
Norman Maurer bc21c9a7ac Remove method from ChannelOutboundInvoker protocol that can be extens… (#214)
* Remove method from ChannelOutboundInvoker protocol that can be extensions.

Motivation:

We should not have methods in the ChannelOutboundInvoker that can easily implemented as extension and that never need to be overriden.

Modifications:

Remove methods that return EventLoopFuture<Void> from the protocol and just implement it in the extension.

Result:

More narrow protocol.

* Address comment
2018-12-17 18:40:44 +00:00
Johannes Weiss 885ac373ad fix remaining warnings & -warnings-as-errors (#712)
Motivation:

Warnings are bad.

Modifications:

- fixed remaining warnings
- enabled -warnings-as-errors

Result:

happier devs
2018-12-17 17:13:25 +00:00
Johannes Weiss 2262b656ac fix CircularBuffer.replaceSubrange expands (#683)
Motivation:

CircularBuffer.replaceSubrange was crazy eager to double the capacity
which is bad.

Modifications:

Made CircularBuffer.replaceSubrange only double if necessary.

Result:

less memory usage
2018-12-14 18:49:34 +00:00
Johannes Weiss 1a69a97b5e use String.UTF8View.withContiguousStorageIfAvailable (#707)
Motivation:

The new shiny Swift 5 gives us fast access to the UTF8 bytes (if
available, which mostly means not bridged from Cocoa), we should use
that.

Modifications:

use UTF8View.withContiguousStorageIfAvailable

Result:

faster String access
2018-12-14 18:19:55 +00:00
Cory Benfield 15be5243ef Improve ByteBuffer String APIs (#696)
Motivation:

ByteBuffer has pretty good string writing APIs, but they're imperfect.
In particular, the methods in ByteBuffer-aux both return optional Int,
which is strange as they cannot fail. We also only have support for writing
strings in UTF-8.

Modifications:

- ByteBuffer.set(string:at:) and ByteBuffer.write(string:) now both
     return `Int`, and their return value has been made discardable.
- NIOFoundationCompat has been expanded with two new methods for writing
     strings to ByteBuffers in various encodings using Foundation's
     encoding support: ByteBuffer.set(string:encoding:at:) and
     ByteBuffer.write(string:encoding:).
- NIOFoundationCompat has also been expanded with a companion to the
     already existing ByteBuffer.getString(at:length:encoding), namely
     ByteBuffer.readString(length:encoding:).
- New error enum for reporting failures to encode.

Result:

Easier to work with strings in a wider variety of cases with
ByteBuffer

Resolves #253.
2018-12-14 17:25:13 +00:00
Johannes Weiss 1ac199c195 allow EventLoopIterator to be constructed using [EventLoop] (#706)
Motivation:

Modules outside of NIO core must be able to create `EventLoopIterator`s
in order to conform to the `EventLoop` protocol, therefore we need an
initialiser that's available.

Modifications:

add public initialiser to construct EventLoopIterator from [EventLoop]

Result:

others can implement EventLoops again
2018-12-14 16:13:17 +00:00
Johannes Weiss ee6534f627 update tools-version to 5.0 & fix latest warnings (#704)
Motivation:

The latest Swift 5.0s bring us new goodies (hence tools version) and
also more warnings, hence the fixes for them.

Modifications:

- set tools-version to 5.0
- fix warnings
- workaround https://bugs.swift.org/browse/SR-9514

Result:

happier developers
2018-12-14 12:34:08 +00:00
Sergo Beruashvili e91dacbd54 Removed specialisations from Atomics functions and added @inlinable attribute instead. (#698)
Access level of properties used from @inlinable functions are now internal and they are marked as @usableFromInline.
2018-12-12 09:43:46 +00:00
Bas Broek 6772f13c29 Change unnecessary functions to variables (#695)
* Change unnecessary functions to variables

Motivation:

The changed functions do not have any side effects and therefore better convey intent when being a simple computed variable.

Modifications:

Changes:

- `func markedElementIndex() -> Int?` to `var markedElementIndex: Int?`
- `func markedElement() -> E?` to `var markedElement: E?`
- `func hasMark() -> Bool` to `var hasMark: Bool`

Result:

- improved code quality
2018-12-11 17:50:58 +00:00
Cory Benfield f3f6e09ba2 Make WebSocketOpcode a struct. (#694)
Motivation:

WebSocketOpcode was made an enumeration to make it possible to cleanly
switch over the values in switch statements. However, this unfortunately
made it possible to construct contradictory values of WebSocketOpcode,
such as .unknownNonControl(0x1) (which should be spelled .text), or
.unknownControl(0xFF) (which is simply invalid).

This patch removes the ability to construct invalid values of
WebSocketOpcode by turning it into a struct and using static lets for known
values. While we're here it cleans some other stuff up.

Modifications:

- Made WebSocketOpcode a struct
- Added several static lets for easy access and to reduce code churn
- Used synthesised Equatable conformance
- Added synthesised Hashable conformance
- Added CustomStringConvertible conformance to better represent known values
- Added CaseIterable conformance to provide entire range of valid values.

Result:

WebSocketOpcode will be a better-behaved type with more guarantees and
fewer places to trap and explode.

Resolves #617.
2018-12-11 11:56:10 +00:00
Cory Benfield 55e4bc805e Reunify errors into a single enum. (#690)
Motivation:

If we're going to have errors in enumerations, we should aim to have as
few as possible. We wanted to throw away these two, so let's do it.

Modifications:

- Removed `ChannelLifecycleError`, moved case to `ChannelError`.
- Removed `MulticastError`, moved cases to `ChannelError`.
- Removed manual `Equatable` conformance, used synthesised one.
- Pulled `connectFailed` out of `ChannelError`, as it made it impossible to
    write a correct implementation of `Equatable`.
- Conformed `NIOConnectionError` to `Error` instead.

Result:

Better error handling.

Resolves #620
2018-12-11 08:44:45 +00:00
Johannes Weiss ce39ec20c0 all caps for SNI (#693)
Motivation:

In Swift, abbreviations use the same case for all letters, therefore it
should be `SNI` and not `Sni`.

Modifications:

changes `Sni` to `SNI`

Result:

more consistent with naming guidelines
2018-12-10 20:28:10 +00:00
Johannes Weiss f6b4acc133 changed port/HTTP version parts to Ints (#691)
Motivation:

- `Int` is the currency type for integral things and we already sort of
  changed the the type for ports but not everywhere.
- generic parameters that aren't just 'any type' shouldn't be just `T`
  or `S` or something

Modifications:

- make more use of `Int`
- rename generic parameters to something more descriptive like `Bytes`

Result:

easier to read/write NIO code
2018-12-10 20:18:13 +00:00
Johannes Weiss 305ee9818b make factory method names start with make (#692)
Motivation:

Swift naming guidelines mandate that factory methods start with `make`,
like `makeSomething`. We had a few that were `newSomething`.

Modifications:

make all factories start with make

Result:

more compliant to Swift naming rules
2018-12-10 17:59:24 +00:00
Johannes Weiss cbc214938a first cut at B2MD v2 (#675)
Motivation:

Explain here the context, and why you're making that change.
What is the problem you're trying to solve.

Modifications:

Describe the modifications you've done.

Result:

After your change, what will change.
2018-12-10 16:23:12 +00:00
Johannes Weiss 9b78557aff kick off NIO2 (#678)
Motivation:

NIO2 development starts now.

Modifications:

Made NIO Swift 5-only for everything else see docs/public-api-changes-NIO1-to-NIO2.md

Result:

NIO2 development can start.
2018-12-07 21:15:34 +00:00
Johannes Weiss a20e129c22 change newPromise(for: T.Type) to newPromise(of: T.Type) (#672)
Motivation:

making a promise of a type is more grammatical than making a promise for
a type.

Modifications:

s/newPromise(for/newPromise(of/g

Result:

more grammar
2018-11-28 17:32:07 +00:00
Johannes Weiss c1a84a0085 optional type parameter for newPromise() (#671)
Motivation:

When creating new promises I always find it very frustrating to type the
`: EventLoopPromise<Type>` type annotation but it's necessary for the
compiler to know type the promise will be fulfilled with.

Modifications:

allow an optional `for: SomeType.self` parameter for `newPromise` as

    let p = eventLoop.newPromise(for: Int.self)

is much easier to type than

    let p: EventLoopPromise<Int> = eventLoop.newPromise()

Result:

easier to write code
2018-11-28 12:18:03 +00:00
Bouke Haarsma 29398d5f26 Assign channel variable in docs (#662) 2018-11-27 08:09:06 +00:00
Kyle Jessup ac9e5d02ac Added public init for HTTPServerProtocolErrorHandler (#660) 2018-11-23 15:29:09 +00:00
Johannes Weiss c66386eb3a don't capture in the callout closures in BaseSocketChannel (#658)
Motivation:

We were complaining that we can't use `@convention(thin)` in a place
where we really need to make sure we don't get allocations for returning
a closure (that doesn't capture any context), so it should just be a
function pointer.

In reality however, we were actually capturing context...

Modifications:

Don't capture context anymore.

Result:

Closures should not actually not enclose anything.
2018-11-21 09:48:06 +00:00
Johannes Weiss cf958fb5a5 ignore pthread_setname_np result (#656)
Motivation:

We have seen pthread_setname_np fail with EPERM in containers, it's
non-critical so best to ignore.

Modifications:

ignore pthread_setname_np's return code.

Result:

works in containers.
2018-11-16 20:01:11 +00:00