Motivation:
We should be consistent with naming and also choose descriptive names.
Modifications:
- Deprecate old init method that uses numThreads
- Add new init with numberOfThreads param name
- Everywhere use the new init
Result:
More consistent and descriptive naming. Fixes https://github.com/apple/swift-nio/issues/432.
Motivation:
Once again, we had an extra event loop hop between a channel
registration and its activation. Usually this shows up as `EPOLLHUP` but
not so for accepted channels. What happened instead is that we had a
small race window after we accepted a channel. It was in a state where
it was not marked active _yet_ and therefore we'd not read out its data
in case we received a `.readEOF`. That usually leads to a stale
connection. Fortunately it doesn't happen very often that the client
connects, immediately sends some data and then shuts the write end of
the socket.
Modifications:
prevent the event loop hop between registration and activation
Result:
will always read out the read buffer on .readEOF
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.
Motivation:
We had a few issues with ByteBuffer methods allocating without us
knowing, the only way to make sure we don't regress is obviously a test.
Modifications:
add a ByteBuffer allocation counter test
Result:
less room for regressions.
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.
Motivation:
Sometimes it's useful if the integration tests can output something that
isn't always useful. `-v` for verbose is way too verbose so this adds a
`-i` flag to the integration tests that lets 'info' messages through.
Modifications:
- add `-i` flags to integration tests
- make use of it in the integration counter tests
Result:
if we enable `-i` in CI, then we can constantly monitor the number of
allocations we do even if we don't fail the tests.
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
Motivation:
Previously the integration tests ran in verbose mode wouldn't actually
fail as we didn't correctly propagate the exit code.
Modifications:
correctly propagate the exit code
Result:
integration tests correctly fail in verbose mode too
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.
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.
Motivation:
We have one integration test left disabled, but actually it basically
already passes: it just expects the wrong format of error. We should
have all our integration tests enabled.
Modifications:
Enabled the test.
Changed the wording of the error it expected.
Result:
No disabled integration tests.
Motivation:
Currently the HTTP decoders can throw errors, but they will be ignored
and lead to a simple EOF. That's not ideal: in most cases we should make
a best-effort attempt to send a 4XX error code before we shut the client
down.
Modifications:
Provided a new ChannelHandler that generates 400 errors when the HTTP
decoder fails.
Added a flag to automatically add that handler to the channel pipeline.
Added the handler to the HTTP sample server.
Enabled integration test 12.
Result:
Easier error handling for HTTP servers.
Motivation:
Previously, the integration test's verbose mode would only shows the
output after the test has run, but a live view is better.
Modifications:
Made the integration test's verbose output live.
Result:
Easier to debug the integration tests.
Motivation:
The allocation counter integration tests previously used a pretty crude
malloc implementation for the case that malloc is needed whilst we
resolve malloc from libc using dlsym. This implements a much better
version and also allows us to actually use free. Previously we just
leaked all the memory (which is okay as its just a test but in
constrained environments this wouldn't scale as we might actually run
out of memory). The reason we used to leak the memory is that given a
pointer we couldn't figure out if that pointer was alloated using libc's
malloc or our crude malloc implementation so we didn't know how to free.
The new version fixes that as all the pointers vended from
recursive_malloc live in one global block. That makes it trivial to
check if a given pointer is allocated by recursive_malloc (lives in that
block) or libc's malloc (doesn't live in the recursive_malloc block).
Modifications:
- implemented a better recursive_malloc function
- make the allocation counter test actually free memory that can be
freed
Result:
Less memory use in the allocation counter test.
Motivation:
Previously we tried to use `host` to determine localhost's IPv4 address.
That didn't work reliably in Docker and sometimes on macOS. Let's just
fix this to 127.0.0.1.
Modifications:
Hardcode localhost's IPv4 address as 127.0.0.1.
Result:
More stable test_16_tcp_client_ip
Motivation:
In shell, subshells don't inherit the shell flags (such as `set -eu`)
and therefore need to set them again. The allocation counter test uses a
subshell but forgot to `set -eu` in the subshell.
Modifications:
`set -eu` in the subshell for the allocation counter tests.
Result:
if there's an error building the test program the test now fails.
Motivation:
We have code running on multiple threads and certain allocations/frees
happen for example after a promise has been fulfilled. We can't
deterministically wait for that so the only thing we can do is sleep a
little and wait...
Modifications:
Added a 100ms delay after an allocation counted test run.
Result:
Hopefully we don't see this again:
["total_allocations": 322051, "remaining_allocations": 24], ["total_allocations": 322051, "remaining_allocations": -24]
where the first test run had 24 allocations that weren't freed and the
second one on the other hand had 24 frees for allocations that "didn't
happen"...
Motivation:
Memory leaks are rare in Swift but they can happen. If we run a
realistic workload and the number of `malloc`s and `free`s is very close
to 0, we can be fairly sure we didn't have any (too bad) leaks.
Therefore this adds a simple leaks detector.
Modifications:
Add a leak detector.
Result:
We should now detect leaks even if the total number of allocations
doesn't increase.
Motivation:
In Swift, it's really easy to accidentally add allocations yet the
number of allocations is really important for performance.
Therefore it's useful to have an integration test that counts the number
of allocations and compares them to a reference (and fails if the number
of allocations increases).
Modifications:
- add an allocation counter
- add a HTTP1 client/server example that is run with the allocation
counter intrumentation
Result:
We should now be able to track the number of allocations.
Motivation:
HTTP pipelining can be tricky to handle properly on the server side.
In particular, it's very easy to write out of order or inconsistently
mutate state. Users often need help to handle this appropriately.
Modifications:
Added a HTTPServerPipelineHandler that only lets one request through
at a time.
Result:
Better servers that are more able to handle HTTP pipelining
Motivation:
Our shell scripts didn't have license headers but they should.
Modifications:
Add licensing headers to all shell scripts
Result:
Licensing clear for shell scripts too.
Motivation:
Currently our HTTP server almost always just assumes that keep-alive
is enabled. It really shouldn't, it should validate more carefully.
Additionally, it mishandles TCP half-close in a way that breaks with
netcat.
Modifications:
Added keep-alive tracking to all handlers and ensured that connections
will be closed after a response to a keep-alive request has been sent.
Also add support for handling TCP half-close.
Result:
Non-keep-alive connections will be closed by the server in all cases.
Half-closed connections will be appropriately managed.
Motivation:
The integration tests didn't work on Darwin anymore as the `CNIODarwin`
module wasn't included in their build.
Modifications:
Included the `CNIODarwin` module, just like `CNIOLinux`.
Result:
integration tests run on Darwin