Motivation:
The HTTPDecoder is a complex object that has very careful state management goals. One source of this
complexity is that it is fed a stream of bytes with arbitrary chunk sizes, but needs to produce a
collection of objects that are contiguous in memory. For example, each header field name and value
must be turned into a String, which requires a contiguous sequence of bytes to do.
As a result, it is quite common to have a situation where the HTTPDecoder has only *part* of an
object that must be emitted atomically. In this situation, the HTTPDecoder would like to instruct
its ByteToMessageHandler to keep hold of the bytes that form the beginning of that object. To avoid
asking http_parser to parse those bytes twice, the HTTPDecoder uses a value called httpParserOffset
to keep track.
As an example, consider what would happen if the "Connection: keep-alive\r\n" header field was delivered
in two chunks: first "Connection: keep-al", and then "ive\r\n". The header field name can be emitted in
its entirety, but the partial field value must be preserved. To achieve this, the HTTPDecoder will store
an offset internally to keep track of which bytes have been parsed. In this case, the offset will be set
to 7: the number of bytes in "keep-al". It will then tell the rest of the code that only 12 bytes of the
original 19 byte message were consumed, causing the ByteToMessageHandler to preserve those 7 bytes.
However, when the next chunk is received, the ByteToMessageHandler will *replay* those bytes to
HTTPDecoder. To avoid parsing them a second time, HTTPDecoder keeps track of how many bytes it is
expecting to see replayed. This is the value in httpParserOffset.
Due to a logic error in the HTTPDecoder, the httpParserOffset field was never returned to zero.
This field would be modified whenever a partial field was received, but would never be returned
to zero when a complete message was parsed. This would cause the HTTPDecoder to unnecessarily keep
hold of extra bytes in the ByteToMessageHandler even when they were no longer needed. In some cases
the number could get smaller, such as when a new partial field was received, but it could never drop
to zero even when a complete HTTP message was receivedincremented.
Happily, due to the rest of the HTTPDecoder logic this never produced an invalid message: while
ByteToMessageHandler was repeatedly producing extra bytes, it never actually passed them to http_parser
again, or caused any other issue. The only situation in which a problem would occur is if the HTTPDecoder
had a RemoveAfterUpgradeStrategy other than .dropBytes. In that circumstance, decodeLast would not
consume any extra bytes, but those bytes would have remained in the buffer passed to decodeLast, which
would then incorrectly *forward them on*. This is the only circumstance in which this error manifested,
and in most applications it led to surprising and irregular crashes on connection teardown. In all
other applications the only effect was unnecessarily preserving a few tens of extra bytes on
some connections, until receiving EOF caused us to drop all that memory anyway.
Modifications:
- Return httpParserOffset to 0 when a full message has been delivered.
Result:
Fewer weird crashes.
(cherry picked from commit ae3d2988f8)
Motivation:
There are a pair of vars that should be lets in the heap tests.
Modifications:
- Moved the var to let
Result:
No test warnings
(cherry picked from commit 12654afd21)
Motivation:
CircularBuffer.Index was asserting that its size is the same as a word.
Whilst that's true on 64-bit architectures, the assert itself is bogus
and a left-over from development.
Modifications:
Remove bogus assert.
Result:
Better 32-bit support.
(cherry picked from commit 2cfa777013)
Motivation:
The file-io implementation in the example HTTP1 server just crashed on
empty file ;).
Modifications:
- fix the crash
- add integration test
Result:
fewer crashes
(cherry picked from commit c90b159b4f)
Motivation:
Our original Heap implementation used a bunch of static functions
in order to make it clear how it related to textbook heap
implementations. This was primarily done to ensure that it was easier
to see the correctness of the code.
However, we've long been satisfied with the correctness of the code,
and the Heap is one of the best tested parts of NIO. For this reason,
we should be free to refactor it.
Given https://bugs.swift.org/browse/SR-10346, we've been given an
incentive to do that refactor right now. This is because the Heap
is causing repeated CoW operations each time we enqueue new work
onto the event loop. That's a substantial performance cost: well
worth fixing with a small rewrite.
Modifications:
- Removed all static funcs from Heap
- Rewrote some into instance funcs
- Merged the implementation of insert into append.
- Added an allocation test to avoid regressing this change.
Result:
Faster Heap, happier users, sadder textbook authors.
(cherry picked from commit 0179535b99)
Motivation:
The documentation popup for viewBytes did not correctly
show what the method returns.
Modifications:
I added a colon after the return doc-block keyword.
Result:
The documentation popup for viewBytes will now correctly show
what the method returns.
Motivation:
B2MDs must be robust regarding data after EOF, just in case. Let's add
test cases for that.
Modifications:
add two tests that send a B2MD data after EOF.
Result:
we're sure we're okay with that.
Motivation:
When first viewing the example classes, coming to the ‘read’ method first, leaves the subject unclear as to what the method is ‘reading’.
It is preferable to view something being sent first, and then to view the reading of the response.
It also matches the call order against the protocol making it a little easier for those unfamiliar with the protocol to see which methods have been implemented.
Modifications:
Moved channel active calls to be top of the class.
Despite the diff there are no actual code modifications.
UDP Client changed to indent using spaces to match rest of project. Incidental change.
Result:
The examples are slighter clearer to read, particularly for newcomers to swift-no as the calls are in a logical chronological order.
Motivation:
To provide a simple example of the HTTP1 client pipeline.
To create a comparable example to the TCP and UDP demos to show what extra steps are needed for a HTTP layer.
Modifications:
Adds a NIOHTTP1Client example in a main file.
Adds a readme to the NIOHTTP1Client example.
Result:
There is now a simple example of how to send a HTTP1 client request to the server.
Motivation:
@tomerd learned the hard way that there were some subtle changes in
B2MDs around intercepting arbitrary channel events. Previously,
intercepting those was illegal but possible. In NIO 2, it's impossible.
Modifications:
add a description for what changed
Result:
easier migrations
Motivation:
B2MD (in debug mode) has some checking that we never move out of a final
state in the state machine. Both .done and .error are final states.
Previously however, the code set .done in a defer block which meant that
even if the decodeLoop throws (which should bring it to .error), it goes
via .done first. That's an error because .done is already a final state
so we shouldn't move to .error after that.
Modifications:
Only move to .done if there was no error. If there was an error, we
should go straight to .error, not via .done.
Result:
- more correct B2MD
- fewer crashes (in debug mode)
* Significantly improve the performance of `EventLoopFuture.{and,when}AllSucceed`.
* PR improvements.
* Add the fast track for `AllComplete` as well.
* Use `future.inEventLoop` instead of pointer comparison.
Add missing server readme docs for WebSocket server and UDP echo server.
Motivation:
With the exception of these two samples, all client and server examples have helpful readme docs.
Provides brief description of purpose of the sample code and how to run it.
Modifications:
Adds a readme to NIOUDPEchoServer.
Adds a readme to NIOWebSocketServer.
Result:
The samples for NIOUDPEchoServer and NIOWebSocketServer have a little explanation.
Motivation:
There is a UDP echo client which is very similar to the NIOEchoClient.
The styles of these two examples should match closely so ‘diff’ comparing will show the pertinent API differences.
Modifications:
Add self tags to member variables in the TCP echo client.
Add a self tag to a member variable in the HTTP1 server.
Remove an assert from the echo client.
Result:
The code in the TCP and UDP examples now matches and fits the SwiftNIO style.
* UDP echo client example.
Motivation:
There is a UDP echo server example but it does not currently have a machine client example.
The TCP echo server has both sample client and server examples and it would be preferable to match.
The UDP client connection also differs from both the UDP server and the TCP client, so it is good to provide and example of this difference.
Modifications:
Added a UDP client example and a matching readme file.
The main swift file is a similar to the TCP echo example as possible.
Result:
There is now a clear example of how to create a simple UDP client side connection.
Motivation:
Our podspecs should also correctly require Swift 5.
Modifications:
require Swift 5 in the podspec generator.
Result:
better podspecs for NIO2.
Motivation:
A bunch of things change with the release of NIO 2. We should update the
documentation to reflect that.
Modifications:
- update readme
- update migration guide
Result:
up to date docs.
Motivation:
Spotted a couple of issues with the documentation and fixed them.
Modifications:
- explicitly marked all `public class`es as `public final class` to get
consistency in the Jazzy documentation.
- removed `public` extension methods of the `internal struct
PriorityQueue` which Jazzy showed in the docs.
- improved the largely missing `EmbeddedChannel` and `EmbeddedEventLoop`
documentation.
- clear up lies in the B2MD docs
- add some other missing docs
Result:
better docs makes happier users
Motivation:
For whatever reason, Scheduled stored the cancellationTask in two
places, one of them it never even used :).
Modifications:
only have the promise have a reference to the cancellationTask
Result:
- don't hold onto things longer than necessary
- shrink Scheduled by one word :)
Motivation:
EmbeddedChannel.finish/writeInbound/writeOutbound returned some mystery
bools. I always had to check the code to remember what they actually
meaned.
Whilst this is technically a breaking change, I couldn't find any users
of the return values on Github that are using the convergence releases.
Modifications:
Replace them by enums giving you all the information.
Result:
- fixes#916
- clearer APIs
The README file example list is missing a couple of examples which are actually in the project, namely the UDP and WebSocket server example projects.
Modifications:
Added UDP and WebSocket server example projects to the README.
Result:
A full list of examples is now included in the README file.
Motivation:
Jazzy adds everything that has a `public` modifier to the documentation,
even if a type is `internal` or `private`. So conforming and internal
type to some protocol and making all those functions `public` has the
effect of essentially documenting stuff that you can't call (as it's not
actually public).
Modifications:
- remove `public` from functions in extensions on types that aren't
public
- change one `final public` to `public final` because Jazzy doesn't
normalise that
Result:
better docs
Motivation:
In the new CircularBuffer indices we had 24 spare bits, let's do
something useful with them: If it fits we can store the expected
collection size there and compare when an element is accessed through
that index.
Modifications:
add collection count to CircularBuffer.Index
Result:
we can somewhat validate the correct use of CircularBuffer indices
* make HTTPMethod.hasRequestBody internal
Motivation:
HTTPMethod.hasRequestBody was unnecessarily public.
Modifications:
make it internal
Result:
fixes#151
* Update public-api-changes-NIO1-to-NIO2.md
Motivation:
Basically I just wasn't 100% sure that I implemented that the EOF events
are not only handled but also forwarded to the next handler in the
pipeline. Quite important.
Modifications:
To figure out if I had implemented that or not I just wrote a test which
this PR is adding.
Result:
One more test, nothing else. Turns out I had implemented that correctly.
Motivation:
ByteBufferView should conform to DataProtocol.
Modifications:
Conform ByteBufferView to DataProtocol.
Result:
ByteBufferView conforms to DataProtocol.
Motivation:
CircularBuffer and its indices should be small. Also the implementation
of methods like remove/popFirst/Last was suboptimal.
Modifications:
- shrink CircularBuffer and its indices
- improve the conformances to the various collection related protocols
- make CircularBuffer self-slicing
- remove Strideable conformance from the indices
Result:
nicer CircularBuffer
Motivation:
Provide a way for CircularBuffer to directly access underlying buffer with an index instead of needing to calculate depending on current head index and underlying buffer count. Related issue: #682
Modifications:
Added needed properties to circular buffers Index and adjust CircularBuffer code accordingly.
Removed MarkedCircularBuffer assumption that an index into it's underlying buffer will not be invalidated.
Result:
CircularBuffer will have a complete Index implementation
Motivation:
swift-nio-http2 and swift-nio-transport-services are now converging.
Modifications:
Updated the migration guide.
Result:
Docs will be up to date.
Motivation:
B2MH used to crash a nil variable if re-added to a pipeline.
Modifications:
provide good error message for the crash instead
Result:
- fixes#902
Motivation:
For historic reasons, the get* implementations checked the same indices
repeatedly and the checks were also repetitive.
Modifications:
unify and de-deplicate get* range checks
Result:
fixes#884
Motivation:
Before native UTF-8, the rawURI was a performance optimisation but
that's no longer true. Before this patch, we still had the rawURIs but
weren't using them at all.
Modifications:
remove any mention of rawURI
Result:
less clutter
Motivation:
ByteBuffer's get* methods are now safe, docs claimed they were unsafe
though.
Modifications:
Document the gets as safe
Result:
More correct docs.