Commit Graph
2140 Commits
Author SHA1 Message Date
Johannes Weiß 8bf1be0672 fix some Swift 4.2 warnings (#374)
Motivation:

Warnings are bad.

Modifications:

Changed things that still work in 4.0/4.1 but are warnings in 4.2 to
improved versions.

Result:

less warnings on the upcoming 4.2
2018-05-01 16:04:44 +01:00
Helge Heß 772917f3a1 Add http-parser license and authors (#251)
* Add http-parser license and authors

Motivation:

Quote:

> The above copyright notice and this permission notice shall be included in
> all copies or substantial portions of the Software.

Just linking is not enough.

Modifications:

Added the http-parser license.
Added the AUTHORS file. Not required by the license,
but the right thing to do.

Result:

Proper license inclusion for http-parser.
2018-05-01 15:49:14 +01:00
Johannes Weiß 86fce7fce8 don't nil out connectPromise prematurely and wait fully (#337)
Motivation:

We can't nil out the connectPromise before we fail it. This totally
slipped through because we never fully waited for it because of the
reasons below.

It's quite easy to misuse `eventLoop.submit` as if you do some
asynchronous operation (will return `EventLoopFuture<T>`) the result of
the submit will be `EventLoopFuture<EventLoopFuture<T>>`. So if you just
do

    try eventLoop.submit {
        return somethingAsynchronous
    }.wait()

then you will have only waited for the asynchronous operation to have
started (it might still be running). Fortunately, the compiler usually
warns you about that unless it returns `Void`. So it naturally tells you
to write

    try eventLoop.submit {
        return somethingAsynchronous
    }.wait().wait()

which will wait for both. Unfortunately, `XCTAssertNoThrow` will just
eat away any type so there it's advisable to write

    XCTAssertNoThrow(try eventLoop.submit { ... }.wait.wait() as Void)

to be sure it's actually a void. The Swift compiler however has an
annoying bug where it won't always realise that that's a problem
(https://bugs.swift.org/browse/SR-7478). Fortunately the 4.1 release
behaves okay so CI will catch it.

Modifications:

- replace the bad pattern
    _ = try el.submit { ... }
- added `as Void`s inside `XCTAssertNoThrow`s that do submit
- added missing `.wait()`s

Result:

tests should be more correct.
2018-05-01 15:24:20 +01:00
adamnemecek a022954256 Refactored UInt32 and UInt64 methods nextPowerOf2 (#371)
* * refactored UInt32 and UInt64 methods nextPowerOf2

* Added nextPowerOf2 to FixedWidthInteger
2018-05-01 10:36:37 +01:00
Cory Benfield a1980922f3 Add UnsafeEmbeddedAtomic and use it. (#373)
Motivation:

Many uses of atomics restrict themselves to using atomic variables within
objects that already have reference semantics (i.e. classes) and have
appropriate lifetime management. In this case, the current NIO Atomic
performs an unnecessary heap allocation in order to get itself a reference
count that it can use to free the underlying heap memory.

This is an unnecessary allocation that can often be elided, especially in
situations where many such atomics may be needed. Thus, we should provide
the ability to manually manage the lifetime of the heap-allocated memory for
the atomic, and change some uses of it in our codebase to reduce overhead.

Modifications:

1. Moved most of the code of Atomic to a new struct, UnsafeEmbeddedAtomic.
2. Reimplemented Atomic in terms of UnsafeEmbeddedAtomic.
3. Updated EventLoopFuture to use an UnsafeEmbeddedAtomic for isFulfilled.

Result:

1/3 as many allocations per EventLoopFuture.
2018-05-01 11:02:27 +02:00
Johannes Weiß a4318d5e75 make BB.debugDescription print xxd -r -p compatible format (#367)
Motivation:

When debugging, I often use ByteBuffer.debugDescription which dumps
the bytes. Unfortunately, the format wasn't compatible with what
`xxd -r -p` expects. Now you can just copy some output, for example

    echo '04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12' | xxd -r -p

and xxd -r -p will correctly parse the byte values.

Modifications:

made BB.debugDescription prefix byte values under 0x10 with a 0 instead
of a space.

Result:

easier debugging with the standard tools
2018-04-27 11:59:08 +01:00
Cory Benfield 708a62d257 Avoid delivering websocket frames twice on EOF. (#368)
Motivation:

Unfortunately as a result of #108 the WebSocketFrameDecoder can emit a
WebSocketFrame more than once if the user closes the connection in
channelRead, or any other callback while decode() is on the call stack.
This is obviously less than ideal, as it can allow multiple delivery of
frames.

Modifications:

Given WebSocketFrameDecoder a no-op implementation of decodeLast.

Result:

No multi-delivery of frames.
2018-04-27 11:02:51 +01:00
Norman Maurer e5dedbec14 Not cause a CoW when calling ByteBuffer.discardReadBytes() while it was fully consumed. (#363)
Motivation:

We should not need to trigger any copy when the ByteBuffer was fully consumed and discardReadBytes() is called.

Modifications:

- If fully consumed just reset reader and writer Index.
- Add unit test

Result:

Less memory copies
2018-04-27 10:04:18 +01:00
Norman Maurer cf064a0575 Remove unnecessary if let usage in writeAndFlush (#365)
Motivation:

There was some if let usage that is not needed . We can remove it and just pass down the promise (no matter if its nil or not).

Modifications:

Remove if let

Result:

Code cleanup
2018-04-27 09:48:34 +01:00
adamnemecek 776c3f42cb trimmed whitespace (#361)
Trimmed trailing whitespace.

Motivation:

Trimmed trailing whitespace.

Modifications:

Trimmed trailing whitespace.

Result:

Less trailing whitespace.
2018-04-27 08:33:32 +02:00
Norman Maurer 6565ec5a39 Not do any precondition range checks when moving the reader / writer index from within our methods of ByteBuffer. (#362)
Motivation:

We should not need to do any precondition range changes when we change the reader / writer index within our ByteBuffer methods as we know the index is valid. Beside this we already have asserts in place.

Modifications:

Add missing _ methods to adjust the reader / writer index and make use of these.

Result:

Less range checks within our ByteBuffer methods.
2018-04-26 21:43:41 +02:00
Johannes Weiß 5d3aa226d1 improve ByteBuffer documentation regarding CoW on slices (#359)
Motivation:

We got a very valid question on Twitter
(https://twitter.com/karim_elna/status/989516133421199361) regarding
ByteBuffers 'sharing storage'. Our docs almost sound like a ByteBuffer
and its slices share storage even after a write. Especially given that
this is a difference to Netty, we should document this better.

Modifications:

Added clarifying documentation.

Result:

Hopefully the docs are more clear now.
2018-04-26 21:24:48 +02:00
Johannes Weiß 69c3d28a0b two missing @_inlineables (#360)
Motivation:

@_inlineable is important for at least all performance sensitive
functions that use generics and/or closures.

Modifications:

added two missing @_inlineable

Result:

fewer allocations (not measurable because https://bugs.swift.org/browse/SR-7542)
2018-04-26 18:32:21 +01:00
Johannes Weiß 89e5574bb4 fix optimiser problem triggered in BB.set(staticString:) (#354)
Motivation:

There's a presumed optimiser bug which will trigger an allocation when
using `self` within `staticString.withUTF8Buffer { ... }` which makes it
cause an extra allocation.

Modifications:

use StaticString.utf8Start instead of withUTF8Buffer

Result:

less allocations
2018-04-26 15:41:49 +02:00
Johannes Weiß bc61e6a815 shrink HTTP{Request,Response}Head by CoW boxing them (#351)
Motivation:

HTTP{Request,Response}Head were too large even with a less than 3 word
ByteBuffer, this shrinks them box CoW boxing them. Should also reduce
ARC traffic when passing around a bunch.

Modifications:

CoW box HTTP{Request,Response}Head

Result:

fewer existential containers created, more performance
2018-04-24 20:03:27 +02:00
Johannes Weiß db61cfe38e make enums containing ByteBuffer fit in three words (#349)
Motivation:

NIO's dynamic pipeline comes with one performance caveat: If your type
isn't specialised in NIOAny (only types in the NIO module can be) and
it's over 3 words (24 bytes on 64-bit platforms), you'll suffer an
allocation for every time you box it in NIOAny. Very unfortunately, both
ByteBuffer and FileRegion were exactly 3 words wide which means that any
enum containing those would be wider than 3 words. Worse, both
HTTP{Request,Response}Head contained types that were wider than 3 words.
The best solution to this problem is to shrink ByteBuffer and FileRegion
to just under 3 words and that's exactly what this PR is doing. That is
slightly tricky as ByteBuffer was already bit packed fairly well
(reader/writer indices & slice begin/end were stored as a UInt32). The
trick we employ now is to store the slice beginning in a UInt24 and the
file region reader index in a UInt56. That saves one byte for both
ByteBuffer and FileRegion with very moderate tradeoffs: The reader index
in a file now needs to be within 64 PiB (peta bytes) and a byte buffer
slice beginning must start within 16 MiB (mega bytes). Note: The
reader/writer indices as well as slice ends are _not_ affected and can
still be within 4 GiB. Clearly no one would care about the restrictions
for FileRegions in the real world but we might hit the ByteBuffer slice
beginning limit in which case the slice would be copied out. But
given that slices are mostly used to slice off headers in network
protocols, 16 MiB should be _plenty_.

Norman was kind enough to measure the perf differences:

master (before this PR):
```
$ wrk -c 256 -d 10s -t 4 -s ~/Downloads/pipeline-many.lua -H "X-Host: SomeValue" -H "Host: swiftnio.io" -H "ThereAreEvenMoreHeaders: AndMoreValues" http://localhost:8888
Running 10s test @ http://localhost:8888
  4 threads and 256 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    93.02ms  158.13ms   1.97s    93.07%
    Req/Sec   112.79k    26.05k  258.75k    84.50%
  4501098 requests in 10.04s, 214.63MB read
  Socket errors: connect 0, read 111, write 0, timeout 89
Requests/sec: 448485.61
Transfer/sec:     21.39MB
```

after this PR:
```
$ wrk -c 256 -d 10s -t 4 -s ~/Downloads/pipeline-many.lua -H "X-Host: SomeValue" -H "Host: swiftnio.io" -H "ThereAreEvenMoreHeaders: AndMoreValues" http://localhost:8888
Running 10s test @ http://localhost:8888
  4 threads and 256 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   107.53ms  206.56ms   1.99s    90.55%
    Req/Sec   124.15k    26.56k  290.41k    89.25%
  4952904 requests in 10.03s, 236.17MB read
  Socket errors: connect 0, read 161, write 0, timeout 22
Requests/sec: 493852.65
Transfer/sec:     23.55MB
```

so we see a nice 10% improvement

Modifications:

- shrank ByteBuffer by 1 byte, making it 23 bytes in total
- shrank FileRegion by 1 byte, making it 23 bytes in total
- added @_inlineable to NIOAny where appropriate to not suffer boxed existentials in different places
- added tests for the new edge cases

Result:

- more speed
- fewer allocations
2018-04-24 18:51:24 +01:00
Shaun Hubbard 163827b68e Refactor http response status equatable (#335)
Refactored extension HTTPResponseStatus: Equatable, no change to the efficacy of the code.

Motivation:

Code readability is essential to a long-lasting maintainable project, the code this PR changes was "readable" however this PR will enhance the readability by removing excess lines of code.

Modifications:

Changed a long case statement, to a much shorter one.

Result:

There will be fewer lines of code, no change to the efficacy of product should occur. I am not familiar enough with the compiler to know if this will have a deleterious effect on performance I don't think it should be based on the way public var code: UInt { get } is implemented.
2018-04-24 15:36:36 +02:00
Norman Maurer 976074d536 Add last / prepend to CircularBuffer (#347)
Motivation:

Its often useful to be able to add / retrieve elements on both sides of a CircularBuffer.

Modifications:

- Add last / prepend to be able to act on both ends.
- Add tests

Result:

Fixes https://github.com/apple/swift-nio/issues/279
2018-04-24 10:26:38 +02:00
Johannes Weiß ec30e5cc5a fix recursive channelReads in pipelining handler (#348)
Motivation:

The pipelining handler made the assumption that `channelRead` is never
called recursively. That's mostly true but there is at least one
situation where that's not true:
- pipelining handler seen a response .end and delivers a .head (which is
  done in `channelRead`)
- a handler further down stream writes and flushes some response data
- the flushes fail which leads to us draining the receive buffer
- if the receive buffer contained more requests, the pipelining
  handler's `channelRead` is called again (recursively)

The net result of that was that the new request parts from the receive
buffer would now jump the queue and go through the channel pipeline
next, before other already buffered messages.

Modifications:

made the pipelining handler buffer if a `channelRead` comes in from the
pipeline and there is already at least one message buffered.

Result:

the ordering of the incoming messages should now be respected which is
very important...
2018-04-24 08:18:21 +01:00
Norman Maurer 9f01374169 Ensure localAddress / remoteAddress are still accessible in channelInactive / handlerRemoved (#346)
Motivation:

Often its useful to be still be able to access the local / remote address during channelInactive / handlerRemoved callbacks to for example log it. We should ensure its still accessible during it.

Modifications:

- Fallback to slow-path in ChannelHandlerContext.localAddress0 / remoteAddress0 if fast-path fails to try accessing the address via the cache.
- Clear cached addresses after all callbacks are run.
- Add unit test.

Result:

Be able to access addresses while handlers are notified.
2018-04-23 12:51:01 +02:00
Norman Maurer cbb3f19f62 Use power of two for underlying storage of CircularBuffer (#313)
Motivation:

We should use power of two for the underlying storage to allow us to make use of bitmasking which is faster then modulo operations.

Modifications:

Use power of two and replace module with bitmasking.

Result:

Faster implementation of CircularBuffer.
2018-04-23 11:18:57 +01:00
Johannes Weiß 7fe7ed6a89 canonical form for headers: fast path for 0 headers (#344)
Motivation:

It's quite common that someone asks for the canonical form of a header
and we don't have any header values. There's no need to do string
comparisons then, we can just return an empty array.

Modifications:

Check if we're asking for a header that isn't present and if so, just
return the empty array.

Result:

Should be ever so slightly faster.
2018-04-23 06:58:28 +02:00
Johannes Weiß 902b18df16 don't deliver events for unregistered fds (#341)
Motivation:

Since forever we had a major bug in the Selector: In this condition:

- kqueue/epoll had many events
- in one of the earlier events we unregister a Channel whose fd is on of
  the later events
- we subsequently (still in the same event loop tick) register a new
  channel which gets the same fd as the previously closed one

then we would deliver an event that was meant for a previous channel to
a newly opened one.

Thanks to @mcdappdev for hitting this bug, helping us debug it and also
providing a repeatedly working repro.

Modifications:

if during event delivery any fd gets unregistered, we stop delivering
the remaining events and rely on the selector to redeliver them
again next time.

Result:

we don't deliver events for previously closed channels to new ones.
2018-04-20 16:19:10 +01:00
Helge Heß 17a2aae68b Add WebSocketUpgrader to channel config (#340)
Motivation:

The NIOWebSocketServer demo works much better, when it actually
configures the WebSocketUpgrader.

You should have a test for that!

Modifications:

This adds the `upgrader` to the `HTTPUpgradeConfiguration`.

Result:

The demo will actually perform something web-sockety.
2018-04-19 18:08:28 +01:00
Johannes Weiß 56b540bfda better descriptions for HTTP{Response,Request}Head and HTTPVersion (#339)
Motivation:

We recently changed the backing for the HTTP request and response
objects so that the URI string was no longer visible in
`request.description`. This patch fixes that and prints a format like
this:

    HTTPRequestHead { method: GET, uri: "/", version: HTTP/1.1, headers: [("Host", "localhost:8888"), ("User-Agent", "curl/7.54.0"), ("Accept", "*/*")] }

and

    HTTPResponseHead { version: HTTP/1.1, status: ok, headers: [("content-length", "12")] }

Modifications:

improved the HTTP request/response descriptions

Result:

debugging is easier
2018-04-19 17:51:46 +01:00
Norman Maurer 91ef938c60 Ensure correct ordering of promise notification when connect is still pending. (#330)
Motivation:

When a pending connect is still in process and the Channel is closed we need to ensure we use the correct ordering when notify the promises and ChannelHandlers in the pipeline.

The ordering should be:

- Notify pending connect promise
- Notify close promise
- Call channelInactive(...)

Modifications:

- Correct ordering of notification
- Add unit test

Result:

Ensure correct ordering when connect is still pending
2018-04-19 14:06:21 +02:00
Lev Walkin 338cfb541b support for initializing off the system file descriptors (#285)
Motivation:

In some contexts it is important to be able to pass file descriptors
around instead of specifying the particular host and port. This allows removing
certain windows for race conditions, and has positive security implications.

Modifications:

Added `withConnectedSocket(_:)` call to the `ClientBootstrap` that can
be used to create a new `Channel` out of existing file descriptor
representing the connected socket.

Result:

Allows initializing `Channel`s off the existing file descriptors.
2018-04-19 09:58:39 +02:00
Tanner 1360fc6995 Make all members of HTTP{Request|Response}Head mutable. (#324)
Motivation:

Immutable members of structs (properties defined with let) can never be changed
even if the struct itself is mutable (defined withh var). This makes it difficult
to build rich convenience APIs on top of these structs.

Modifications:

Changed all 'let' stored properties on HTTP{Request|Response}Head to 'var'.

Result:

HTTP{Request|Response}Head are easier to use now.
2018-04-18 20:48:33 +02:00
Johannes Weiß a1ab3f32b3 propagate socket errors on .reset (#325)
Motivation:

Since #286, we do eagerly detect connection resets which is good. To
resolve the situation we first try to read everything off the socket. If
something fails there, everything is good and the client gets the
correct error. If that however doesn't lead to any errors, we would
close the connection with `error: ChannelError.eof` which the client
would then see for example on a connect that happened to get
`ECONNRESET` or `ECONNREFUSED`. That's not right so these situations are
fixed here.

Modifications:

- on reset, try to get the socket error if any
- if no socket error was detected, we send a `ECONNRESET` because we
  know we have been reset but not why and didn't encounter any errors
- write tests for these situations

Result:

connection resets that don't hit any other errors will now see the
correct error
2018-04-18 18:56:00 +01:00
Johannes Weiß 5327cb119c non-blocking versions of file open/stat (#256)
Motivation:

Opening a file, seeking it or querying its size (or other information)
is blocking on UNIX so `NonBlockingFileIO` should support that too.

Modifications:

Added a method to `NonBlockingFileIO` which lets the user open a file
without blocking the calling thread.

Result:

Less blocking is good :)
2018-04-18 17:56:44 +01:00
Cory Benfield 707b413ec6 Synchronous connection failures must close channels. (#329)
Motivation:

When a connect() call returns an error other than EINPROGRESS, we
currently leave the FD registered and don't close the channel. This is
wrong, we should take this as a signal to close the socket immediately,
rathern than letting the selector tell us this FD is dead: after all,
we know it's dead.

Modifications:

Call `close0()` in synchronous connect failures.

Result:

Faster failures!
2018-04-18 16:07:05 +01:00
Cory Benfield 9326f749aa Allow ChannelCore implementors to unwrap NIOAny. (#321)
Motivation:

When implementing a custom ChannelCore, you will probably need access
to the data inside a NIOAny. It should be possible to unwrap that.

Modifications:

Added an extension to ChannelCore to allow unwrapping a NIOAny.
Added @_versioned to almost all of NIOAny.

Result:

ChannelCore is implementable outside NIO
2018-04-18 10:04:12 +01:00
Johannes Weiß c3706cdf2a reduce allocations & test we don't regress (#316)
Motivation:

The ping/pong test should only do 4 allocations:
- readFromSocket does unconditionally allocate one ByteBuffer
  (allocating one ByteBuffer does 2 allocations)
- we have a ping and a pong side, each call readFromSocket

so 2 * 2 = 4 allocations per message. Before adding the `@_inlineable`
to the read/write/get/set integer methods in ByteBuffer however I was
seeing way more allocations. So looked at them and fixed them.

Turns out that massively reduces the number of allocations for HTTP too.

On Linux I was seeing this change:

1000_reqs_1_conn: 282000 -> 70000
1_reqs_1000_conn: 1287000 -> 907000

Modifications:

- made the ByteBuffer integer methods inlineable
- added a test that makes sure we don't regress on the simplest possible
  use of NIO: a ping/pong server

Result:

After your change, what will change.
2018-04-17 13:07:15 +01:00
Pushkar N Kulkarni acfb5f7136 Make IdleStateHandler.IdleStateEvent public (#319)
IdleStateHandler.IdleStateEvent is currently internal. Make it public.

Motivation:

The IdleStateEvent is delivered to the user viapublic func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any). In this method, it must be possible to have a check on the type of event. IdleStateEvent must be public.

Modifications:

Change access level of IdleStateHandler.IdleStateEvent to public.

Result:

IdleStateEvent will become a part of the NIO API.
2018-04-16 20:57:03 +02:00
Norman Maurer 178dd76503 Allow to specify the max websockets frame size when using the upgrader. (#315)
* Allow to specify the max websockets frame size when using the upgrader.

Motivation:

At the moment its not possible to adjust the max websockets frame size when using the upgrader while its possible when directly use the WebSocketFrameDecoder.

Modifications:

Allow to specify max frame size via an init argument (with default value).

Result:

More flexible way to use the upgrader.
2018-04-16 20:13:20 +02:00
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
Johannes Weiß dc0d7311da fix errors received when holding messages in HTTPServerPipelineHandler (#314)
Motivation:

We had a bug which is happens in the combination of these states:
- we held a request in the pipelining handler (because we're procesing a
  previous one)
- a http handler error happened whilst a response's `.head` had already
  been sent (but not yet the `.end`)
- the HTTPServerProtocolErrors handler is in use

That would lead to this situation:
- the error isn't held by the pipelining handler
- the error handler then just sends a full response (`.head` and `.end`)
  but the actual http server already send a `.head`. So all in all, we
  sent `.head`, `.head`, `.end` which is illegal
- the pipelining handler didn't notice this and beause it saw an `.end`
  it would send through the next requst
- now the http server handler is in the situation that it gets `.head`,
  `.head` too (which is illegal)

Modifications:

- hold HTTP errors in the pipelining handler too

Result:

- more correctness
2018-04-16 15:13:07 +01:00
Norman Maurer 7758140634 Reduce allocations when notify promises related to pending writes. (#308)
Motivation:

We returned an callback which needed to be executed by the caller. This will cause extra allocations and is not needed. We can just return a EventLoopPromise which will cascade the notification.

Modification:

Not return a closure but promise.

Result:

Less allocations.
2018-04-13 15:20:47 +02:00
Norman Maurer 098239bb27 Remove PendingWriteStateManager.chunks (#312)
Motivation:

chunks variable is not needed anymore.

Modifications:

Remove not needed variable

Result:

Less code FTW.
2018-04-13 07:49:27 +02:00
Norman Maurer 575e7686cf Optimize for HTTP/1.1 and HTTP/1.0 responses (#300)
Motivation:

Its very likely that the response will be HTTP/1.1 or HTTP/1.0 so we can optimize for it by minimizing the buffer.write(...) calls.
Beside this we should also change the pattern of *.write(into: inout ByteBuffer) to extension on ByteBuffer itself.

Modificiations:

- Optimize for HTTP/1.0 and 1.1
- Use extensions on ByteBuffer

Result:

Faster and more clean code.
2018-04-12 16:22:32 +02:00
Cory Benfield bc6e3db537 Fix up HTTP message framing edge cases. (#298)
Motivation:

HTTP message framing has a number of edge cases that NIO currently does
not tolerate. We should decide what our position is on each of these edge
cases and handle it appropriately.

Modifications:

Provide an extensive test suite that codifies our expected handling of
these edge cases. Fix divergences from this behaviour.

Result:

Better tolerance for the weird corners of HTTP.
2018-04-12 15:12:39 +01:00
Cory Benfield 3275ff7c9c Don't call to user code before reconciling Channel state. (#310)
Motivation:

Callouts to user code allows the user to make calls that re-enter
channel code. In the case of channel closure, we could call out to the
user before the channel knew it was completely closed, which would
trigger a safety assertion (in debug mode) or hit a fatalError
(in release mode).

Modifications:

Reconciled channel state before we call out to user code.
Added a test for this case.

Result:

Fewer crashes, better channel state management.
2018-04-12 13:49:09 +01:00
Johannes Weiß aee4aad3f9 fix a crash when async connect failed (#302) (#309)
Motivation:

When an asynchronous connect failed we would crash because we didn't
correctly close the connection to start tearing everything down.
That correctly made the `SocketChannelLifecycleManager` trip.

This was filed as #302.

Modifications:

Correctly deregister and close the connection if an async connect fails.

Result:

Fewer crashes, fixes #302.
2018-04-12 14:21:12 +02:00
Cory Benfield 241aed70c8 Restore unary initializer for HTTPHeaders (#301)
Motivation:

The Swift compiler seems to get very nervous when variadic inits
are used for types that have constructors with optional values. In
general we're not worried about breaking downstream consumers'
extensions when we update our code, but in this case we break the
ability to conform HTTPHeaders to ExpressibleByDictionaryLiteral,
which is a bit annoying. See https://bugs.swift.org/browse/SR-7415
for more details.

For 1.5.0 we should conform HTTPHeaders ourselves, but until that
time we can restore anyone who was conforming HTTPHeaders to
ExpressibleByDictionaryLiteral by restoring the old unary initializer
and delegating it to the new one. This presents an effect that is
equivalent to the old behaviour, but is new.

As a side note, in general it's a bad idea to conform types that you
do not own to standard library protocols. NIO reserves the right to
add conformances to our types in any Semver Minor release, so having
that conformance in your own code risks breakage without a Semver
Major patch bump. Please be aware.

Modifications:

Restored the unary initializer for HTTPHeaders.

Result:

Less breakage, more happiness.
2018-04-11 17:55:00 +01:00
Johannes Weiß d94ab56d58 workaround ab (ApacheBench) in keep-alive mode (#260)
Motivation:

`ab -k` behaves weirdly: it'll send an HTTP/1.0 request with keep-alive set
but stops doing anything at all if the server doesn't also set Connection: keep-alive
which our example HTTP1Server didn't do.

Modifications:

In the HTTP1Server example if we receive an HTTP/1.0 request with
Connection: keep-alive, we'll now set keep-alive too.

Result:

ab -k doesn't get stuck anymore.
2018-04-10 18:28:10 +02:00
Norman Maurer b6681c0daf TailChannelHandler does not need to implement _ChannelOutboundHandler (#297)
Motivation:

TailChannelHandler does not implement any _ChannelOutboundHandler methods and so has no need to conform to it.

Modifications:

Not implement _ChannelOutboundHandler for TailChannelHandler

Result:

More correct code.
2018-04-10 16:07:53 +02:00
Norman Maurer 33facfe0dc Stop calling read(...) if we were not able to fill the whole buffer. (#296)
Motivation:

If we not filled the whole buffer with read(...) we should stop reading and wait until we get notified again. Otherwise chances are good that the next read(...) call will either read nothing or only a very small amount of data.
Also this will allow us to call fireChannelReadComplete() which may give the user the chance to flush out all pending writes.

Modifications:

Stop reading and wait for next wakup when we not fill the whole buffer.

Result:

Less wasted read calls and early chance of flushing out pending data.
2018-04-10 15:44:46 +02:00
Norman Maurer 48cef4a59b Implement lazy headers parsing (#291)
Motivation:

We are currently parsing each header eagly which means we need to convert from bytes to String frequently. The reality is that most of the times the user is not really interested in all the headers and so it is kind of wasteful to do so.

Modification:

Rewrite our internal storage of HTTPHeaders to use a ByteBuffer as internal storage and so only parse headers on demand.

Result:

Less overhead for parsing headers.
2018-04-10 14:57:58 +02:00
Karim ElNaggar 39b9a3d60b Add EventLoopFuture reduction primitives (#240)
* Add EventLoopFuture reduce(initialResult:) function (#240)

Motivation:

Add a new function to allow us to work with multiple EventLoopFutures, that return values, by combining their results and wrapping it in a new
EventLoopFuture, using a specified reducer function.

Modifications:

- Add a new function
EventLoopFuture<T>.reduce<U>(initialResult:futures:eventLoop:nextPartialResult)
- Changed the implementation of andAll<Void> to use
EventLoopFuture<Void>.reduce(initialResult)
- Add a new function EventLoopFuture<T>.reduce<U>(into:)

Result:

Can reduce multiple EventLoopFutures without having to deal with
creating EventLoopFutures.

Simplified EventLoopFuture andAll<Void>

- An API which can be used to construct collections from future values in a linear time.
2018-04-09 17:42:35 +01:00
Johannes Weiß 1506a1901c make generic/closure ByteBuffer methods inlineable (#266)
Motivation:

Generics and closures across modules can be problematic as the cause
allocations. ByteBuffer is one of NIO's core data types and therefore
should be well optimised. Generic/closure taking methods so far were a
bit of a problem. And with
[SE-0193](https://github.com/apple/swift-evolution/blob/master/proposals/0193-cross-module-inlining-and-specialization.md)
accepted, there's not much reason to use those features even though they
are (at the moment) @_.

For the 1000 requests over 1 connection allocation benchmark we go from

    "total_allocations": 325378

to

    "total_allocations": 321366

so we lose 4 allocations per HTTP request/response pair, not super
impressive but not nothing either.

Modifications:

Make generic/closure taking methods on ByteBuffer `@_inlineable`

Result:

Fewer allocations
2018-04-09 15:11:01 +02:00