Skip to content

Add unix domain socket support for bind targets#96

Draft
mob-connection wants to merge 1 commit into
swift-server:mainfrom
mob-connection:uds-bind-target
Draft

Add unix domain socket support for bind targets#96
mob-connection wants to merge 1 commit into
swift-server:mainfrom
mob-connection:uds-bind-target

Conversation

@mob-connection

Copy link
Copy Markdown

Adds a .unixDomainSocket(path:) bind target so the server can listen on a UNIX domain socket in addition to host and port.

  • Add BindTarget.unixDomainSocket(path:) and a matching SocketAddress case
  • Bind via ServerBootstrap.bind(unixDomainSocketPath:) for both plaintext and secure-upgrade channels, and report the bound path from listeningAddresses
  • Remove the socket file on shutdown; fail the bind if the path is already occupied so a stale socket is never silently reused
  • Support a socketPath key in swift-configuration, mutually exclusive with host/port

Resolves #69

Adds a `.unixDomainSocket(path:)` bind target so the server can listen on a
UNIX domain socket in addition to host and port.

- Add `BindTarget.unixDomainSocket(path:)` and a matching `SocketAddress` case
- Bind via `ServerBootstrap.bind(unixDomainSocketPath:)` for both plaintext and
  secure-upgrade channels, and report the bound path from `listeningAddresses`
- Remove the socket file on shutdown; fail the bind if the path is already
  occupied so a stale socket is never silently reused
- Support a `socketPath` key in swift-configuration, mutually exclusive with
  `host`/`port`

Resolves swift-server#69
@mob-connection

Copy link
Copy Markdown
Author

For #69 I used a struct-backed NIOHTTPServer.SocketAddress with an added
.unixDomainSocket(path:) case, so it stays extensible (e.g. vsock later)
without a source-breaking change.

Do you think it makes sense to expose it as a plain enum instead ?

/// ```swift
/// let target = BindTarget.unixDomainSocket(path: "/tmp/server.sock")
/// ```
public static func unixDomainSocket(path: String) -> Self {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably use the new FilePath type once it becomes available

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably use the new FilePath type once it becomes available

In the UDS bind support the socket path has two different types depending on direction: on input we take a FilePath (BindTarget.unixDomainSocket(path:)), but on output we expose it as String (SocketAddress.unixDomainSocketPath, mirroring NIOCore.SocketAddress.pathname). If we commit to FilePath, I'd lean toward using it on output too for consistency — but keeping String also makes sense since that's just what NIO reports back. Which way should we go?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support binding to a unix socket

2 participants