Skip to content

systemd socket activation support #10072

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

Merged
merged 2 commits into from
Nov 8, 2023
Merged

systemd socket activation support #10072

merged 2 commits into from
Nov 8, 2023

Conversation

yawkat
Copy link
Member

@yawkat yawkat commented Nov 6, 2023

This PR adds support for binding to a systemd socket. It's a fairly minor internal change, but it could be useful.

This PR adds support for binding to a systemd socket. It's a fairly minor internal change, but it could be useful.
@yawkat yawkat added the type: improvement A minor improvement to an existing feature label Nov 6, 2023
prefer-native-transport: true
listeners:
systemd:
fd: 3 # systemd passes the server socket as fd 3
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this always 3?

Or is this dependant on when systemd sets up the socket via the below config?

Are you on a Mac? If so, how did you test this? ;-)

Copy link
Member Author

Choose a reason for hiding this comment

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

it's always 3. and no, i'm on linux.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

can we not default the value to 3 if it is always 3?

Copy link
Member Author

Choose a reason for hiding this comment

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

i think it could be 0 if you were using inetd for example. the 3 is systemd-specific, but the fd config option is more generic.

Copy link
Member Author

Choose a reason for hiding this comment

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

ive thought about detecting it from the env variables that systemd sets. but it would be a bit more complicated of a PR, there's no way to test it, it might have negative perf impact, and it is pretty niche in the first place.

this pr is basically the minimum to make it work with systemd with config options only, with no adverse impact since it's a niche feature.

Copy link
Contributor

Choose a reason for hiding this comment

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

https://github.com/systemd/systemd/blob/main/src/systemd/sd-daemon.h#L56

If I'm reading that right, it starts at 3, but if you have multiple services, they'll be on 3 + n?

Copy link
Member Author

Choose a reason for hiding this comment

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

right. i dont really see a use case for micronaut there though, so i didnt add it to the docs.

Copy link
Contributor

@timyates timyates left a comment

Choose a reason for hiding this comment

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

👍

I can't test it though, as I don't have Epoll :-(

…/configuration/NettyHttpServerConfiguration.java

Co-authored-by: Tim Yates <tim.yates@gmail.com>
Copy link

sonarqubecloud bot commented Nov 7, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

57.4% 57.4% Coverage
5.1% 5.1% Duplication

idea Catch issues before they fail your Quality Gate with our IDE extension sonarlint SonarLint

@yawkat yawkat merged commit 3ef7021 into 4.3.x Nov 8, 2023
@yawkat yawkat deleted the socket-activation branch November 8, 2023 15:14
yawkat added a commit that referenced this pull request Feb 27, 2024
In #10072, I implemented support for inheriting a ServerSocketChannel from a fixed file descriptor to support systemd socket activation. This PR extends that in two ways:

- Allow specifying an "accepted FD". That means an established *connection* (SocketChannel), not a listening socket (ServerSocketChannel). This allows for inetd-style socket activation which starts a process for each connection.
- Add support for nio when fd=0 or accepted-fd=0, using System.inheritedChannel().
@yawkat yawkat mentioned this pull request Feb 27, 2024
yawkat added a commit that referenced this pull request Feb 27, 2024
* Accept inetd-style FDs
In #10072, I implemented support for inheriting a ServerSocketChannel from a fixed file descriptor to support systemd socket activation. This PR extends that in two ways:

- Allow specifying an "accepted FD". That means an established *connection* (SocketChannel), not a listening socket (ServerSocketChannel). This allows for inetd-style socket activation which starts a process for each connection.
- Add support for nio when fd=0 or accepted-fd=0, using System.inheritedChannel().

* add since

* sonar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: improvement A minor improvement to an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants