Commit Graph
66 Commits
Author SHA1 Message Date
Ayush Garg 7840b6b067 ChannelOption: Allow types to be accessed with leading dot syntax (#2816)
Motivation:

Since Swift 5.5 and [SE-0299](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0299-extend-generic-static-member-lookup.md) it is possible to add static members to protocols which are discoverable through the shorthand dot syntax.
This change reduces type repetition and improves call-site legibility.

Modifications:

Added extensions for ChannelOption with static members where Self is bound to a concrete type.

Result:

ChannelOption types can be used with the leading dot syntax. For eg:

```
//before
.channelOption(ChannelOptions.explicitCongestionNotification, value: true)
.channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)

//after
.channelOption(.explicitCongestionNotification, value: true)
.channelOption(.socketOption(.so_reuseaddr), value: 1)
```
2024-07-30 15:19:59 +00:00
Franz Busch c9756e1083 Adopt swift-format (#2794)
* Apply formatting

* Apply no block comments rule

* Apply OmitExplicitReturns

* Apple OnlyOneTrailingClosureArgument

* Apply NoAssignmentInExpressions

* Fix up DontRepeatTypeInStaticProperties lint errors

* Apply `OrderedImports`

* Apply `ReplaceForEachWithForLoop`

* format file

* Enable the formatting pipeline

* Adopt `AmbiguousTrailingClosureOverload`

* Fix license header

* Fix format check

* Fix `EndOfLineComment`

* Fix CI

* Adapt CI script to check if changes when running formatting

* Separate lint and format into to steps

* Fix format

* Adopt `UseEarlyExits`

* Revert "Adopt `UseEarlyExits`"

This reverts commit d1ac5bbe12.
2024-07-19 11:48:17 +02:00
Franz BuschandCory Benfield 63e8c4e1aa Migrate to syncOperations in more places (#2661)
# Motivation

Adding a handler via the regular `pipeline.addHandler` APIs can lead to transferring the handler over isolation regions. To avoid running into `Sendable` warnings here we can use the `syncOperations` of the pipeline to avoid transferring the handlers.

# Modification

This PR is mostly changing code to avoid transferring handlers to and from the event loops in tests. This should be a no-op mostly.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2024-03-01 15:05:22 +00:00
Si BeaumontandCory Benfield 858cb8927e Add bootstrap APIs for VSOCK sockets (#2479)
* Add bootstrap APIs for vsock sockets

Signed-off-by: Si Beaumont <beaumont@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2023-08-03 15:03:09 +01:00
Cory Benfield b05c6f2206 Move NIO to NIOPosix, make NIO a shell. (#1936)
Motivation:

The remaining NIO code really conceptually belongs in a module called
NIOPosix, and NIOCore should really be called NIO. We can't really do
that last step, but we can prepare by pushing the bulk of the remaining
code into a module called NIOPosix.

Modifications:

- Move NIO to NIOPosix
- Make NIO an umbrella module.

Result:

NIOPosix exists.
2021-08-16 16:50:40 +01:00
Cory BenfieldandGeorge Barnett 64285cbff2 Clean up dependencies and imports. (#1935)
Motivation:

As we've largely completed our move to split out our core abstractions,
we now have an opportunity to clean up our dependencies and imports. We
should arrange for everything to only import NIO if it actually needs
it, and to correctly express dependencies on NIOCore and NIOEmbedded
where they exist.

We aren't yet splitting out tests that only test functionality in
NIOCore, that will follow in a separate patch.

Modifications:

- Fixed up imports
- Made sure our protocols only require NIOCore.

Result:

Better expression of dependencies.

Co-authored-by: George Barnett <gbarnett@apple.com>
2021-08-12 13:49:46 +01:00
Cory Benfield 30265d69a8 Adjust names of BSDSocket option values. (#1510)
Motivation:

The names of most of the BSD socket option values were brought over into
their NIO helper properties, with their namespace removed. This vastly
increases the risk of collision, particularly for things like TCP_INFO,
which just became .info.

Modifications:

- Added the prefixes back, e.g. `.info` is now `.tcp_info`.
- Renamed socket type `.dgram` to `.datagram`, as this is the nice clean
  API and we should use nice clean names.

Result:

Better APIs
2020-05-11 15:13:58 +01:00
Saleem AbdulrasoolandCory Benfield bcc180dab6 Add new BSDSocket namespace (#1461)
* NIO: Add new `BSDSocket` namespace

This starts the split of the POSIX/Linux/Darwin/BSD interfaces and the
BSD Socket interfaces in order to support Windows.  The constants on
Windows are not part of the C standard library and need to be explicitly
prefixed.  Use the import by name to avoid the `#if` conditions on the
wrapped versions.  This will allow us to cleanly cleave the dependency
on the underlying C library.

This change adds a `BSDSocket.OptionLevel` and `BSDSocket.Option` types
which allow us to get proper enumerations for these values and pipe them
throughout the NIO codebase.

* Apply suggestions from code review

Co-Authored-By: Cory Benfield <lukasa@apple.com>
2020-04-02 19:06:48 +01:00
Johannes Weiss 7f20464df3 enable TCP_NODELAY by default (#1020)
Motivation:

Networking software like SwiftNIO that always has explicit flushes
usually does not benefit from having TCP_NODELAY switched off. The
benefits of having it turned on are usually quite substantial and yet we
forced our users for the longest time to enable it manually.

Quite a bit of engineering time has been lost finding performance
problems and it turns out switching TCP_NODELAY on solves them
magically.

Netty has made the switch to TCP_NODELAY on by default, SwiftNIO should
follow.

Modifications:

Enable TCP_NODELAY by default.

Result:

If the user forgot to enable TCP_NODELAY, their software should now be
faster.
2019-06-19 18:04:01 +01:00
Johannes Weiss a41280919e rename ctx to context (#842)
Motivation:

`ctx` was always an abbreviation was 'context` and in Swift we don't
really use abbreviations, so let's fix it.

Modifications:

- rename all instances of `ctx` to `context`

Result:

- fixes #483
2019-02-25 18:20:22 +00:00
Johannes Weiss 46ffd630de ChannelPipeline: addHandler and removeHandler instead of add/remove (#817)
Motivation:

- `ChannelPipeline.add(name:handler:...)` had a strange order of arguments
- `remove(handler:)` and `remove(ctx:)` both remove `ChannelHandler`s
  but they read like they remove different things

So let's just fix the argument order and name them `addHandler` and
`removeHandler` making clear what they do.

Modifications:

- rename all `ChannelPipeline.add(name:handler:...)`s to `ChannelPipeline.addHandler(_:name:...)`
- rename all `ChannelPipeline.remove(...)`s to `ChannelPipeline.removeHandler(...)`

Result:

more readable and consistent code
2019-02-21 11:46:54 +00:00
Trevör Anne Denise c6c137cde4 Remove useless comments (#806)
Now that flush() doesn't take a promise anymore, this comment isn't
needed.
2019-02-05 16:06:08 +00:00
Moritz Lang 5c2de043b3 Make use of dropFirst(_:) in example code (#794)
Motivation:

While getting into NIO through the examples I stumbled
accross some chained dropFirst() calls, which can be
simplified by using dropFirst(_:) instead.

Modifications:

I did a quick project-wide search of chained dropFirst()
usage and modified every line to use dropFirst(_:) instead.

Result:

This will only effect the readability of the code,
not its functionality.
2019-01-31 14:54:20 +00:00
Johannes Weiss 3e7d6a7bfd rename ELF.then to ELF.flatMap (#760)
Motivation:

ELF's API should be as close as possible to the new Result's API.
Therefore, we should rename `then` to `flatMap`

Modifications:

- renamed `then` to `flatMap`
- renamed `thenIfError` to `flatMapError`
- renamed ELF's generic parameter from `T` to `Value`

Result:

- more like Result
- fixes #688
2019-01-21 16:41:04 +00:00
Cotton Hou ee52d7694a Typo in EchoServer example (#583) 2018-08-18 18:44:32 +01:00
Norman Maurer 82a6e4d821 Rename numThreads to numberOfThreads parameter. (#443)
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.
2018-05-25 17:37:55 +02:00
Bas Broek afa1037410 Prefer passing functions to flatMap
Motivation:

Passing in a function directly makes the code a bit more concise.

Modifications:

Changing `flatMap { Int($0) }` to `flatMap(Int.init)`.

Result:

More concise code.
2018-03-19 14:55:53 +01:00
Cory Benfield 684ae3d933 Add READMEs for the various sample applications (#117) 2018-03-09 09:06:06 -08: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
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 Weiss 225951e7c3 remove Foundation dependency
Motivation:

Foundation is problematic for a few reasons:
- its implementation is different on Linux and on macOS which means our
  macOS tests might be inaccurate
- on macOS it uses ObjC Foundation which means the autorelease pool
  might get populated
- it links the world on Linux which means we can't do static
  binaries at all

Modifications:

removed the last bits of Foundation dependency

Result:

no Foundation dependency
2018-02-19 17:22:38 +00:00
Norman Maurer 3f4a755de1 Remove no needed return statements and cleanup examples.
Motivation:

We do not need to use return in the closures used in the examples.

Modifications:

- Remove return
- Remove bug where we set the same channel option two times in one example

Result:

Cleaner code.
2018-02-16 10:50:57 +01:00
Johannes Weiss eaa8ffaee4 remove redundant labels
Motivation:

Lots of our most important operations had redundant labels like

    func write(data: NIOAny)

the `data: ` label doesn't add anything meaningful and therefore it
should be removed.

Modifications:

removed lots of redundant labels

Result:

less redundant labels
2018-02-13 11:13:30 +00:00
Johannes Weiß 85b45e6912 make function names for connect/bind/SocketAddress more Swift-like 2018-02-12 15:09:22 +00:00
Norman Maurer aa9728b852 Use Int for port to make API more consistent
Motivation:

In ClientBootstrap we use Int to represent the port but in ServerBootstrap we use Int32. As swift usually always use Int we should just use Int everywhere to represent the port.

Modifications:

Change Int32 to Int for port

Result:

More consistent API
2018-02-12 13:39:01 +01:00
Norman Maurer 18a8467dfe Make parameter naming between connect / bind consistent.
Motivation:

We should have consistent parameter naming.

Modifications:

Use to path: everywhere.

Result:

Consistent naming.
2018-02-12 14:21:07 +01:00
Cory Benfield 3f357a3c79 Minor cleanups for Swift 4.1 2018-02-08 11:40:45 +00:00
Johannes Weiss 734904d0f4 clean up the (proto)types for EventLoopFuture
* clean up the (proto)types for EventLoopFuture

* Remove more unnecessary parens
2018-02-09 16:03:28 +00:00
Norman Maurer 46ac01f517 Use AdaptiveRecvBufferAllocator in the example as this is what users usually should do 2018-01-29 15:01:30 +01:00
Johannes Weiß c79c5ba364 re-export Darwin/Glibc and reduce number of #if os(...) 2018-01-09 15:08:17 +00:00
Norman Maurer 8f1ad37edd Use camel case for ChannelOption factory values and add docs 2017-12-20 14:18:07 +01:00
Johannes Weiss dfd25bf576 refactor Channel initialisation & *Bootstrap docs 2017-12-13 15:42:54 +00:00
Johannes Weiß eaf658691c some grammar fixes 2017-12-19 15:23:26 +00:00
Johannes Weiß 31cd14eaad fix inEventLoop race and makes TSan happy 2017-11-10 14:56:33 -08:00
Johannes Weiß 6173bee782 Split IOData into NIOAny & IOData (ByteBuffer | FileRegion) 2017-10-25 19:31:27 +01:00
Daniel Dunbar 87a47c88fc Prefer let bindings in port binding parsing. 2017-10-27 23:49:33 -07:00
Johannes Weiss 1adb581f59 remove more Foundation imports 2017-10-10 16:25:43 +01:00
Johannes Weiß 8e06697362 fix EventLoop shutdown by introducing shutdownGracefully 2017-09-20 17:15:06 +01:00
Cory Benfield 007feec636 Add Unix domain socket support part 2: Electric Boogaloo
* support UNIX Domain Sockets

* Add test for connect: with UDS path

* Add UDS-special connect method
2017-09-21 15:26:37 +01:00
Norman Maurer 1c469541a9 Force unwrap localAddress() as this should never be nil here and so fix the compiler warnings 2017-08-11 20:37:43 +02:00
Norman Maurer 16e0ffd9bf Allow to obtain remote and local address 2017-08-08 15:43:47 +02:00
Johannes Weiß 55bed8359b IPv6 support 2017-07-25 16:55:27 +01:00
Norman Maurer 7b2af6a03e Allow to pass the port as argument when starting the servers 2017-07-17 09:58:33 +02:00
Johannes Weiß c09178ceb2 baby steps towards a type-safe Channel pipeline 2017-06-30 13:04:19 +01:00
Norman Maurer 122360f812 Mark class ass private final 2017-07-01 14:10:36 +02:00
Norman Maurer 164cf244ac Swifty bind and connect method signature 2017-06-29 13:08:32 +02:00
Norman Maurer a9acf251e4 Have read and flush take an optional promise as well.
* Make Channel.read(...) take an optional promise so its more inline with the rest of the outbound operations

* Also let flush(...) take an optional promise and remove writeAndFlush(...)
2017-06-28 11:57:17 +02:00
Norman Maurer f35fb80c7e Make the promise optional for outbound operations.
This change allows the user to use `nil` as promise for outbound operations. This is useful if the user is not really interested in the outcome of the operations as it save extra allocations and so make things faster.

Before:
`Aggregate bandwidth: 10464.620↓, 10465.091↑ Mbps`

After (with using nil as Promise):

`Aggregate bandwidth: 11283.322↓, 11284.157↑ Mbps`
2017-06-27 14:37:24 +02:00
Tom Doron a6bae8b759 linux support
motivation: be able to build and test on linux

changes:
* add dockerfile to create docker image running all toold required for testing on linux
* add readme with instruction on how to use the docker setup
* add script to generate linux tests mapping
* add .swift_version file to explicitly call out the swift version needed to build the project
* adjust code that did not build on linux, mainly casting between Int32 and Int
2017-06-21 10:58:28 -07:00
Norman Maurer dbb98eb905 Split ChannelHandler to ChannelInboundHandler and ChannelOutboundHandler
The mention change was done for performance reasons as often the user only either implement inbound or outbound operations per handler. The problem before was that we had to traverse the full list all the time even if the user was not really interested in either inbound or outbound events.

With this change we also split the doubly-linked-list into two linked-lists, one for inbound handlers and one for outbound handlers. This way we only need to traverse the handlers that are really interested in th event itself.

To give an idea about the impact of the change in terms of performance, here are some numbers.

Two ChannelInboundHandlers in the ChannelPipeline that do nothing and so just forward the events to the next handler in the pipeline:

Before:

`Bandwidth per channel: 18369.796⇅ Mbps (2296224.5 kBps)`

After:

`Bandwidth per channel: 20043.638⇅ Mbps (2505454.7 kBps)`
2017-06-21 14:35:33 +02:00