-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat: support systemd socket activation #10977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support systemd socket activation #10977
Conversation
🦋 Changeset detectedLatest commit: 7ad9733 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
02e6464
to
0154801
Compare
Thank you. This PR is rather cryptic to me — things like It really seems like this sort of thing belongs in a custom server rather than in the turnkey server you get OOTB. Is there any reason that wouldn't work? |
I've been playing around with this myself and wanted to create a PR for this too. You could indeed do this with a custom server but the changes required to do this ootb are very minimal and other adapters try to take advantage of the platform being deployed to as much as possible, so why not |
I'm open to the idea that this PR is necessary given that systemd is used on most machines the |
I can help with that. @elohmeier in your code it looks like you don't throw an error when more than one |
const sd_listen_fds_start = 3; | ||
|
||
if (listen_pid && listen_fds && process.pid === parseInt(listen_pid)) { | ||
const fd = sd_listen_fds_start + parseInt(listen_fds) - 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const sd_listen_fds_start = 3; | |
if (listen_pid && listen_fds && process.pid === parseInt(listen_pid)) { | |
const fd = sd_listen_fds_start + parseInt(listen_fds) - 1; | |
// systemd file descriptor start index - https://0pointer.de/blog/projects/socket-activation.html | |
const SD_LISTEN_FDS_START = 3; | |
if (listen_pid && listen_fds && process.pid === parseInt(listen_pid)) { | |
const fd = SD_LISTEN_FDS_START + parseInt(listen_fds) - 1; |
I just realized that the code to actually shut down the server is not included so this PR as it is wouldn't fix the original issue. Maybe we could add a |
It's 3 because each process starts with three FDs 0 = stdin then it's 3 for the socket because it's the next available. BTW the whole awesomeness around this socket activation system is that systemd is the one that binds the socket. While there is no access to it the application doesn't run. When something connects the socket systemd wakes up the server then handover the control of the socket. There are two ways of doing this. One way is systemd calls BTW today I did a on demand system that wakes up a inference service to run a STT model. I don't want it everytime eating my VRAM so now I have this inference service running on demand and auto sleeping after one minute of inactivity and a Telegram bot that transcribes audio messages. When binding the port, instead of creating a new socket the process basically only wraps the socket that is already opened on fd 3. NGL this stuff is kinda boring to test as one must create a service unit and a socket unit that links to the service unit. [1] https://github.com/lucasew/nixcfg/blob/master/nix/nodes/riverwood/test_socket_activated/service.py |
I basically applied the changes of this PR using You guys are free to take this patch and apply on your own stuff. Just follow the setup instructions [2] and put this patch in the patches folder. The patch will be applied automatically when doing a [1] https://github.com/lucasew/cf-torrent/blob/dc1482d04507bbe585a6b0a2d774adfc4e5ed68a/patches/%40sveltejs%2Badapter-node%2B1.2.3.patch |
You can install this (or any) branch into a SvelteKit project directly from GitHub using https://gitpkg.vercel.app/ But as you said, it doesn't handle shutting down the server so I wouldn't say it's solved just yet. I would also only accept one file descriptor like most apps unless anyone can think of a use case where someone would want to pass multiple |
I found how to do this. I updated that patch to support it. That link will not show the latest version because it's a permalink. Everything is opt-in. The socket activation logic will be activated only when it detects the environment variables that systemd sets and the idle shutdown will only happens if BTW the cf-torrent unit is taking about 17MB of RAM (from |
I've just created an alternative PR #11653 with documentation and instructions on how to try it out yourself. |
@karimfromjordan cool, thanks for picking it up! Much appreciated 👍 |
Adds support for systemd socket activation for the node adapter. Fixes #10134.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.