Skip to content

Add socket activation support for TCP and UDS servers#2734

Open
AndreeaDrehuta wants to merge 1 commit into
grpc:masterfrom
AndreeaDrehuta:adrehuta/grpc-socket-activation
Open

Add socket activation support for TCP and UDS servers#2734
AndreeaDrehuta wants to merge 1 commit into
grpc:masterfrom
AndreeaDrehuta:adrehuta/grpc-socket-activation

Conversation

@AndreeaDrehuta

Copy link
Copy Markdown

Adds an opt-in "socket-activation" feature that lets tonic servers accept pre-opened listeners passed by systemd for both TCP and Unix domain sockets. Includes examples, systemd unit files and unit tests.

Fixes #2733

Motivation

Under systemd, a service can be started on-demand when a client connects. The manager creates and listens on the socket itself, then passes the already-listening file descriptor to the spawned process via the LISTEN_FDS / LISTEN_PID environment variables.
Consider a gRPC server that receives requests only occasionally. Keeping it running around the clock just to answer the rare request wastes memory and other resources. A better fit for that situation is to let the process run only while it is actually serving requests: the service manager (systemd) holds the socket, starts the server on an incoming connection, and the server can stop itself once it goes idle, after which the manager re-activates it on the next connection.

Solution

Introduces a new, opt-in socket-activation cargo feature (Unix only, disabled by default, so there is no impact on existing users).

When the feature is enabled, socket binding checks for an inherited listener before opening a new one:

TCP: TcpIncoming::bind(addr) adopts an inherited listening socket whose address matches addr, if none was passed in, it falls back to binding normally.
UDS: the Unix domain socket path binding adopts a matching inherited listener when present, otherwise binds as usual.

Implementation details:

A helper (socket_activation.rs) reads LISTEN_PID / LISTEN_FDS, verifies the fds were intended for this process, and returns the first inherited fd that is a listening stream socket matching the requested address.
Behavior is fully backward compatible: with the feature off, or when no fds are passed in, binding works exactly as before.

Also included to make the feature usable and testable:

Example TCP and UDS client/server binaries under examples/src/socket_activation/.
Ready-to-use systemd .socket / .service unit files.
Shell test scripts (test_tcp.sh, test_uds.sh) that exercise the activation flow end to end.
Unit tests covering fd scanning and listening-socket detection.

Adds an opt-in "socket-activation" feature that lets tonic servers accept
pre-opened listeners passed by a supervisor (e.g. systemd) for both TCP
and Unix domain sockets. Includes examples, systemd unit files, test
scripts, and unit tests.
@linux-foundation-easycla

Copy link
Copy Markdown

CLA Missing ID

  • ❌ The email address for the commit (8468066) is not linked to the GitHub account, preventing the EasyCLA check. Consult this Help Article and GitHub Help to resolve. (To view the commit's email address, add .patch at the end of this PR page's URL.) For further assistance with EasyCLA, please visit our EasyCLA portal and chat with our support bot.

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.

Add socket-activation support for grpc.

1 participant