Browse Source

Merge pull request #70 from Vigilans/vigilans/inbound-sockopt

Move setting of SO_REUSEPORT out of applyInboundSocketOptions
Kslr 5 years ago
parent
commit
746ec61fb6

+ 8 - 0
transport/internet/sockopt_darwin.go

@@ -50,3 +50,11 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
 func bindAddr(fd uintptr, address []byte, port uint32) error {
 	return nil
 }
+
+func setReuseAddr(fd uintptr) error {
+	return nil
+}
+
+func setReusePort(fd uintptr) error {
+	return nil
+}

+ 18 - 9
transport/internet/sockopt_freebsd.go

@@ -188,15 +188,8 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
 }
 
 func bindAddr(fd uintptr, ip []byte, port uint32) error {
-	if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
-		return newError("failed to set resuse_addr").Base(err).AtWarning()
-	}
-
-	if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, soReUsePortLB, 1); err != nil {
-		if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, soReUsePort, 1); err != nil {
-			return newError("failed to set resuse_port").Base(err).AtWarning()
-		}
-	}
+	setReuseAddr(fd)
+	setReusePort(fd)
 
 	var sockaddr syscall.Sockaddr
 
@@ -219,3 +212,19 @@ func bindAddr(fd uintptr, ip []byte, port uint32) error {
 
 	return syscall.Bind(int(fd), sockaddr)
 }
+
+func setReuseAddr(fd uintptr) error {
+	if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
+		return newError("failed to set SO_REUSEADDR").Base(err).AtWarning()
+	}
+	return nil
+}
+
+func setReusePort(fd uintptr) error {
+	if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, soReUsePortLB, 1); err != nil {
+		if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, soReUsePort, 1); err != nil {
+			return newError("failed to set SO_REUSEPORT").Base(err).AtWarning()
+		}
+	}
+	return nil
+}

+ 13 - 8
transport/internet/sockopt_linux.go

@@ -15,13 +15,8 @@ const (
 )
 
 func bindAddr(fd uintptr, ip []byte, port uint32) error {
-	if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
-		return newError("failed to set resuse_addr").Base(err).AtWarning()
-	}
-
-	if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
-		return newError("failed to set resuse_port").Base(err).AtWarning()
-	}
+	setReuseAddr(fd)
+	setReusePort(fd)
 
 	var sockaddr syscall.Sockaddr
 
@@ -107,9 +102,19 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
 		}
 	}
 
+	return nil
+}
+
+func setReuseAddr(fd uintptr) error {
+	if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
+		return newError("failed to set SO_REUSEADDR").Base(err).AtWarning()
+	}
+	return nil
+}
+
+func setReusePort(fd uintptr) error {
 	if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
 		return newError("failed to set SO_REUSEPORT").Base(err).AtWarning()
 	}
-
 	return nil
 }

+ 8 - 0
transport/internet/sockopt_other.go

@@ -13,3 +13,11 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
 func bindAddr(fd uintptr, ip []byte, port uint32) error {
 	return nil
 }
+
+func setReuseAddr(fd uintptr) error {
+	return nil
+}
+
+func setReusePort(fd uintptr) error {
+	return nil
+}

+ 8 - 0
transport/internet/sockopt_windows.go

@@ -46,3 +46,11 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
 func bindAddr(fd uintptr, ip []byte, port uint32) error {
 	return nil
 }
+
+func setReuseAddr(fd uintptr) error {
+	return nil
+}
+
+func setReusePort(fd uintptr) error {
+	return nil
+}

+ 4 - 6
transport/internet/system_listener.go

@@ -27,6 +27,8 @@ func getControlFunc(ctx context.Context, sockopt *SocketConfig, controllers []co
 				}
 			}
 
+			setReusePort(fd)
+
 			for _, controller := range controllers {
 				if err := controller(network, address, fd); err != nil {
 					newError("failed to apply external controller").Base(err).WriteToLog(session.ExportIDToError(ctx))
@@ -39,9 +41,7 @@ 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
 
-	if sockopt != nil || len(dl.controllers) > 0 {
-		lc.Control = getControlFunc(ctx, sockopt, dl.controllers)
-	}
+	lc.Control = getControlFunc(ctx, sockopt, dl.controllers)
 
 	return lc.Listen(ctx, addr.Network(), addr.String())
 }
@@ -49,9 +49,7 @@ func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *S
 func (dl *DefaultListener) ListenPacket(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.PacketConn, error) {
 	var lc net.ListenConfig
 
-	if sockopt != nil || len(dl.controllers) > 0 {
-		lc.Control = getControlFunc(ctx, sockopt, dl.controllers)
-	}
+	lc.Control = getControlFunc(ctx, sockopt, dl.controllers)
 
 	return lc.ListenPacket(ctx, addr.Network(), addr.String())
 }