🔍 Auto detect TCP protocol

pull/640/head
Fenny 2020-07-18 01:17:52 +02:00 committed by ReneWerner87
parent 1dce1a08fd
commit 4554ea135c
2 changed files with 8 additions and 22 deletions

24
app.go
View File

@ -107,11 +107,6 @@ type Settings struct {
// Default: false
ETag bool `json:"etag"`
// Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only)
// Prefork does not support the IPv6 network
// Default: "tcp"
Network string
// When set to true, this will spawn multiple Go processes listening on the same port.
// Default: false
Prefork bool `json:"prefork"`
@ -220,7 +215,6 @@ const (
defaultReadBufferSize = 4096
defaultWriteBufferSize = 4096
defaultCompressedFileSuffix = ".fiber.gz"
defaultNetwork = "tcp"
)
var defaultErrorHandler = func(ctx *Ctx, err error) {
@ -277,9 +271,6 @@ func New(settings ...*Settings) *App {
if app.Settings.ErrorHandler == nil {
app.Settings.ErrorHandler = defaultErrorHandler
}
if app.Settings.Network == "" {
app.Settings.Network = defaultNetwork
}
if app.Settings.Immutable {
getBytes, getString = getBytesImmutable, getStringImmutable
}
@ -488,16 +479,17 @@ func (app *App) Listen(address interface{}, tlsconfig ...*tls.Config) error {
}
// Update fiber server settings
app.init()
// Set correct network protocol
network := "tcp4"
if isIPv6(addr) {
network = "tcp6"
}
// Start prefork
if app.Settings.Prefork {
// Prefork only supports tcp4 or tcp6, but not both
if isIPv6(addr) {
app.Settings.Network = "tcp6"
}
return app.prefork(addr, tlsconfig...)
return app.prefork(network, addr, tlsconfig...)
}
// Setup listener
ln, err := net.Listen(app.Settings.Network, addr)
ln, err := net.Listen(network, addr)
if err != nil {
return err
}
@ -610,8 +602,6 @@ func (app *App) init() *App {
fmt.Printf("views: %v\n", err)
}
}
// TCP4 -> tcp4
app.Settings.Network = utils.ToLower(app.Settings.Network)
}
if app.server == nil {
app.server = &fasthttp.Server{

View File

@ -29,16 +29,12 @@ func (app *App) IsChild() bool {
}
// prefork manages child processes to make use of the OS REUSEPORT or REUSEADDR feature
func (app *App) prefork(addr string, tlsconfig ...*tls.Config) (err error) {
func (app *App) prefork(network, addr string, tlsconfig ...*tls.Config) (err error) {
// 👶 child process 👶
if app.IsChild() {
// use 1 cpu core per child process
runtime.GOMAXPROCS(1)
var ln net.Listener
network := "tcp4"
if app.Settings.Network == "tcp6" {
network = app.Settings.Network
}
// Linux will use SO_REUSEPORT and Windows falls back to SO_REUSEADDR
// Only tcp4 or tcp6 is supported when preforking, both are not supported
if ln, err = reuseport.Listen(network, addr); err != nil {