Skip to content

Conversation

@MayCXC
Copy link

@MayCXC MayCXC commented Oct 9, 2024

I'd like to upstream the function here https://github.com/MayCXC/caddy-systemd-socket-activation/blob/master/networks.go#L25

which is like the *WithNames funcs here

func ListenersWithNames() (map[string][]net.Listener, error) {
, but with the benefit that it works for any network (tcp/udp/unix/unixgram/etc.) that a socket might be bound to, and lets the caller choose how to make a FileListener or FilePacketConn from them.

@prestist
Copy link

Thank you for contributing! Would you mind explaining your usecase ?

@MayCXC
Copy link
Author

MayCXC commented Oct 18, 2024

sure, my program uses socket units with both Listen and ListenDatagram settings, some of which may be used to create TLS listeners, and some of which may not. The existing *WithNames functions assume that all the sockets use the same protocol, and that either all or none will be used to create TLS listeners. FilesWithNames() provides the same LISTEN_FDS/LISTEN_FDNAMES mapping to files that the appropriate listeners can be created with, but without making these assumptions.

Copy link
Collaborator

@kolyshkin kolyshkin left a comment

Choose a reason for hiding this comment

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

From what I see, this is just a wrapper on top of Files which converts the returned *os.File slice into a map, where the key is file name and the value is a slice of *os.File corresponding to this name.

Such functionality, if needed, can easily be recreated in software that uses this package and activation.Files, in just a few lines of code. Unless such usage is a very common case, I can hardly see why we should put this method into this repo.

@MayCXC
Copy link
Author

MayCXC commented Aug 1, 2025

From what I see, this is just a wrapper on top of Files which converts the returned *os.File slice into a map, where the key is file name and the value is a slice of *os.File corresponding to this name.

Such functionality, if needed, can easily be recreated in software that uses this package and activation.Files, in just a few lines of code. Unless such usage is a very common case, I can hardly see why we should put this method into this repo.

right, these are those few lines of code. there are already funcs in activation that create TCP listeners with TLS configurations for each stream fd and map names to them, which is a much more special case than what FilesWithNames handles. for example, http3 web servers that use socket activation will receive file descriptors for two TCP sockets and one UDP socket, the latter of which will have its name ignored as there is no PacketConnsWithNames func in activation. so in general a map of fd names to files is a more useful construct for working with socket activation.

@kolyshkin
Copy link
Collaborator

@MayCXC can you please squash your commits, and add a activation: prefix to the commit subject?

@MayCXC MayCXC force-pushed the patch-1 branch 4 times, most recently from 0bbfee7 to a12d14c Compare January 8, 2026 05:07
@MayCXC
Copy link
Author

MayCXC commented Jan 8, 2026

@kolyshkin no problem. i also moved the func out of the goos targets.

Copy link
Collaborator

@kolyshkin kolyshkin left a comment

Choose a reason for hiding this comment

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

lgtm

Comment on lines 25 to 32
current, ok := filesWithNames[f.Name()]

if !ok {
current = []*os.File{}
filesWithNames[f.Name()] = current
}

filesWithNames[f.Name()] = append(current, f)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that could be made much simpler with just doing

filesWithNames[f.Name()] = append(filesWithNames[f.Name()], f)

The map lookup returns the zero value nil which is totally fine to append to and will create a new slice and otherwise returns the existing one and appends there so this one line should be enough

Copy link
Author

Choose a reason for hiding this comment

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

seems so, that was copied from the other funcs:

current, ok := listeners[f.Name()]

Copy link
Collaborator

Choose a reason for hiding this comment

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

Opened #498

@MayCXC MayCXC requested a review from Luap99 January 9, 2026 18:01
@MayCXC MayCXC closed this Jan 11, 2026
@MayCXC MayCXC deleted the patch-1 branch January 11, 2026 09:08
kolyshkin added a commit to kolyshkin/go-systemd that referenced this pull request Jan 14, 2026
As map lookup returns nil if no key is found, and append will create a
new slice if it's first argument is nil, we can greatly simplify the
loop, as suggested in [1].

[1]: coreos#448 (comment)

Reported-by: Paul Holzinger <[email protected]>
Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin
Copy link
Collaborator

FYI, this PR is being carried in #497

Luap99 pushed a commit to kolyshkin/go-systemd that referenced this pull request Jan 20, 2026
As map lookup returns nil if no key is found, and append will create a
new slice if it's first argument is nil, we can greatly simplify the
loop, as suggested in [1].

[1]: coreos#448 (comment)

Reported-by: Paul Holzinger <[email protected]>
Signed-off-by: Kir Kolyshkin <[email protected]>
Luap99 pushed a commit that referenced this pull request Jan 20, 2026
As map lookup returns nil if no key is found, and append will create a
new slice if it's first argument is nil, we can greatly simplify the
loop, as suggested in [1].

[1]: #448 (comment)

Reported-by: Paul Holzinger <[email protected]>
Signed-off-by: Kir Kolyshkin <[email protected]>
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.

4 participants