Skip to content

feat: add Unix domain socket (UDS) listener support#2494

Open
SBALAVIGNESH123 wants to merge 1 commit intodrogonframework:masterfrom
SBALAVIGNESH123:feature/unix-domain-sockets
Open

feat: add Unix domain socket (UDS) listener support#2494
SBALAVIGNESH123 wants to merge 1 commit intodrogonframework:masterfrom
SBALAVIGNESH123:feature/unix-domain-sockets

Conversation

@SBALAVIGNESH123
Copy link
Copy Markdown
Contributor

Closes #1153

This PR adds native Unix domain socket support to Drogon. Unix domain sockets provide significantly lower latency than TCP for same-host communication, making them ideal for reverse proxy setups like Nginx → Drogon. This has been a requested feature since issue #1153 was opened back in January 2022.

The implementation spans both Drogon and Trantor. On the Trantor side, I extended InetAddress with a sockaddr_un union member and added a new constructor for Unix domain socket paths, along with helper methods like isUnixDomain() and toUnixPath(). The Socket class was updated to use protocol = 0 instead of IPPROTO_TCP when creating AF_UNIX sockets, and I made bind() UDS-aware — it removes any stale socket file before binding and sets chmod 0660 permissions afterward. The accept() path now uses sockaddr_storage (110 bytes) instead of sockaddr_in6 (28 bytes) to accommodate the larger sockaddr_un structure, and TCP_NODELAY is correctly skipped for UDS connections since it only applies to TCP.

On the Drogon side, I added a new addListener(const std::string &unixSocketPath) overload to HttpAppFramework that takes just a socket path. The ListenerManager creates and manages UDS listeners alongside existing TCP listeners, and automatically cleans up socket files via unlink() when the server stops. JSON configuration is also supported through a "unix_socket" key in the listeners array.

Usage is straightforward — either programmatic:

app().addListener("/var/run/drogon.sock");

@SBALAVIGNESH123 SBALAVIGNESH123 force-pushed the feature/unix-domain-sockets branch 4 times, most recently from 3ff642c to df7665c Compare April 13, 2026 16:22
Add support for listening on Unix domain sockets, enabling lower-latency
same-host communication for reverse proxy setups (e.g., Nginx -> Drogon).

Server API:
  app().addListener(/var/run/drogon.sock);

Config file:
  {listeners: [{unix_socket: /var/run/drogon.sock}]}

Changes:
- Add HttpAppFramework::addListener(unixSocketPath) overload (POSIX only)
- Add ListenerManager::addUnixListener() with UDS HttpServer creation
- Add ConfigLoader support for 'unix_socket' field in listener config
- Socket file is automatically cleaned up on server stop
- Includes Trantor changes: InetAddress AF_UNIX support, Socket fixes

Addresses drogonframework#1153
@SBALAVIGNESH123 SBALAVIGNESH123 force-pushed the feature/unix-domain-sockets branch from df7665c to b33408b Compare April 13, 2026 16:40
@an-tao
Copy link
Copy Markdown
Member

an-tao commented Apr 14, 2026

@SBALAVIGNESH123 please make PR to trantor first.

@SBALAVIGNESH123
Copy link
Copy Markdown
Contributor Author

Thanks @an-tao! I've submitted the Trantor PR here: an-tao/trantor#399

Once that's merged, I'll update the submodule pointer in this PR to reference the official Trantor commit.

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 for unix domain sockets?

2 participants