Motivation:
fireChannelActive(...) must always be called before fireChannelRead(...) which was not the cause all of the times as once ChannelOptions were applied to the accepted SocketChannel it was possible that read0() was called during this. This would then lead to an assert error as it tried to reregister on the Selector before we ever registered.
Modifications:
- Ensure we only try to reregister once we have registered to the Selector
- Only trigger read0() or pauseRead0() if the autoRead value actually changed
- Add unit test
Result:
Correct order of events and no crashes.
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
Motivation:
Changes the Boolean `fulfilled` to `isFulfilled` to make it more clear it is in fact a Boolean.
Also changes a `func isRecoverable()` to `var isRecoverable`.
Both of these were non-public, so this is no breaking change.
Modifications:
Renamed one Boolean and changed one function to a variable.
Result:
No use of unncessary (and potentially misinforming) functions, as well as adhering to the Swift boolean naming guidelines.
Motivation:
There are quite a few `switch`es that cover just two cases. Explicitly state this via an `if case` + `else` structure.
Modifications:
Changes something like:
switch a {
case .b:
return c
default:
return d
}
to
if case .b = a {
return c
} else {
return d
}
Result:
This should prevent misinterpretation and thus potential bugs in the future.
Motivation:
Removes unncessary explicit types that are inferred.
Modifications:
Infer types that are being returned from a function or computed variable.
Result:
Cleans up the code.
Motivation:
This changes two things:
- it removes the negation of non-used associated types (the removal of `(_)`).
- it prefers exhausting `switch`es over `default`ing.
This change does not impact bahavior; it is pure refactoring.
Modifications:
Omitting needless `(_)`'s and explicitly exhausting switches.
Result:
This change is pure refactoring.
Motivation:
We not always ensure correct ordering which made it quite hard to reason about things. We should always first notify the promise before we call the fire* method that belongs to the event. Beside this we sometimes fired events or notified promised before correctly update the active state / addresses of a Channel which could result in unexpected results when query the Channel during execution of callbacks and handlers.
Modifications:
- Ensure we always notify promise first
- Always correctly update channel state before notify promise
- Add test to verify notification order.
Result:
Correct ordering of events which makes things easier to reason about and which follows what netty is doing.
Motivation:
Testing specific ordering behaviours (e.g. cancelling tasks) requires
that EmbeddedEventLoop behave as similarly as possible to the full
event loop.
Modifications:
Adopt the batched scheduled task dispatch logic from the regular
event loop.
Result:
Easier to test subtle behaviours around task dispatch.
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.
Motivation:
No need for the "self-invoking function" here.
Modifications:
Call fatalError directly.
Result:
18 bytes are saved in the source code. No functional
difference.
Motivation:
NIO doesn't compile on iOS. Which I'd like to do.
Modifications:
The #if's used to protect the syscall do not match
the #if's used to import.
Specifically the Darwin branch only applied to
macOS, not watchOS or homePodOS.
Result:
Hopefully will compile on iOS.
Motivation:
There's no reason for us to emit compiler warnings.
Modifications:
Remove use of _isDebugAssertConfiguration to guard code, instead
use a hand-rolled hacky solution on top of assert.
Result:
Fewer warnings, more sadness.
Motivation:
A somewhat common requirement when working with chains of futures
is needing to hop from one event loop to another. This is particularly
common when relying on the fact that EventLoopFuture will synchronize
with the event loop that created it: you often want to rely on that
implicit locking, rather than use Dispatch or Lock to achieve the
same effect.
While doing this hop requires relatively little code, it's not
necessarily totally apparent to new users how they would do it.
Additionally, the most naive implementation incurs the overhead of
allocations and reference counting in cases where it's not necessary
(e.g. when you have only one event loop, or when both work items are
being scheduled on the same event loop).
For this reason, we should have a nice concise way for a user to
request this behaviour and get a relatively performant implementation
of the behaviour.
Modifications:
Added EventLoopFuture<T>.on(eventLoop:).
Changed AcceptHandler to use the new method rather than its (slower)
alternative.
Result:
Users will have an easier time working with EventLoopFutures.
Motivation:
EventLoopFuture.and had serious threading issues if the EventLoops
weren't the same.
Modifications:
Fixed the threading issues and tested them properly.
Result:
Hopefully `and` and `andAll` now don't crash if you use them across
EventLoops.
Motivation:
We should not close the DatagramChannel when recvfrom fails with ECONNREFUSED / ENOMEM. The former may be caused by a previous sendto and the other may be recoverable by just stop reading for some time.
Modifications:
- Special handle ECONNREFUSED and ENOMEN to not close the channel.
- Add unit tests
Result:
More robust datagram support.
Motivation:
For servers NIO had a race where when you start tearing down the
`ServerSocketChannel` and at roughly the same time a client connects, we
would not fully bring that channel up, nor tear it down. That lead to
open channels being deallocated which leads to a crash as that's an
illegal state.
Modifications:
Made sure newly accepted channels get closed if the
`ServerSocketChannel` is already clsing.
Result:
No crashes anymore when a client connects to a closing server.
Motivation:
We returned port numbers directly from the sockaddr structures but
they're in network byte order. Our code only read them correctly when
presenting, oops.
Modifications:
Corrected the port number reading and tested that.
Result:
Port numbers should now be correct.
Motivation:
Often accept errors are recoverable over time as for example you may just run out of file descriptors. Because of this it may be useful to just "delay" accepts a bit to recover.
Modifications:
Add ChannelHandler which will backoff accepts.
Result:
Be able to recover from accept errors gracefully.
Motivation:
At the moment its possible that channelRead is triggered even if autoRead is never changed to true and no read() is triggered.
Modifications:
- Always register with .none on the Selector and then just call readIfNeeded0()
- Only register for EPOLLRDHUP if .read, .write or .all is used.
- Add test case.
Result:
Fixes https://github.com/apple/swift-nio/issues/160.
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]
Motivation:
It is often useful to find a handler of a given type even when you
aren't holding a reference to that handler. This patch allows us to
do that.
Modifications:
Add context(handlerType:) function to the channel pipeline.
Result:
Users can find handlers of a given type in a pipeline.
Motivation:
We should not close the server socket when accept fails with ECONNABORTED / EMFILE / ENFILE, ENOBUFS. ENOMEM as generally speaking these may be "recoverable" once some existing connections were closed for example. It's possible to implement a backoff strategy for these with a ChannelHandler.
Modifications:
- Special handle of ECONNABORTED / EMFILE / ENFILE, ENOBUFS. ENOMEM for ServerSocketChannel.
Result:
Not close servet socket for errors from which it is possible to recover.
Motivation:
Reduce memory footprint of BaseSocket
Modifications:
- made isOpen a computed var from 'descriptor >= 0'
- BaseSocket init check precondition 'descriptor >= 0'
Result:
- isOpen no longer requires storage space
- close() sets descriptor to -1
- descriptor property changed from let to var
Motivation:
Currently we only supporting adding channel handlers at the front
or the end of the pipeline. This is potentially a bit frustrating:
it becomes impossible to insert handlers relative to a specific
point in the pipeline, which limits users ability to code generic
pipeline modification operations.
Modifications:
Added two new methods to ChannelPipeline, specifically
add(handler:before:) and add(handler:after:). Also performed a
substantial internal refactoring of the ChannelPipeline handler
adding code to ensure that all operations go through the same
logical path, redefining add(handler:first:) in terms of adding
either before tail or after head.
Result:
Users will be able to insert channel handlers at any point in a
ChannelPipeline, so long as they are able to point at a handler
already in that pipeline.
Motivation:
Reduce system calls on Linux required for configuring sockets as
nonblocking.
Modifications:
- expose accept4 as CNIOLinux_accept4
- add Linux internal enum for exposing Linux specific API
- wrap CNIOLinux_accept4 with Linux.accept4
- expose SOCK_NONBLOCK and SOCK_CLOEXEC on Linux
- add setNonBlocking parameter to BaseSocket.newSocket
- add setNonBlocking parameter to ServerSocket.accept
Result:
Servers and clients on Linux will use fewer system calls creating new sockets.
Motivation:
Reduce memory footprint of BaseSocket and FileHandle
Modifications:
- made isOpen a computed var from 'descriptor >= 0'
- add withSocketpair to TestUtils
- use real pipe and socketpair file descriptors for tests
- BaseSocket and FileHandle init check precondition 'descriptor >= 0'
Result:
- isOpen no longer requires storage space
- close() sets descriptor to -1
- descriptor property changed from let to var
- tests will need to use valid file descriptors
Motivation:
We can cleanup the code by removing return keywords in closures sometimes.
Modifications:
Remove return when possible.
Result:
Cleaner code.
Motivation:
Right now addHandlers(first:) does not actually add handlers at the
front of the pipeline: it just adds them *backwards* at the end of
the pipeline. That's definitely not right.
Modifications:
Pass the first: flag into the call to add each specific handler to
ensure they're actually put at the front of the pipeline.
Result:
Handlers added to the front of the pipeline actually are.
Motivation:
Fix for issue #110, .description on IPv6 addresses
return a String with 0 codepoints in it, like so:
[IPv6]::1<NUL><NUL><NUL>....lots..
Modifications:
Use String(cString:) instead of decode, which doesn't
care about \0-cstr terminators.
Result:
Issue fixed, everything is awesome, everyone is happy.
Motivation:
It should be possible to have channels that are not registered
for any form of I/O without them getting stuck in that model
forever.
Modifications:
Remove the code that prevents channels registered for `.none` from
registering for anything else.
Result:
Channels can actually be registered for nothing without becoming
wedged open forever.
Motivation:
It's often useful for users to be able to store a thread-specific value (and so also EventLoop specific). We have ThreadSpecificVariable for this which was internal.
Modifications:
Change ThreadSpecificVariable to be public.
Result:
Fixes [#61].
Motivation:
Thread is declared final, so I believe spawnAndRun should be static.
Modifications:
Change class func to static
Result:
More consistent code, and spawnAndRun is statically dispatched.
Motivation:
We should just use one Box to store both, the local and the remote address. This will remove some heap allocations. And reduce memory overhead.
Modifications:
Use a tuple which holds the local and remote address and so only use one Box for the cached addresses.
Result:
Less heap allocations and less memory usage.
Motivation:
There are a few network protocols that are best implemented with child
`Channel`s. And given that those child `Channels` need a
`ChannelPipeline` the init for `ChannelPipeline` should be public.
Otherwise these protocols can only be sensibly implemented in the core
`NIO` module which is unfair.
Modifications:
make `ChannelPipeline.init` public
Result:
modules other than `NIO` can implement child `Channel`s.
Motivation:
Sometimes getting read/getInteger to guess the type correctly can be
annoying and it's handy to just say `as: UInt8.self` or similar. In
other cases it's not as the type is clear from the environment. This now
enables both, just like we already have for `write/set(integer:)`
Modifications:
added an `as: T.Type = T.self` argument which is optional as it has a
default
Result:
easier to use `ByteBuffer` interfaces
Motivation:
We missed to correctly update the cached remote and local addresses for accepted Channels. Because of this localAddress and remoteAddr always returned nil.
Modifications:
- Update cached addresses when constructing SocketChannel from existing Socket.
- Add testcase
Result:
Fixes [#97].
Motivation:
The `addHandler` method is useful for creating extensions to add handlers similar to what Swift NIO offers w/ HTTP. For example:
```swift
extension ChannelPipeline {
func addPostgreSQLClientHandlers(first: Bool = false) -> EventLoopFuture<Void> {
return addHandlers(PostgreSQLMessageEncoder(), PostgreSQLMessageDecoder(), first: first)
}
}
```
Result:
Array and variadic accepting `ChannelPipeline.addHandler` methods are now available when importing the NIO module.
Motivation:
When writing programs that use networks, particularly if you want to
perform multicast joins, it's extremely important to be able to query
network interfaces and get results.
Modifications:
Add support for querying interfaces using getifaddrs and wrap those query
results in a Swift class.
Result:
Users will be able to enumerate the interfaces on their machine.
Motivation:
Previously if we'd futureOnEL1.then { ... in futureInEL2 } we'd forget
to hop from EL1 to EL2 which is bad. Shout out to @tanner0101 for
reporting!
Modifications:
Hop from one EventLoop to another if needed.
Result:
thens between Futures of different EventLoops now correctly switch
EventLoops.