mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
### 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)
```