forked from traefik/traefik
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SO_REUSEPORT support for EntryPoints
- Loading branch information
Showing
19 changed files
with
279 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//go:build !(linux || freebsd || openbsd || darwin) | ||
|
||
package server | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/traefik/traefik/v3/pkg/config/static" | ||
) | ||
|
||
// newListenConfig creates a new net.ListenConfig for the given configuration of | ||
// the entry point. | ||
func newListenConfig(configuration *static.EntryPoint) (lc net.ListenConfig) { | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//go:build !(linux || freebsd || openbsd || darwin) | ||
|
||
package server | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/traefik/traefik/v3/pkg/config/static" | ||
) | ||
|
||
func TestNewListenConfig(t *testing.T) { | ||
ep := static.EntryPoint{Address: ":0"} | ||
listenConfig := newListenConfig(&ep) | ||
require.Nil(t, listenConfig.Control) | ||
require.Zero(t, listenConfig.KeepAlive) | ||
|
||
l1, err := listenConfig.Listen(context.Background(), "tcp", ep.Address) | ||
require.NoError(t, err) | ||
require.NotNil(t, l1) | ||
defer l1.Close() | ||
|
||
l2, err := listenConfig.Listen(context.Background(), "tcp", l1.Addr().String()) | ||
require.Error(t, err) | ||
require.ErrorContains(t, err, "address already in use") | ||
require.Nil(t, l2) | ||
|
||
ep = static.EntryPoint{Address: ":0", ReusePort: true} | ||
listenConfig = newListenConfig(&ep) | ||
require.Nil(t, listenConfig.Control) | ||
require.Zero(t, listenConfig.KeepAlive) | ||
|
||
l3, err := listenConfig.Listen(context.Background(), "tcp", ep.Address) | ||
require.NoError(t, err) | ||
require.NotNil(t, l3) | ||
defer l3.Close() | ||
|
||
l4, err := listenConfig.Listen(context.Background(), "tcp", l3.Addr().String()) | ||
require.Error(t, err) | ||
require.ErrorContains(t, err, "address already in use") | ||
require.Nil(t, l4) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//go:build linux || freebsd || openbsd || darwin | ||
|
||
package server | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"syscall" | ||
|
||
"github.com/traefik/traefik/v3/pkg/config/static" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// newListenConfig creates a new net.ListenConfig for the given configuration of | ||
// the entry point. | ||
func newListenConfig(configuration *static.EntryPoint) (lc net.ListenConfig) { | ||
if configuration != nil && configuration.ReusePort { | ||
lc.Control = controlReusePort | ||
} | ||
return | ||
} | ||
|
||
// controlReusePort is a net.ListenConfig.Control function that enables SO_REUSEPORT | ||
// on the socket. | ||
func controlReusePort(network, address string, c syscall.RawConn) error { | ||
var setSockOptErr error | ||
err := c.Control(func(fd uintptr) { | ||
// Note that net.ListenConfig enables unix.SO_REUSEADDR by default, | ||
// as seen in https://go.dev/src/net/sockopt_linux.go. Therefore, no | ||
// additional action is required to enable it here. | ||
|
||
setSockOptErr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unixSOREUSEPORT, 1) | ||
if setSockOptErr != nil { | ||
return | ||
} | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("control: %w", err) | ||
} | ||
if setSockOptErr != nil { | ||
return fmt.Errorf("setsockopt: %w", setSockOptErr) | ||
} | ||
return nil | ||
} |
7 changes: 7 additions & 0 deletions
7
pkg/server/server_entrypoint_listenconfig_unix_sockopt_freebsd.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//go:build freebsd | ||
|
||
package server | ||
|
||
import "golang.org/x/sys/unix" | ||
|
||
const unixSOREUSEPORT = unix.SO_REUSEPORT_LB |
7 changes: 7 additions & 0 deletions
7
pkg/server/server_entrypoint_listenconfig_unix_sockopt_other.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//go:build linux || openbsd || darwin | ||
|
||
package server | ||
|
||
import "golang.org/x/sys/unix" | ||
|
||
const unixSOREUSEPORT = unix.SO_REUSEPORT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//go:build linux || freebsd || openbsd || darwin | ||
|
||
package server | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/traefik/traefik/v3/pkg/config/static" | ||
) | ||
|
||
func TestNewListenConfig(t *testing.T) { | ||
ep := static.EntryPoint{Address: ":0"} | ||
listenConfig := newListenConfig(&ep) | ||
require.Nil(t, listenConfig.Control) | ||
require.Zero(t, listenConfig.KeepAlive) | ||
|
||
l1, err := listenConfig.Listen(context.Background(), "tcp", ep.Address) | ||
require.NoError(t, err) | ||
require.NotNil(t, l1) | ||
defer l1.Close() | ||
|
||
l2, err := listenConfig.Listen(context.Background(), "tcp", l1.Addr().String()) | ||
require.Error(t, err) | ||
require.ErrorContains(t, err, "address already in use") | ||
require.Nil(t, l2) | ||
|
||
ep = static.EntryPoint{Address: ":0", ReusePort: true} | ||
listenConfig = newListenConfig(&ep) | ||
require.NotNil(t, listenConfig.Control) | ||
require.Zero(t, listenConfig.KeepAlive) | ||
|
||
l3, err := listenConfig.Listen(context.Background(), "tcp", ep.Address) | ||
require.NoError(t, err) | ||
require.NotNil(t, l3) | ||
defer l3.Close() | ||
|
||
l4, err := listenConfig.Listen(context.Background(), "tcp", l3.Addr().String()) | ||
require.NoError(t, err) | ||
require.NotNil(t, l4) | ||
defer l4.Close() | ||
|
||
_, l3Port, err := net.SplitHostPort(l3.Addr().String()) | ||
require.NoError(t, err) | ||
l5, err := listenConfig.Listen(context.Background(), "tcp", "127.0.0.1:"+l3Port) | ||
require.NoError(t, err) | ||
require.NotNil(t, l5) | ||
defer l5.Close() | ||
|
||
l6, err := listenConfig.Listen(context.Background(), "tcp", l1.Addr().String()) | ||
require.Error(t, err) | ||
require.ErrorContains(t, err, "address already in use") | ||
require.Nil(t, l6) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.