Commit Graph
2140 Commits
Author SHA1 Message Date
Ankit Aggarwal 51c59907fb Some random Swiftyness (#66) 2018-03-05 19:22:45 +09:00
Arjun Nayini 5c2d98cd8f Clean up some code style violations. (#76)
Motivation:

Get the code to adhere better to style standards.

Modifications:

Fixed colon violation, unused arg, unneeded initialization,
and spacing nits.

Result:

No behavioral change, only code style changes.
2018-03-05 19:15:54 +09:00
Johannes Weiß 94c21b6043 write/set(integer:) should like read/getInteger have an option as: argument so types can be specified (#82)
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
2018-03-05 19:01:10 +09:00
Norman Maurer f3d3267977 Correctly update cached addresses for accepted Channels. (#98)
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].
2018-03-05 18:44:14 +09:00
Ben Cohen 2c1d993978 Rename PriorityQueue's T to Element and iterator to Iterator, avoiding need for typealiases. (#91)
Motivation:

We've found following these conventions in the standard library simplifies
the code significantly if you create a lot of generic types that conform
to the std lib protocols.

Modifications:

Change the placeholder T to Element and remove now-unnecessary typealias.
Switch the name of the iterator to just Iterator, and have it re-use
the outer type's Element placeholder. Remove now-unnecessary typealiases.
Add deprecated typealiases to make this a non-breaking change.

Result:

Should be a non-functional change. Explicit typing outside the PriorityQueue
type would need to change, but there wasn't any.
2018-03-04 08:36:15 -08: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
Tanner 5ff6d9ba72 Publicizes variadic / array ChannelPipeline.addHandler methods and moves them from NIOHTTP1 to NIO. (#79) (#79)
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.
2018-03-03 11:03:22 +09:00
Ian Partridge da65c4f629 Define ChannelDuplexHandler as a typealias (#74) 2018-03-03 10:16:39 +09:00
Johannes Weiß b4d2bfc720 default example servers to use as many threads as cores (#78)
Motivation:

Our example servers used an arbitrary number of threads (the http one
just 1 :|) but `System.coreCount` is a much better default and also
demoes that API.

Modifications:

Changed the example servers to use `System.coreCount` threads.

Result:

Use more of the hardware you paid for.
2018-03-03 10:09:41 +09:00
Cory Benfield f3f37af4ae Support enumerating network interfaces. (#47)
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.
2018-03-03 09:04:57 +09:00
Ian Partridge 1167841b6f typo: ChannnelDuplexHandler should be ChannelDuplexHandler (#69) 2018-03-02 21:56:33 +09:00
Johannes Weiß 7402483b8d fix EventLoop-hopping thens (#51) (#63)
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.
2018-03-02 17:51:03 +09:00
adamnemecek 0bbed6d72f * removed trailing whitespace (#52) 2018-03-02 15:40:52 +09:00
Norman Maurer 613998ba9e Add ChannelDuplexHandler protocol (#65)
Motivation:

Sometimes a user needs to implement ChannelInboundHandler and ChannelOutboundHandler. We should remove some of the "boilerplate" code for doing so.

Modifications:

- Add ChannelDuplexHandler and use it.

Result:

Less boilerplate code.
2018-03-02 14:52:42 +09:00
Sebastian Thiebaud 4cfbbee94a Fix typo for Bootstrap’s resolver parameter (#53) 2018-03-02 10:12:32 +09:00
Johannes Weiß b81ae9a4b0 fix zero capacity ByteBuffer reallocs (#44)
Motivation:

We'd go into an infinite loop when trying to increase the capacity of a
0 capacity ByteBuffer *doh*. Thanks @vlm for reporting and writing the
test.

Modifications:

made sure ensureAvailableCapacity returns at least 1 if we don't have
enough capacity available.

Result:

0 capacity ByteBuffers resize now.
2018-03-01 17:55:28 +09:00
Norman Maurer ee6cfcf9cf Add FileDescriptor protocol and implement it for FileHandle / BaseSocket (#38)
Motivation:

FileHandle and BaseSocket should implement a common protocol as these are both "based" on file descriptors

Modifications:

- Add FileDescriptor protocol
- Implement it for FileHandler and BaseSocket

Result:

Better code structure
2018-02-28 09:46:29 +00: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
Norman Maurer 580924909d Add Selectable.withUnsafeFileDescriptor and use it through the code. (#28)
Motivation:

We missed to check if the file descriptor is still open in ServerSocket before we act on it. We can remove a lot of complexity by just providing a withUnsafeFileDescriptor which will do this for us and use it through out the code-base.

Modifications:

Introduce withUnsafeFileDescriptor and use it to ensure we always only act on open Selectables.

Result:

More correct code.
2018-02-26 11:30:37 +00:00
Norman Maurer 0797b5c293 EmbeddedChannel should call fireChannelRegistered(...) when register(...) is called. (#34)
Motivation:

At the moment EmbeddedChannel calls fireChannelRegistered() when connect(...) is called. This is not correct, as it should be done when register(...) is called.

Modifications:

- Correctly call `fireChannelRegistered(...)` when register(...) is called
- Simplify init of EmbeddedChannel
- Fix EmbeddedChannelTest

Result:

Correct invocation of methods.
2018-02-26 10:57:07 +00:00
Norman Maurer 21065ed8f0 Revisit access levels and remove return (#35)
Motivation:

We sometimes used internal / public where fileprivate should have been used. Also we used a few return statements which were not needed.

Modifications:

Cleanup access levels and remove return.

Result:

Cleaner code
2018-02-26 09:58:22 +00:00
Norman Maurer 65d47a1ac1 Clarification in *Bootstrap docs. (#31)
Motivation:

Some of the docs for *Bootstrap could use some clarification. Also code examples were not formatted as such via markdown.

Modifications:

- Clarify docs
- Use code highlighting via markdown.

Result:

Better docs.
2018-02-26 09:51:03 +00:00
Norman Maurer b4456fbac4 Replace precondition with assert when checks for an implementation bug. (#30)
Motivation:

We used precondition for a few cases where it really checks for our own implementation. We should use assert here as there is nothing the user really can do to trigger it.

Modifications:

Replace precondition with assert.

Result:

Less overhead when in release mode and more correct in general.
2018-02-26 09:49:43 +00:00
Norman Maurer d41c528b68 Add docs for BaseSocket, Socket and ServerSocket. (#27)
Motivation:

Our *Socket classes did not have any docs.

Modification:

Add docs.

Result:

More documentation.
2018-02-23 17:04:53 +00:00
Norman Maurer c7eab7f0b9 We should assert that the BaseSocket was closed when deinit() is called. (#26)
Motivation:

Leaking a fd is bad we should at least assert that we not do.

Modifications:

Add assert in deinit so we crash in debug mode if we leak a socket.

Result:

Code is more robust.
2018-02-23 12:15:35 +00:00
Cory Benfield a779d2f084 Remove the promise-handling from the PWM. (#25)
Motivation:

We no longer have flush promises, so the PWM no longer needs to
handle them.

Modifications:

Removed all flush promise handling from the PWM and associated
tests.

Result:

Less code, more clarity.
2018-02-23 13:13:50 +01:00
Cory Benfield 369a0714ae Remove management of flush promises from PDWM. (#24)
Motivation:

A substantial chunk of code complexity exists in the PDWM to manage
flush promises. This complexity is no longer justified now that we
don't bother with them.

Modifications:

Removed much of the complexity of the PDWM handling of flush promises.

Result:

Simpler, easier to understand code.
2018-02-23 13:09:00 +01:00
Norman Maurer c95051db78 Make WriteBufferWaterMark be a struct and validate low and high watermarks. (#21)
Motivation:

We used Range for WriteBufferWaterMark which did not do any validation of the low and high marks.

Modifications:

Make WriteBufferWaterMark a struct and validate low and high values

Result:

More correct impl for watermarks.
2018-02-23 09:52:30 +00:00
Norman Maurer 02dae4a452 Add missing doc for the ChannelOutboundInvoker extension. (#20)
Motivation:

ChannelOutboundInvoker exntesion has no docs.

Modifications:

Add docs.

Result:

More docs FTW.
2018-02-22 19:55:31 +00:00
Cory Benfield 95c00129ee Correctly update remote peer address on delayed connect. (#18)
Motivation:

When the connect() call does not complete synchronously we were not
updating the peer IP address when it finally did complete. This meant
that any channel that didn't connect synchronously (in the real world,
almost all of them) would not correctly see the remote peer IP.

Modifications:

Update the cached addresses on delayed connect.

Result:

Connected channels will see the correct remote IP.
2018-02-22 17:47:56 +00:00
Norman Maurer eabbb2fa1c Add documentation for protocols and extensions in ChannelHandler (#7)
Motivation:

We should include docs for our core abstractions. This adds docs for ChannelHandler and sub-protocols.

Modifications:

Add docs.

Result:

More docs.
2018-02-22 18:02:28 +01:00
Norman Maurer ebae948897 Add docs for ChannelCore methods. (#17)
Motivation:

`ChannelCore` methods lack docs.

Modifications:

Add docs.

Result:

More docs == happier users
2018-02-22 16:38:10 +00:00
Norman Maurer b3e2362f40 Add docs for NIOAny methods. (#16)
Motivation:

NIOAny methods did not have any documentation.

Modifications:

- Add docs to methods.
- Fix typos.

Result:

More docs.
2018-02-22 15:31:16 +00:00
Cory Benfield 2a3396998d HTTPUpgradeHandler should accept Futures for upgraders. (#10)
Motivation:

Currently the HTTPUpgradeHandler does not allow for the protocol
upgraders to return EventLoopFuture objects. This leads to an
awkward scenario: it's essentially required that an upgrader be able
to synchronously change the pipeline to its satisfaction and prepare for
more bytes.

This is not really possible: ChannelPipeline.add(handler:) returns a
Future, which means that it is possible for this operation, at least
in principle, to not execute synchronously. This reality puts the
HTTPUpgradeHandler at odds with the reality of channel pipelines, and
cannot stand.

Modifications:

Give the HTTPProtocolUpgrader protocol to have upgrade() return an
EventLoopFuture<Void>, and update the HTTPServerUpgradeHandler to
appropriately handle the returned future by buffering until the future
completes.

Result:

It is no longer necessary to synchronously change the channel
pipeline during protocol upgrade.
2018-02-22 14:03:26 +01:00
Cory Benfield c8c8dbf2a2 Don't require casing to match in the HTTP upgrade header. (#1)
Motivation:

The HTTP upgrade handler should not require that we case-match with
the mandatory headers.

Modifications:

Treat all headers as lowercase.

Result:

Upgrade is easier.
2018-02-21 20:15:12 +01:00
Cory Benfield d2c5c7aad1 Add documentation for EventLoopFuture and EventLoopPromise
Motivation:

It's useful to be able to understand how these objects work.

Modifications:

Documented the public interface for both types.

Result:

There will be documentation
2018-02-21 13:15:49 +00:00
Johannes Weiss 5deffc630d replace var closed with var isOpen for more positive conditions
Motivation:

guard !self.open else { ... } is a double negation and can be confusing,
guard self.isOpen else { ... } is much better

Modifications:

replaced all `var closed: Bool` with `var isOpen: Bool`

Result:

we're more positive
2018-02-21 15:09:22 +00:00
Norman Maurer 8c2169663d Add docs for ByteToMessageDecoder and MessageToByteEncoder
Motivation:

We should provide docs for our provided codec extensions.

Modifications:

Add docs.

Result:

More clear for the user how to implement a custom codec.
2018-02-21 15:56:54 +01:00
Norman Maurer 2180d03690 Channel.getOption(...) should return EventLoopFuture and not block
Motivation:

Channel.getOption(...) should return an EventLoopFuture to ensure we not end up with a deadlock.

Modifications:

Change getOption(...) to return EventLoopFuture.

Result:

No deadlocks and more consistent API.
2018-02-21 11:55:15 +01:00
Johannes Weiß 2e5f62d653 make ByteBuffer.set/write work on Sequence rather than Collection
Motivation:

Previously we required the `ByteBuffer.set/write` methods to be a
`Collection` of `UInt8` but a `Sequence` is really enough.

Modifications:

Implemented `ByteBuffer.write/set` for `Sequence`s of `UInt8`

Result:

More stuff can be used with `ByteBuffer`
2018-02-20 16:37:39 +00:00
Johannes Weiss c7e47e75f3 remove flush promises
Motivation:

We have multiple reasons why flush promises weren't great, for example:

- writeAndFlush sounds like it's a write + flush optimisation but in
  reality it was more expensive (2 extra promises)
- the semantics were never quite clear
- lots of implementation complexity, especially for the datagram channel

Modifications:

- removed the flush promises in the API
- this deliberately doesn't do the PendingWritesManager simplifications
  that this unlocks

Result:

flush doesn't have a promise anymore.
2018-02-20 16:13:41 +00:00
Johannes Weiß c6fa249797 implement Max's suggestions
Motivation:

Max Moiseev had some good suggestions on how to clean up some APIs, this
implements those.

Modifications:

- remove `IOData.isFileRegion` (not necessary or used)
- offer an optional `as: T.Type` parameter for the integer reading methods
- remove one unnecessary method in `StaticString`'s `Collection` conformance

Result:

nicer
2018-02-20 15:51:35 +00:00
Johannes Weiß 64eac6177c make inbound channel operations non-throwing
Motivation:

Except for blocking methods (which shouldn't be used on the event loop),
there aren't that many error cases that we should just send through the
pipeline automatically. Best proof for that is that nothing in the code
base did that. Therefore these methods shouldn't be throwing.

If the user does indeed want to catch and fire an error through the
pipeline that's still super simple `ctx.fireErrorCaught(error)`.

Modifications:

removed `throws` from the `ChannelInboundHandler` methods.

Result:

better
2018-02-19 18:10:21 +00:00
Cory Benfield 58e147f682 ByteToMessageDecoder should reclaim memory.
Motivation:

ByteToMessageDecoder implementations can potentially have usage
patterns that never reclaim memory. This is problematic when used
for a long time, as they can be holding on to large chunks of memory
they don't need.

Modifications:

Gave ByteToMessageDecoder a new protocol method,
shouldReclaimBytes(buffer:), and a default implementation.

Result:

ByteToMessageDecoders will not be able to hold on to more than 2kB
of unreachable memory under any circumstances, and will often hold
less memory than that.
2018-02-20 14:12:20 +00:00
Johannes Weiss 68724be6e6 fix Channel thread-safety (local/remoteAddress)
Motivation:

There were a couple of places in the Channel implementations that just
weren't thread-safe at all, namely:

- localAddress
- remoetAddress
- parent

those are now fixed. I also re-ordered the code so it should be easier
to maintain in the future (the `// MARK` markers were totally
incorrect).

Modifications:

- made Channel.{local,remote}Address return a future
- made Channel.parent a `let`
- unified more of `SocketChannel` and `DatagramSocketChannel`
- fixed the `// MARK`s by reordering code
- annotated methods/properties that are in the Channel API and need to
be thread-safe

Result:

slightly more thread-safety :)
2018-02-20 15:18:51 +00:00
Norman Maurer 273932ae36 Change ByteToMessageDecoder.decode and decodeLast to return an enum.
Motivation:

We used a bool to signal if we should continue decoding or not. Using an enum is more self-documenting.

Modifications:

Add DecodingState and use.

Result:

Code is more self-documenting.
2018-02-20 13:07:31 +01:00
Cory Benfield 6b999ad368 Remove quadratic buffering from ByteToMessageDecoder
Motivation:

Don't make the system fall over under slowloris-style attacks.

Modifications:

Remove the quadratic copy loop from ByteToMessageDecoder.

Result:

NIO goes from 🚂 to 🚄
2018-02-20 08:02:43 +00:00
Norman Maurer 1912948fd4 Remove promise parameter on read method.
Motivation:

The promise on read is not very useful, we should just remove it.

Modifications:

Remove promise.

Result:

Cleaner code.
2018-02-19 21:39:46 +01: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