diff --git a/go.mod b/go.mod index 5b8c0c3c6e..f7659c671a 100644 --- a/go.mod +++ b/go.mod @@ -9,4 +9,6 @@ require ( github.com/mattn/go-isatty v0.0.12 github.com/valyala/bytebufferpool v1.0.0 github.com/valyala/fasthttp v1.14.0 + github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a + golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae ) diff --git a/go.sum b/go.sum index 97c3d86d6d..6275107382 100644 --- a/go.sum +++ b/go.sum @@ -24,4 +24,6 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo= +golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/prefork.go b/prefork.go index 8d502bde94..22d6b3aa88 100644 --- a/prefork.go +++ b/prefork.go @@ -13,7 +13,6 @@ import ( "time" utils "github.com/gofiber/utils" - reuseport "github.com/valyala/fasthttp/reuseport" ) var ( @@ -37,7 +36,7 @@ func (app *App) prefork(addr string, tlsconfig ...*tls.Config) (err error) { runtime.GOMAXPROCS(1) var ln net.Listener // SO_REUSEPORT is not supported on Windows, use SO_REUSEADDR instead - if ln, err = reuseport.Listen("tcp4", addr); err != nil { + if ln, err = reuseport("tcp4", addr); err != nil { if !app.Settings.DisableStartupMessage { time.Sleep(100 * time.Millisecond) // avoid colliding with startup message } diff --git a/reuseport.go b/reuseport.go new file mode 100644 index 0000000000..9cf235f03e --- /dev/null +++ b/reuseport.go @@ -0,0 +1,45 @@ +// +build !windows + +// 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 fiber + +import ( + "net" + "strings" + + tcplisten "github.com/valyala/tcplisten" +) + +// Listen returns TCP listener with SO_REUSEPORT option set. +// +// The returned listener tries enabling the following TCP options, which usually +// have positive impact on performance: +// +// - TCP_DEFER_ACCEPT. This option expects that the server reads from accepted +// connections before writing to them. +// +// - TCP_FASTOPEN. See https://lwn.net/Articles/508865/ for details. +// +// Use https://github.com/valyala/tcplisten if you want customizing +// these options. +// +// Only tcp4 and tcp6 networks are supported. +// +// ErrNoReusePort error is returned if the system doesn't support SO_REUSEPORT. +func reuseport(network, addr string) (net.Listener, error) { + cfg := &tcplisten.Config{ + ReusePort: true, + DeferAccept: true, + FastOpen: true, + } + ln, err := cfg.NewListener(network, addr) + if err != nil && strings.Contains(err.Error(), "SO_REUSEPORT") { + return nil, err + } + return ln, err +} diff --git a/reuseport_windows.go b/reuseport_windows.go new file mode 100644 index 0000000000..f7a68b1238 --- /dev/null +++ b/reuseport_windows.go @@ -0,0 +1,22 @@ +package fiber + +import ( + "context" + "net" + "syscall" + + "golang.org/x/sys/windows" +) + +// Listen returns TCP listener with SO_REUSEADDR option set, SO_REUSEPORT is not supported on Windows, so it uses +// SO_REUSEADDR as an alternative to achieve the same effect. +func reuseport(network, addr string) (net.Listener, error) { + cfg := net.ListenConfig{ + Control: func(network, address string, c syscall.RawConn) (err error) { + return c.Control(func(fd uintptr) { + err = windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_REUSEADDR, 1) + }) + }, + } + return cfg.Listen(context.Background(), network, addr) +}