88 Commits
Author SHA1 Message Date
Johannes Weiß b109389c54 eagerly process input EOF/connection resets (fixes #277) (#286)
Motivation:

We had a number of problems:

1. We wanted to lazily process input EOFs and connection resets only
   when the user actually calls `read()`. On Linux however you cannot
   unsubscribe from `EPOLLHUP` so that's not possible.
2. Lazily processing input EOFs/connection resets wastes kernel
   resources and that could potentially lead to a DOS
3. The very low-level `Selector` interpreted the eventing mechanism's
   events quite a lot so the `EventLoop`/`Channel` only ever saw
   `readable` or `writable` without further information what exactly
   happened.
4. We completely ignored `EPOLLHUP` until now which on Unix Domain
   Socket close leads to a 100% CPU spin (issue #277)

Modifications:

- made the `Selector` interface richer, it now sends the following
  events: `readable`, `writable`, `readEOF` (input EOF), `reset`
  (connection reset or some error)
- process input EOFs and connection resets/errors eagerly
- change all tests which relied on using unconnected and unbound sockets
  to user connected/bound ones as `epoll_wait` otherwise would keep
  sending us a stream of `EPOLLHUP`s which would now lead to an eager
  close

Result:

- most importantly: fix issue #277
- waste less kernel resources (by dealing with input EOFs/connection
  resets eagerly)
- bring kqueue/epoll more in line
2018-04-16 19:36:47 +02:00
Cory Benfield e6b8da38b5 Make Scheduled's constructor public. (#290)
Motivation:

You need to be able to implement EventLoop.scheduleTask to implement
your own event loop. EventLoop.scheduleTask requires creating a
Scheduled. Currently, Scheduled has no public initializer.

Modifications:

Make Scheduled's initializer public.

Result:

It will be possible to actually implement EventLoop.
2018-04-06 17:38:21 +02:00
Matt Eaton 66e4fdfbba Fixed a few minor typos and some force unwraps (#259)
fix some typos, force unwraps and inconsistencies

Motivation:
Reviewing the code I found a few typos that hindered readability, some unnecessary force unwraps and inconsistencies in the codebase.

Modifications:

- The main file in NIOWebSocketServer now accepts command line arguments
and binds to a passed in host and port like the other server examples do.
- Minor typo fixes
- removed one force unwrap

Result:

- The result of my change will bring the NIOWebSocketServer more inline
with how the NIOHTTP1 and NIOChat servers examples are setup.
- less force unwrapping
- fewer typos

[message edited by @weissi]
2018-04-03 11:44:14 +01:00
Johannes Weiß c9c3974276 bring PriorityQueue/Heap implementations inline (#245)
Motivation:

We triggered extra allocations for the generics on PriorityQueue/Heap.
They unfortunately can't be solved using `@_specialize` as the element
type is `ScheduledTask`.
Fortunately we never exposed those types externally so we can just
inline without breaking the public API.

Modifications:

Moved everything from the `NIOPriorityQueue` module into the `NIO`
module.

Result:

Less allocations, more happiness.
2018-03-27 19:04:33 +02:00
Norman Maurer f4036e2b0d Make use of lock.withLockVoid to simplify code (#242)
Motivation:

We should make use of lock.withLockVoid to simplify our code and make it more robust.

Modifications:

- Replace manual lock() / unlock() calls by withLockVoid(...)
- Mark lock.withLock* as @_inlineable to ensure we not get an allocation for the closure.

Result:

Code is cleaner and easier to understand.
2018-03-27 15:22:57 +01:00
Norman Maurer cce02c9e40 Remove one pair of lock() / unlock() during EventLoop run. (#235)
Motivation:

We can remove one lock() / unlock() pair by saving the next ready task during loop execution.

Modifications:

Remvoe one lock() / unlock() pair.

Result:

Less locking during EventLoop run executions.
2018-03-26 16:42:39 +02:00
Norman Maurer 6cf8ca4398 Remove #available(OSX 10.12, *) check which is not needed anymore (#233)
Motivation:

At some point we needed to check for #available(OSX 10.12, *) which is not true anymore as we use our own Thread impl.

Modifications:

Remove #available(OSX 10.12, *) check

Result:

Remove incorrect and not needed check.
2018-03-26 14:23:14 +01:00
Norman Maurer 58a9651d90 Re-use same ContiguousArray for tasks copy operations. (#192)
Motivation:

We allocated a new ContiguousArray per each EventLoop run when tasks needed to be executed. We can just re-use the same instance all the time and so safe malloc operations (we will need to do memset tho).
Beside this we also did not reserve the needed capacity up fron which could lead to realloc operations.

Modifications:

- Reuse same ContiguousArray instance
- Correctly set the initial capacity

Result:

Less overhead when processing tasks via the EventLoop
2018-03-19 19:35:43 +01:00
Norman Maurer b91090f563 Fix possible endless loop when shutdown SelectableEventLoop (#188)
Motivation:

When the SelectableEventLoop is shutdown it will process a defer block in which we tried to fail all scheduled tasks that were not processed yet. The problem here was that we incorrectly not dropped the processed task and so ended in an endless loop which prevent the defer block to complete.

Beside this how we processed the scheduled tasks that were ready for execution was quite in-efficient as we always called removeFirst() which will most likely case a memory copy to move the elements around.

Modifications:

- Correctly fail all scheduled tasks without end up in an endless loop
- Use a for each loop to reduce overhead of processing ready tasks.

Result:

It's always possible to shutdown a SelectableEventLoop and less overhead when processing ready tasks.
2018-03-19 19:53:41 +09:00
Norman Maurer c40c6e1222 Add support to obtain the current EventLoop (#120)
Motivation:

In certain cases it would be useful to be able to access the current EventLoop (which is tied to a thread).

Modifications:

- Add MultiThreadEventLoopGroup.currentEventLoop
- Add tests

Result:

Fixes [#59]
2018-03-15 16:05:35 +01:00
sun_ 2a9e2d6660 Small cleanup in EventLoop. (#96)
Motivation:
Cleaner code.

Modifications:
Change () throws -> (T) to () throws -> T

Result:
Cleaner code.
2018-03-04 06:58:20 -08:00
adamnemecek 0bbed6d72f * removed trailing whitespace (#52) 2018-03-02 15:40:52 +09:00
Norman Maurer d74dda7439 Remove Channel.open and rename Selectable.open to isOpen (#19)
Motivation:

Channel.open was not really needed as for our use-case Selectable already provides everything we need. Also how we used Channel.open in some scenarios was not really correct.

Modifications:

- Remove extension method.
- Rename property
- Remove out-dated comment.
- Fix usage of open

Result:

More clear and correct API
2018-02-27 09:34:26 +09:00
Johannes Weiß 73a805c7f6 minor stylistic improvements (#36)
Motivation:

Sometimes we deviated from the style the Swift stdlib sets out for no
reason.

Modifications:

Fixed some stylistic deviations.

Result:

Looks more Swift-like.
2018-02-26 15:52:49 +00:00
Johannes Weiss 26582a3e97 redundant let elimination
Motivation:

`let _ = ...` is redundant and should be `_ =`

Modifications:

removed redundant `let`s

Result:

saved disk space ;)
2018-02-19 17:41:52 +00:00
Johannes Weiss 4a09182a39 rewrite closures that just ignore their parameter & change whenComplete
to not return a value

Motivation:

We recently had a bug where we had `EventLoopFuture<EventLoopFuture<()>>` which didn't make any sense. The compiler couldn't catch that problem because we just ignored a closure's argument like this:

    future.then { _ in
        ...
    }

which is dangerous. For closures that take an empty tuple, the `_ in`
isn't actually required and the others should state the type they want
to ignore.

And most whenComplete calls can be better (and often shorter) expressed
by other combinators.

Modifications:

remove pretty much all closures which just blanket ignore their
parameter.

Result:

- no closures which just ignore their parameter without at least stating
  its type.
- rewrote all whenCompletes that actually used the value
2018-02-16 17:28:43 +00:00
Cory Benfield ab9427bdd4 Initial UDP support 2018-01-16 17:00:09 +00:00
Cory Benfield 67bb71e44b Add a pending datagram writes manager
This change adds support for doing datagram-style writes. It's not currently
hooked up to anything, but it will be sometime soon.
2018-02-14 15:32:31 +00:00
Johannes Weiß dba0a5379c fix longstanding typo in newSucceededFuture
Motivation:

We used to have a method `newSucceedFuture` which should be
`newSucceededFuture`

Modifications:

grammar

Result:

better grammar
2018-02-13 09:55:33 +00:00
Johannes Weiß 11a609396d don't blow up when holding onto a ChannelPipeline of a deinitialised Channel 2018-02-09 14:53:09 +00:00
Johannes Weiss 734904d0f4 clean up the (proto)types for EventLoopFuture
* clean up the (proto)types for EventLoopFuture

* Remove more unnecessary parens
2018-02-09 16:03:28 +00:00
Norman Maurer 995177fbc6 Support setting Thread affinity and use it with EventLoopGroups 2018-02-06 16:08:57 +01:00
Cory Benfield 3768e2a2b9 Don't allow registration on shutting down loops 2018-02-08 10:25:47 +00:00
Johannes Weiß 32bb597c24 remove Int extension we do not need 2018-02-06 12:30:27 +00:00
Norman Maurer e1bf447a9a Replace usage of Foundation.Thread with our own Thread that just used…
* Replace usage of Foundation.Thread with our own Thread that just used pthread

* Address comments

* some more docs / comments

* Address comments of cory
2018-02-06 11:45:57 +01:00
Johannes Weiss 4a2425f5b7 promise leak debugging 2018-01-24 08:21:35 +00:00
Johannes Weiß c79c5ba364 re-export Darwin/Glibc and reduce number of #if os(...) 2018-01-09 15:08:17 +00:00
Johannes Weiss 34e0b1ac0a sanitise time interval (aka TimeAmount) APIs 2018-01-08 15:41:40 +00:00
Johannes Weiss 246bd7e56f EventLoop and EventLoopGroup should be class bound 2018-01-04 17:41:10 +00:00
Johannes Weiß 94e1af5452 compile warning-free on Swift 4.1 2017-12-18 15:37:57 +00:00
Johannes Weiß 25f4e2ceff PriorityQueue: make it work for partial orders 2017-12-12 11:13:33 +00:00
Johannes Weiss 64a1c2feb8 Channel writes: unit tests & lots of fixes 2017-12-11 12:20:46 +00:00
Norman Maurer 2bfd69b980 Add docs for EventLoop
* Add docs for EventLoop

* Address comments
2017-12-05 13:54:12 +01:00
Johannes Weiss 8335b6c3a0 replace priority queue 2017-11-28 16:13:26 +00:00
Johannes Weiß 06f9b1d63b merge ConcurrencyHelpers 2017-11-21 18:32:33 +00:00
Johannes Weiß 33fe401322 rename Future/Promise to EventLoopFuture/EventLoopPromise 2017-11-21 17:41:36 +00:00
Max (Swift) Moiseev 198d6b447b Explicit access modifiers on members in extensions 2017-11-18 04:40:26 -08:00
Max Moiseev 01b28a3f6d Simplify logic in guards (replace some with ifs instead) 2017-11-16 17:29:35 -08:00
Cory Benfield 1cb736e370 Catch bugs with sync shutdown 2017-11-10 17:28:52 +00:00
Johannes Weiß 31cd14eaad fix inEventLoop race and makes TSan happy 2017-11-10 14:56:33 -08:00
Norman Maurer 9c7829a065 Port IdleStateHandler from Netty 2017-10-29 09:46:48 +01:00
Johannes Weiss 3bef8a4012 turn import Foundation comments into code :) 2017-10-12 16:33:50 +01:00
Johannes Weiss 1adb581f59 remove more Foundation imports 2017-10-10 16:25:43 +01:00
Cory Benfield 3b40563a5e Prevent hot loop starving I/O 2017-10-09 18:14:49 -07:00
Johannes Weiß 8e06697362 fix EventLoop shutdown by introducing shutdownGracefully 2017-09-20 17:15:06 +01:00
Johannes Weiß 1b46d7c4ba fix ScheduledTask's Comparable implementation 2017-09-20 17:57:31 +01:00
Johannes Weiss 6721d46b9d make scheduling tasks on the EventLoop compatible with the tail call syntax 2017-09-18 16:24:08 +01:00
Norman Maurer ca7aac3253 Allow to cancel scheduled tasks 2017-08-18 14:07:17 +02:00
Norman Maurer 351d7c40d5 Cleanup code and remove old comments 2017-08-17 19:29:11 +02:00
Norman Maurer b2405eac44 Add support to schedule tasks on the EventLoop 2017-08-17 13:50:32 +02:00