Add socket option API for SO_REUSEPORT (#3741)

### Motivation:

`NIOBSDSocket.Option` provides shorthand API for socket options, e.g.
`.so_reuseaddr` for `SO_REUSEADDR`. We don't have API for
`SO_REUSEPORT`.

`SO_REUSEPORT` is used to bind more than one socket to the same address
and port to spread accepts or datagrams over multiple sockets and
support hot restarts.

Users can already use it today, but must use `init(rawValue:)`, and
import the platform C lib, etc:

```swift
.channelOption(.socketOption(.init(rawValue: SO_REUSEPORT)), value: 1)
```

There's no reason not to provide a convenience for this option, like we
do for many others.

### Modifications:

This PR adds `NIOBSDSocket.Option.so_reuseport`, wrapping
`SO_REUSEPORT`, alongside the other options in `BSDSocketAPI.swift`.

It's not provided on Windows or WASI, which don't have `SO_REUSEPORT`,
but there was already precedent for other options like this, e.g.
`SO_TIMESTAMP`.

### Result:

Users can pass `.socketOption(.so_reuseport)` like other supported
socket options:

```swift
DatagramBootstrap(group: group)
    .channelOption(.socketOption(.so_reuseport), value: 1)
    .bind(host: "127.0.0.1", port: 12345)
```
This commit is contained in:
Si Beaumont authored and GitHub committed 2026-09-14 15:19:06 +00:00
1 parent a8a8f22d99
commit 03f13fb86c
2 files changed
+39

No files matched your search

+12
View File
@@ -567,6 +567,18 @@ extension NIOBSDSocket.Option {
public static var so_timestamp: NIOBSDSocket.Option {
NIOBSDSocket.Option(rawValue: SO_TIMESTAMP)
}
/// Allows multiple sockets to be bound to an identical address and port.
///
/// - Important: The behavior of this socket option is platform-specific. See platform documentation for details.
///
/// - Seealso: `man 2 socket` (Linux)
/// - Seealso: `man 2 setsockopt` (Darwin)
/// - Seealso: https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ
@inlinable
public static var so_reuseport: NIOBSDSocket.Option {
NIOBSDSocket.Option(rawValue: SO_REUSEPORT)
}
}
#endif
@@ -634,6 +634,33 @@ class DatagramChannelTests: XCTestCase {
#endif
}
func testSettingSoReuseportAllowsTwoChannelsToBindTheSamePort() throws {
#if os(Windows)
throw XCTSkip("SO_REUSEPORT is not available on Windows")
#else
let first = try assertNoThrowWithValue(
DatagramBootstrap(group: group)
.channelOption(.socketOption(.so_reuseport), value: 1)
.bind(host: "127.0.0.1", port: 0)
.wait()
)
defer {
XCTAssertNoThrow(try first.close().wait())
}
let second = try assertNoThrowWithValue(
DatagramBootstrap(group: group)
.channelOption(.socketOption(.so_reuseport), value: 1)
.bind(to: first.localAddress!)
.wait()
)
defer {
XCTAssertNoThrow(try second.close().wait())
}
XCTAssertTrue(try getBoolSocketOption(channel: first, level: .socket, name: .so_reuseport))
XCTAssertTrue(try getBoolSocketOption(channel: second, level: .socket, name: .so_reuseport))
#endif
}
func testUnprocessedOutboundUserEventFailsOnDatagramChannel() throws {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {