Skip to content
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

feat: add support for ReuseAddr #1510

Merged
merged 4 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions listen_no_reuseport.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import "net"

const supportsReusePort = false

func listenTCP(network, addr string, reuseport bool) (net.Listener, error) {
if reuseport {
func listenTCP(network, addr string, reuseport, reuseaddr bool) (net.Listener, error) {
if reuseport || reuseaddr {
// TODO(tmthrgd): return an error?
}

return net.Listen(network, addr)
}

func listenUDP(network, addr string, reuseport bool) (net.PacketConn, error) {
if reuseport {
const supportsReuseAddr = false

func listenUDP(network, addr string, reuseport, reuseaddr bool) (net.PacketConn, error) {
if reuseport || reuseaddr {
// TODO(tmthrgd): return an error?
}

Expand Down
30 changes: 26 additions & 4 deletions listen_reuseport.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,41 @@ func reuseportControl(network, address string, c syscall.RawConn) error {
return opErr
}

func listenTCP(network, addr string, reuseport bool) (net.Listener, error) {
const supportsReuseAddr = true

func reuseaddrControl(network, address string, c syscall.RawConn) error {
var opErr error
err := c.Control(func(fd uintptr) {
opErr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
})
if err != nil {
return err
}

return opErr
}

func listenTCP(network, addr string, reuseport bool, reuseaddr bool) (net.Listener, error) {
miekg marked this conversation as resolved.
Show resolved Hide resolved
var lc net.ListenConfig
if reuseport {
switch {
case reuseaddr && reuseport:
case reuseport:
lc.Control = reuseportControl
case reuseaddr:
lc.Control = reuseaddrControl
}

return lc.Listen(context.Background(), network, addr)
}

func listenUDP(network, addr string, reuseport bool) (net.PacketConn, error) {
func listenUDP(network, addr string, reuseport bool, reuseaddr bool) (net.PacketConn, error) {
miekg marked this conversation as resolved.
Show resolved Hide resolved
var lc net.ListenConfig
if reuseport {
switch {
case reuseaddr && reuseport:
case reuseport:
lc.Control = reuseportControl
case reuseaddr:
lc.Control = reuseaddrControl
}

return lc.ListenPacket(context.Background(), network, addr)
Expand Down
10 changes: 7 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ type Server struct {
// Whether to set the SO_REUSEPORT socket option, allowing multiple listeners to be bound to a single address.
// It is only supported on certain GOOSes and when using ListenAndServe.
ReusePort bool
// Whether to set the SO_REUSEADDR socket option, allowing multiple listeners to be bound to a single address.
// Crucially this allows binding when an existing server is listening on `0.0.0.0` or `::`.
// It is only supported on certain GOOSes and when using ListenAndServe.
ReuseAddr bool
// AcceptMsgFunc will check the incoming message and will reject it early in the process.
// By default DefaultMsgAcceptFunc will be used.
MsgAcceptFunc MsgAcceptFunc
Expand Down Expand Up @@ -304,7 +308,7 @@ func (srv *Server) ListenAndServe() error {

switch srv.Net {
case "tcp", "tcp4", "tcp6":
l, err := listenTCP(srv.Net, addr, srv.ReusePort)
l, err := listenTCP(srv.Net, addr, srv.ReusePort, srv.ReuseAddr)
if err != nil {
return err
}
Expand All @@ -317,7 +321,7 @@ func (srv *Server) ListenAndServe() error {
return errors.New("dns: neither Certificates nor GetCertificate set in Config")
}
network := strings.TrimSuffix(srv.Net, "-tls")
l, err := listenTCP(network, addr, srv.ReusePort)
l, err := listenTCP(network, addr, srv.ReusePort, srv.ReuseAddr)
if err != nil {
return err
}
Expand All @@ -327,7 +331,7 @@ func (srv *Server) ListenAndServe() error {
unlock()
return srv.serveTCP(l)
case "udp", "udp4", "udp6":
l, err := listenUDP(srv.Net, addr, srv.ReusePort)
l, err := listenUDP(srv.Net, addr, srv.ReusePort, srv.ReuseAddr)
if err != nil {
return err
}
Expand Down
69 changes: 69 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,75 @@ func TestServerReuseport(t *testing.T) {
}
}

func TestServerReuseaddr(t *testing.T) {
startServer := func(t *testing.T, addr string, expectSuccess bool) (*Server, chan error) {
wait := make(chan struct{})
srv := &Server{
Net: "udp",
Addr: addr,
NotifyStartedFunc: func() { close(wait) },
ReuseAddr: true,
}

fin := make(chan error, 1)
go func() {
fin <- srv.ListenAndServe()
}()

select {
case <-wait:
case err := <-fin:
switch {
case expectSuccess:
t.Fatalf("%s: failed to start server: %v", t.Name(), err)
default:
fin <- err
return srv, fin
}
}
return srv, fin
}
t.Run("should-fail", func(t *testing.T) {
// ReuseAddr should fail if you try to bind to exactly the same
// combination of source address and port.
// This should fail whether or not ReuseAddr is supported on a
// particular OS
srv1, fin1 := startServer(t, ":0", true) // :0 is resolved to a random free port by the kernel
_, fin2 := startServer(t, srv1.PacketConn.LocalAddr().String(), false)
if err := <-fin2; err == nil {
t.Fatalf("second ListenAndServe should have returned a startup error: %v", err)
}

if err := srv1.Shutdown(); err != nil {
t.Fatalf("failed to shutdown first server: %v", err)
}
if err := <-fin1; err != nil {
t.Fatalf("first ListenAndServe returned error after Shutdown: %v", err)
}
})
t.Run("should-succeed", func(t *testing.T) {
if !supportsReuseAddr {
t.Skip("reuseaddr is not supported")
}
// ReuseAddr should succeed if you try to bind to the same port but a different source address
srv1, fin1 := startServer(t, ":0", true) // :0 is resolved to a random free port by the kernel
srv2, fin2 := startServer(t, fmt.Sprintf("localhost:%d", srv1.PacketConn.LocalAddr().(*net.UDPAddr).Port), true)

if err := srv1.Shutdown(); err != nil {
t.Fatalf("failed to shutdown first server: %v", err)
}
if err := srv2.Shutdown(); err != nil {
t.Fatalf("failed to shutdown second server: %v", err)
}
if err := <-fin1; err != nil {
t.Fatalf("first ListenAndServe returned error after Shutdown: %v", err)
}
if err := <-fin2; err != nil {
t.Fatalf("second ListenAndServe returned error after Shutdown: %v", err)
}
})
}

func TestServerRoundtripTsig(t *testing.T) {
secret := map[string]string{"test.": "so6ZGir4GPAqINNh9U5c3A=="}

Expand Down