Skip to content

Commit

Permalink
Feat: add special handling for /dev/fd address
Browse files Browse the repository at this point in the history
to support socket activation
  • Loading branch information
ruihe774 committed Aug 29, 2024
1 parent b7d02ad commit 9348ce2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions common/net/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
DialUDP = net.DialUDP
DialUnix = net.DialUnix
FileConn = net.FileConn
FileListener = net.FileListener
Listen = net.Listen
ListenTCP = net.ListenTCP
ListenUDP = net.ListenUDP
Expand Down
25 changes: 21 additions & 4 deletions transport/internet/system_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func getControlFunc(ctx context.Context, sockopt *SocketConfig, controllers []co
func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.Listener, error) {
var lc net.ListenConfig
var network, address string
var l net.Listener
var err error
// callback is called after the Listen function returns
// this is used to wrap the listener and do some post processing
callback := func(l net.Listener, err error) (net.Listener, error) {
Expand Down Expand Up @@ -93,6 +95,16 @@ func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *S
copy(fullAddr, address[1:])
address = string(fullAddr)
}
} else if strings.HasPrefix(address, "/dev/fd/") {
fd, err := strconv.Atoi(address[8:])
if err != nil {
return nil, err
}
_ = syscall.SetNonblock(fd, true)

Check failure on line 103 in transport/internet/system_listener.go

View workflow job for this annotation

GitHub Actions / build (windows, 386)

cannot use fd (variable of type int) as syscall.Handle value in argument to syscall.SetNonblock

Check failure on line 103 in transport/internet/system_listener.go

View workflow job for this annotation

GitHub Actions / build (windows, amd64)

cannot use fd (variable of type int) as syscall.Handle value in argument to syscall.SetNonblock

Check failure on line 103 in transport/internet/system_listener.go

View workflow job for this annotation

GitHub Actions / build (windows, arm, 7)

cannot use fd (variable of type int) as syscall.Handle value in argument to syscall.SetNonblock

Check failure on line 103 in transport/internet/system_listener.go

View workflow job for this annotation

GitHub Actions / build (windows, arm64)

cannot use fd (variable of type int) as syscall.Handle value in argument to syscall.SetNonblock

Check failure on line 103 in transport/internet/system_listener.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

cannot use fd (variable of type int) as syscall.Handle value in argument to syscall.SetNonblock
l, err = net.FileListener(os.NewFile(uintptr(fd), address))
if err != nil {
return nil, err
}
} else {
// normal unix domain socket
var fileMode *os.FileMode
Expand Down Expand Up @@ -133,13 +145,18 @@ func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *S
}
}

l, err := lc.Listen(ctx, network, address)
l, err = callback(l, err)
if err == nil && sockopt != nil && sockopt.AcceptProxyProtocol {
if l == nil {
l, err = lc.Listen(ctx, network, address)
l, err = callback(l, err)
if err != nil {
return nil, err
}
}
if sockopt != nil && sockopt.AcceptProxyProtocol {
policyFunc := func(upstream net.Addr) (proxyproto.Policy, error) { return proxyproto.REQUIRE, nil }
l = &proxyproto.Listener{Listener: l, Policy: policyFunc}
}
return l, err
return l, nil
}

func (dl *DefaultListener) ListenPacket(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.PacketConn, error) {
Expand Down

0 comments on commit 9348ce2

Please sign in to comment.