Skip to content

feat: add ListenerControl callback to Config#144

Open
krsna1729 wants to merge 2 commits into
datarhei:mainfrom
krsna1729:feat/listener-control
Open

feat: add ListenerControl callback to Config#144
krsna1729 wants to merge 2 commits into
datarhei:mainfrom
krsna1729:feat/listener-control

Conversation

@krsna1729

Copy link
Copy Markdown

Summary

Go's net.ListenConfig exposes a Control callback so callers can set arbitrary socket options on the raw fd before bind. gosrt hardcodes its own ListenControl() inside Listen() but provides no way for the caller to hook into the same mechanism.

This PR adds a ListenerControl field to Config, following the same signature as net.ListenConfig.Control. If set, it runs after gosrt's built-in socket options (SO_REUSEADDR, IP_TOS, IP_TTL) but before the socket is bound.

This lets callers like mediamtx set SO_RCVBUF (or any other socket option) without gosrt needing to know about application-specific details:

conf.ListenerControl = func(network, address string, c syscall.RawConn) error {
    return c.Control(func(fd uintptr) {
        syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_RCVBUF, 26214400)
    })
}

Changes

  • config.go: Add ListenerControl func(network, address string, c syscall.RawConn) error field to Config
  • listen.go: Wrap the net.ListenConfig.Control callback to chain gosrt's built-in ListenControl with the caller's ListenerControl (if set)

Test plan

  • All existing tests pass (go test ./...)
  • Builds cleanly against latest master
  • Zero behavioral change when ListenerControl is nil (default)

Go's net.ListenConfig exposes a Control callback so callers can set
arbitrary socket options on the raw fd before bind. gosrt hardcodes
its own ListenControl() inside Listen() but provides no way for the
caller to hook into the same mechanism.

Add a ListenerControl field to Config, following the same signature as
net.ListenConfig.Control. If set, it runs after gosrt's built-in socket
options (SO_REUSEADDR, IP_TOS, IP_TTL) but before the socket is bound.

This lets callers set SO_RCVBUF (or any other socket option) without
gosrt needing to know about application-specific details:

    conf.ListenerControl = func(network, address string, c syscall.RawConn) error {
        return c.Control(func(fd uintptr) {
            syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_RCVBUF, 26214400)
        })
    }
Verify that the ListenerControl callback is invoked during Listen().
@krsna1729

Copy link
Copy Markdown
Author

cc @ioppermann would be nice to have? I plan to send the counterpart app patch to mediamtx once this is merged.

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.

1 participant