From 2f6ffb69ec5a47ec6366c7b76aa7440095aba9bc Mon Sep 17 00:00:00 2001 From: andrewheberle Date: Mon, 19 Aug 2019 10:34:40 +0800 Subject: [PATCH 01/16] Create Windows stub for reuseport.Listen --- reuseport/reuseport_windows.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 reuseport/reuseport_windows.go diff --git a/reuseport/reuseport_windows.go b/reuseport/reuseport_windows.go new file mode 100644 index 0000000000..733c05c360 --- /dev/null +++ b/reuseport/reuseport_windows.go @@ -0,0 +1,27 @@ +// Package reuseport provides TCP net.Listener with SO_REUSEPORT support. +// +// SO_REUSEPORT allows linear scaling server performance on multi-CPU servers. +// See https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/ for more details :) +// +// The package is based on https://github.com/kavu/go_reuseport . +package reuseport + +import ( + "fmt" + "net" +) + +// ErrNoReusePort is returned if the OS doesn't support SO_REUSEPORT. +type ErrNoReusePort struct { + err error +} + +// Error implements error interface. +func (e *ErrNoReusePort) Error() string { + return fmt.Sprint("The OS doesn't support SO_REUSEPORT: %s", e.err) +} + +// Listen always returns ErrNoReusePort on Windows +func Listen(network, addr string) (net.Listener, error) { + return nil, &ErrNoReusePort{fmt.Errorf("Not supported on Windows")} +} From 9dc50d5c7c35fee5354d323c64dc0fb6df5d0605 Mon Sep 17 00:00:00 2001 From: andrewheberle Date: Mon, 19 Aug 2019 11:12:04 +0800 Subject: [PATCH 02/16] Fix Sprintf --- reuseport/reuseport_windows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reuseport/reuseport_windows.go b/reuseport/reuseport_windows.go index 733c05c360..806e9dc53c 100644 --- a/reuseport/reuseport_windows.go +++ b/reuseport/reuseport_windows.go @@ -18,7 +18,7 @@ type ErrNoReusePort struct { // Error implements error interface. func (e *ErrNoReusePort) Error() string { - return fmt.Sprint("The OS doesn't support SO_REUSEPORT: %s", e.err) + return fmt.Sprintf("The OS doesn't support SO_REUSEPORT: %s", e.err) } // Listen always returns ErrNoReusePort on Windows From 29971b24a47d62799b450c762ba08a11a55fed52 Mon Sep 17 00:00:00 2001 From: andrewheberle Date: Mon, 19 Aug 2019 11:14:00 +0800 Subject: [PATCH 03/16] Update reuseport_windows.go --- reuseport/reuseport_windows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reuseport/reuseport_windows.go b/reuseport/reuseport_windows.go index 806e9dc53c..d581ed53b0 100644 --- a/reuseport/reuseport_windows.go +++ b/reuseport/reuseport_windows.go @@ -8,7 +8,7 @@ package reuseport import ( "fmt" - "net" + "net" ) // ErrNoReusePort is returned if the OS doesn't support SO_REUSEPORT. From da80148acd721848475ec1510548766c3a7876c2 Mon Sep 17 00:00:00 2001 From: andrewheberle Date: Mon, 19 Aug 2019 12:08:48 +0800 Subject: [PATCH 04/16] Add tests --- reuseport/reuseport_test_windows.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 reuseport/reuseport_test_windows.go diff --git a/reuseport/reuseport_test_windows.go b/reuseport/reuseport_test_windows.go new file mode 100644 index 0000000000..34809d561c --- /dev/null +++ b/reuseport/reuseport_test_windows.go @@ -0,0 +1,16 @@ +package reuseport + +import ( + "testing" +) + +func TestListen(t *testing.T) { + ln, err := Listen("tcp6", "[::1]:10082") + if err == nil { + t.Fatalf("unexpected non-error creating listener") + } + + if _, errnoreuseport := err.(*ErrNoReusePort); !errnoreuseport { + t.Fatalf("unexpected error creating listener: %s", err) + } +} From f08025ed0c7fe3cbc82bd5e9436f06876cb4067e Mon Sep 17 00:00:00 2001 From: andrewheberle Date: Mon, 19 Aug 2019 12:10:08 +0800 Subject: [PATCH 05/16] Removed unused variable --- reuseport/reuseport_test_windows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reuseport/reuseport_test_windows.go b/reuseport/reuseport_test_windows.go index 34809d561c..fadebe4a37 100644 --- a/reuseport/reuseport_test_windows.go +++ b/reuseport/reuseport_test_windows.go @@ -5,7 +5,7 @@ import ( ) func TestListen(t *testing.T) { - ln, err := Listen("tcp6", "[::1]:10082") + _, err := Listen("tcp6", "[::1]:10082") if err == nil { t.Fatalf("unexpected non-error creating listener") } From f6d263094745744b12a67d03ab343ff8ae411ed2 Mon Sep 17 00:00:00 2001 From: andrewheberle Date: Mon, 19 Aug 2019 12:16:35 +0800 Subject: [PATCH 06/16] Rename reuseport_test_windows.go to reuseport_windows_test.go --- .../{reuseport_test_windows.go => reuseport_windows_test.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename reuseport/{reuseport_test_windows.go => reuseport_windows_test.go} (100%) diff --git a/reuseport/reuseport_test_windows.go b/reuseport/reuseport_windows_test.go similarity index 100% rename from reuseport/reuseport_test_windows.go rename to reuseport/reuseport_windows_test.go From a04f80be22e22702338d1898fd0fa392ccc92f27 Mon Sep 17 00:00:00 2001 From: andrewheberle Date: Mon, 19 Aug 2019 12:17:03 +0800 Subject: [PATCH 07/16] Update reuseport_test.go --- reuseport/reuseport_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/reuseport/reuseport_test.go b/reuseport/reuseport_test.go index e781ff11f3..0eac3041f3 100644 --- a/reuseport/reuseport_test.go +++ b/reuseport/reuseport_test.go @@ -1,3 +1,5 @@ +// +build linux darwin dragonfly freebsd netbsd openbsd rumprun + package reuseport import ( From 6911442638a45bd6f2172d6d52a094ee91276d4d Mon Sep 17 00:00:00 2001 From: andrewheberle Date: Mon, 19 Aug 2019 21:12:42 +0800 Subject: [PATCH 08/16] Update reuseport/reuseport_test.go Co-Authored-By: Erik Dubbelboer --- reuseport/reuseport_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reuseport/reuseport_test.go b/reuseport/reuseport_test.go index 0eac3041f3..7763d3feb4 100644 --- a/reuseport/reuseport_test.go +++ b/reuseport/reuseport_test.go @@ -1,4 +1,4 @@ -// +build linux darwin dragonfly freebsd netbsd openbsd rumprun +// +build !windows package reuseport From 27beb928642c5463e3f72ca49b6c8f314edf2d32 Mon Sep 17 00:00:00 2001 From: andrewheberle Date: Mon, 19 Aug 2019 21:14:41 +0800 Subject: [PATCH 09/16] Remove comment --- reuseport/reuseport_windows.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/reuseport/reuseport_windows.go b/reuseport/reuseport_windows.go index d581ed53b0..01f7dbca84 100644 --- a/reuseport/reuseport_windows.go +++ b/reuseport/reuseport_windows.go @@ -1,9 +1,3 @@ -// Package reuseport provides TCP net.Listener with SO_REUSEPORT support. -// -// SO_REUSEPORT allows linear scaling server performance on multi-CPU servers. -// See https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/ for more details :) -// -// The package is based on https://github.com/kavu/go_reuseport . package reuseport import ( From 2fa960e89ac88e69178ee1354d093247d9bbf002 Mon Sep 17 00:00:00 2001 From: andrewheberle Date: Thu, 22 Aug 2019 07:30:27 +0800 Subject: [PATCH 10/16] Update reuseport_windows.go --- reuseport/reuseport_windows.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/reuseport/reuseport_windows.go b/reuseport/reuseport_windows.go index 01f7dbca84..8137eb5ba0 100644 --- a/reuseport/reuseport_windows.go +++ b/reuseport/reuseport_windows.go @@ -5,16 +5,6 @@ import ( "net" ) -// ErrNoReusePort is returned if the OS doesn't support SO_REUSEPORT. -type ErrNoReusePort struct { - err error -} - -// Error implements error interface. -func (e *ErrNoReusePort) Error() string { - return fmt.Sprintf("The OS doesn't support SO_REUSEPORT: %s", e.err) -} - // Listen always returns ErrNoReusePort on Windows func Listen(network, addr string) (net.Listener, error) { return nil, &ErrNoReusePort{fmt.Errorf("Not supported on Windows")} From 90e251822e937a4c460be1e56813b3bd73ec99de Mon Sep 17 00:00:00 2001 From: andrewheberle Date: Thu, 22 Aug 2019 07:31:08 +0800 Subject: [PATCH 11/16] Create reuseport_error.go --- reuseport/reuseport_error.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 reuseport/reuseport_error.go diff --git a/reuseport/reuseport_error.go b/reuseport/reuseport_error.go new file mode 100644 index 0000000000..9165625cea --- /dev/null +++ b/reuseport/reuseport_error.go @@ -0,0 +1,15 @@ +package reuseport + +import ( + "fmt" +) + +// ErrNoReusePort is returned if the OS doesn't support SO_REUSEPORT. +type ErrNoReusePort struct { + err error +} + +// Error implements error interface. +func (e *ErrNoReusePort) Error() string { + return fmt.Sprintf("The OS doesn't support SO_REUSEPORT: %s", e.err) +} From c14e2fc76896d07184deed00b610a33e3ec024a5 Mon Sep 17 00:00:00 2001 From: andrewheberle Date: Thu, 22 Aug 2019 07:31:38 +0800 Subject: [PATCH 12/16] Update reuseport.go --- reuseport/reuseport.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/reuseport/reuseport.go b/reuseport/reuseport.go index 191fe64103..8d920d2894 100644 --- a/reuseport/reuseport.go +++ b/reuseport/reuseport.go @@ -16,16 +16,6 @@ import ( "github.com/valyala/tcplisten" ) -// ErrNoReusePort is returned if the OS doesn't support SO_REUSEPORT. -type ErrNoReusePort struct { - err error -} - -// Error implements error interface. -func (e *ErrNoReusePort) Error() string { - return fmt.Sprintf("The OS doesn't support SO_REUSEPORT: %s", e.err) -} - // Listen returns TCP listener with SO_REUSEPORT option set. // // The returned listener tries enabling the following TCP options, which usually From 3714b83a269dd5e0a51ab0f563d65b82eb36b058 Mon Sep 17 00:00:00 2001 From: andrewheberle Date: Thu, 22 Aug 2019 07:37:28 +0800 Subject: [PATCH 13/16] Indents --- reuseport/reuseport_error.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reuseport/reuseport_error.go b/reuseport/reuseport_error.go index 9165625cea..3e29d42ae7 100644 --- a/reuseport/reuseport_error.go +++ b/reuseport/reuseport_error.go @@ -1,7 +1,7 @@ package reuseport import ( - "fmt" + "fmt" ) // ErrNoReusePort is returned if the OS doesn't support SO_REUSEPORT. From 8ef82b3c7f8818b62f001bbc30e8e7712e0eb827 Mon Sep 17 00:00:00 2001 From: andrewheberle Date: Thu, 22 Aug 2019 07:46:52 +0800 Subject: [PATCH 14/16] Remove unsused import --- reuseport/reuseport.go | 1 - 1 file changed, 1 deletion(-) diff --git a/reuseport/reuseport.go b/reuseport/reuseport.go index 8d920d2894..82f7b54fd8 100644 --- a/reuseport/reuseport.go +++ b/reuseport/reuseport.go @@ -9,7 +9,6 @@ package reuseport import ( - "fmt" "net" "strings" From 47dba90db0b7ca9dae0266b91c1a8d4468506137 Mon Sep 17 00:00:00 2001 From: andrewheberle Date: Mon, 26 Aug 2019 07:50:32 +0800 Subject: [PATCH 15/16] Build constraint not for windows --- reuseport/reuseport.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reuseport/reuseport.go b/reuseport/reuseport.go index 82f7b54fd8..85c7d65d0d 100644 --- a/reuseport/reuseport.go +++ b/reuseport/reuseport.go @@ -1,4 +1,4 @@ -// +build linux darwin dragonfly freebsd netbsd openbsd rumprun +// +build !windows // Package reuseport provides TCP net.Listener with SO_REUSEPORT support. // From a95ce0f91159d08c100c2986d17eee0bc93634fe Mon Sep 17 00:00:00 2001 From: andrewheberle Date: Mon, 26 Aug 2019 07:51:11 +0800 Subject: [PATCH 16/16] Build only for windows --- reuseport/reuseport_windows_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/reuseport/reuseport_windows_test.go b/reuseport/reuseport_windows_test.go index fadebe4a37..934526730f 100644 --- a/reuseport/reuseport_windows_test.go +++ b/reuseport/reuseport_windows_test.go @@ -1,3 +1,5 @@ +// +build windows + package reuseport import (