🐛 bug: remove prefork support from custom listeners (#2060)

* 🐛 bug: remove prefork support from custom listeners

* Update listen_test.go
pull/2064/head
M. Efe Çetin 2022-08-30 14:01:24 +03:00 committed by GitHub
parent e72ea32dd2
commit cbfcac2c0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 5 additions and 91 deletions

View File

@ -24,37 +24,6 @@ import (
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )
/* #nosec */
// lnMetadata will close the listener and return the addr and tls config
func lnMetadata(network string, ln net.Listener) (addr string, cfg *tls.Config) {
// Get addr
addr = ln.Addr().String()
// Close listener
if err := ln.Close(); err != nil {
return
}
// Wait for the listener to be closed
var closed bool
for i := 0; i < 10; i++ {
conn, err := net.DialTimeout(network, addr, 3*time.Second)
if err != nil || conn == nil {
closed = true
break
}
_ = conn.Close()
time.Sleep(100 * time.Millisecond)
}
if !closed {
panic("listener: " + addr + ": Only one usage of each socket address (protocol/network address/port) is normally permitted.")
}
cfg = getTlsConfig(ln)
return
}
/* #nosec */ /* #nosec */
// getTlsConfig returns a net listener's tls config // getTlsConfig returns a net listener's tls config
func getTlsConfig(ln net.Listener) *tls.Config { func getTlsConfig(ln net.Listener) *tls.Config {

View File

@ -5,9 +5,7 @@
package fiber package fiber
import ( import (
"crypto/tls"
"fmt" "fmt"
"net"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -260,48 +258,6 @@ func Benchmark_Utils_IsNoCache(b *testing.B) {
utils.AssertEqual(b, true, ok) utils.AssertEqual(b, true, ok)
} }
func Test_Utils_lnMetadata(t *testing.T) {
t.Run("closed listen", func(t *testing.T) {
ln, err := net.Listen(NetworkTCP, ":0")
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, nil, ln.Close())
addr, config := lnMetadata(NetworkTCP, ln)
utils.AssertEqual(t, ln.Addr().String(), addr)
utils.AssertEqual(t, true, config == nil)
})
t.Run("non tls", func(t *testing.T) {
ln, err := net.Listen(NetworkTCP, ":0")
utils.AssertEqual(t, nil, err)
addr, config := lnMetadata(NetworkTCP4, ln)
utils.AssertEqual(t, ln.Addr().String(), addr)
utils.AssertEqual(t, true, config == nil)
})
t.Run("tls", func(t *testing.T) {
cer, err := tls.LoadX509KeyPair("./.github/testdata/ssl.pem", "./.github/testdata/ssl.key")
utils.AssertEqual(t, nil, err)
config := &tls.Config{Certificates: []tls.Certificate{cer}}
ln, err := net.Listen(NetworkTCP4, ":0")
utils.AssertEqual(t, nil, err)
ln = tls.NewListener(ln, config)
addr, config := lnMetadata(NetworkTCP4, ln)
utils.AssertEqual(t, ln.Addr().String(), addr)
utils.AssertEqual(t, true, config != nil)
})
}
// go test -v -run=^$ -bench=Benchmark_SlashRecognition -benchmem -count=4 // go test -v -run=^$ -bench=Benchmark_SlashRecognition -benchmem -count=4
func Benchmark_SlashRecognition(b *testing.B) { func Benchmark_SlashRecognition(b *testing.B) {
search := "wtf/1234" search := "wtf/1234"

View File

@ -26,12 +26,6 @@ import (
// Listener can be used to pass a custom listener. // Listener can be used to pass a custom listener.
func (app *App) Listener(ln net.Listener) error { func (app *App) Listener(ln net.Listener) error {
// Prefork is supported for custom listeners
if app.config.Prefork {
addr, tlsConfig := lnMetadata(app.config.Network, ln)
return app.prefork(app.config.Network, addr, tlsConfig)
}
// prepare the server for the start // prepare the server for the start
app.startupProcess() app.startupProcess()
@ -45,6 +39,11 @@ func (app *App) Listener(ln net.Listener) error {
app.printRoutesMessage() app.printRoutesMessage()
} }
// Prefork is not supported for custom listeners
if app.config.Prefork {
fmt.Println("[Warning] Prefork isn't supported for custom listeners.")
}
// Start listening // Start listening
return app.server.Serve(ln) return app.server.Serve(ln)
} }

View File

@ -114,16 +114,6 @@ func Test_App_Listener(t *testing.T) {
utils.AssertEqual(t, nil, app.Listener(ln)) utils.AssertEqual(t, nil, app.Listener(ln))
} }
// go test -run Test_App_Listener_Prefork
func Test_App_Listener_Prefork(t *testing.T) {
testPreforkMaster = true
app := New(Config{DisableStartupMessage: true, Prefork: true})
ln := fasthttputil.NewInmemoryListener()
utils.AssertEqual(t, nil, app.Listener(ln))
}
func Test_App_Listener_TLS_Listener(t *testing.T) { func Test_App_Listener_TLS_Listener(t *testing.T) {
// Create tls certificate // Create tls certificate
cer, err := tls.LoadX509KeyPair("./.github/testdata/ssl.pem", "./.github/testdata/ssl.key") cer, err := tls.LoadX509KeyPair("./.github/testdata/ssl.pem", "./.github/testdata/ssl.key")