Skip to content

Commit

Permalink
split listen & listener
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Aug 26, 2022
1 parent 96d5309 commit db23bd9
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 66 deletions.
42 changes: 21 additions & 21 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Test_Client_Invalid_URL(t *testing.T) {
})

go func() {
require.Nil(t, app.Listen(ln, ListenConfig{
require.Nil(t, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -77,7 +77,7 @@ func Test_Client_Get(t *testing.T) {
})

go func() {
require.Nil(t, app.Listen(ln, ListenConfig{
require.Nil(t, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -107,7 +107,7 @@ func Test_Client_Head(t *testing.T) {
})

go func() {
require.Nil(t, app.Listen(ln, ListenConfig{
require.Nil(t, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -137,7 +137,7 @@ func Test_Client_Post(t *testing.T) {
})

go func() {
require.Nil(t, app.Listen(ln, ListenConfig{
require.Nil(t, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -174,7 +174,7 @@ func Test_Client_Put(t *testing.T) {
})

go func() {
require.Nil(t, app.Listen(ln, ListenConfig{
require.Nil(t, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -211,7 +211,7 @@ func Test_Client_Patch(t *testing.T) {
})

go func() {
require.Nil(t, nil, app.Listen(ln, ListenConfig{
require.Nil(t, nil, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -249,7 +249,7 @@ func Test_Client_Delete(t *testing.T) {
})

go func() {
require.Nil(t, app.Listen(ln, ListenConfig{
require.Nil(t, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -283,7 +283,7 @@ func Test_Client_UserAgent(t *testing.T) {
})

go func() {
require.Nil(t, nil, app.Listen(ln, ListenConfig{
require.Nil(t, nil, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -429,7 +429,7 @@ func Test_Client_Agent_Host(t *testing.T) {
})

go func() {
require.Nil(t, app.Listen(ln, ListenConfig{
require.Nil(t, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -529,7 +529,7 @@ func Test_Client_Agent_Custom_Response(t *testing.T) {
})

go func() {
require.Nil(t, app.Listen(ln, ListenConfig{
require.Nil(t, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -570,7 +570,7 @@ func Test_Client_Agent_Dest(t *testing.T) {
})

go func() {
require.Nil(t, nil, app.Listen(ln, ListenConfig{
require.Nil(t, nil, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -638,7 +638,7 @@ func Test_Client_Agent_RetryIf(t *testing.T) {
app := New()

go func() {
require.Nil(t, app.Listen(ln, ListenConfig{
require.Nil(t, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -759,7 +759,7 @@ func Test_Client_Agent_MultipartForm(t *testing.T) {
})

go func() {
require.Nil(t, nil, app.Listen(ln, ListenConfig{
require.Nil(t, nil, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -833,7 +833,7 @@ func Test_Client_Agent_MultipartForm_SendFiles(t *testing.T) {
})

go func() {
require.Nil(t, nil, app.Listen(ln, ListenConfig{
require.Nil(t, nil, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -948,7 +948,7 @@ func Test_Client_Agent_Timeout(t *testing.T) {
})

go func() {
require.Nil(t, nil, app.Listen(ln, ListenConfig{
require.Nil(t, nil, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -977,7 +977,7 @@ func Test_Client_Agent_Reuse(t *testing.T) {
})

go func() {
require.Nil(t, nil, app.Listen(ln, ListenConfig{
require.Nil(t, nil, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -1022,7 +1022,7 @@ func Test_Client_Agent_InsecureSkipVerify(t *testing.T) {
})

go func() {
require.Nil(t, nil, app.Listen(ln, ListenConfig{
require.Nil(t, nil, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -1055,7 +1055,7 @@ func Test_Client_Agent_TLS(t *testing.T) {
})

go func() {
require.Nil(t, nil, app.Listen(ln, ListenConfig{
require.Nil(t, nil, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -1087,7 +1087,7 @@ func Test_Client_Agent_MaxRedirectsCount(t *testing.T) {
})

go func() {
require.Nil(t, nil, app.Listen(ln, ListenConfig{
require.Nil(t, nil, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -1135,7 +1135,7 @@ func Test_Client_Agent_Struct(t *testing.T) {
})

go func() {
require.Nil(t, nil, app.Listen(ln, ListenConfig{
require.Nil(t, nil, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -1212,7 +1212,7 @@ func testAgent(t *testing.T, handler Handler, wrapAgent func(agent *Agent), exce
app.Get("/", handler)

go func() {
require.Nil(t, nil, app.Listen(ln, ListenConfig{
require.Nil(t, nil, app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down
71 changes: 45 additions & 26 deletions listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ type ListenConfig struct {
OnShutdownSuccess func()
}

// ListenConfigDefault is a function to set default values of ListenConfig.
func ListenConfigDefault(config ...ListenConfig) ListenConfig {
// listenConfigDefault is a function to set default values of ListenConfig.
func listenConfigDefault(config ...ListenConfig) ListenConfig {
if len(config) < 1 {
return ListenConfig{
ListenerNetwork: NetworkTCP4,
Expand Down Expand Up @@ -129,8 +129,8 @@ func ListenConfigDefault(config ...ListenConfig) ListenConfig {
// app.Listen(":8080")
// app.Listen("127.0.0.1:8080")
// app.Listen(":8080", ListenConfig{EnablePrefork: true})
func (app *App) Listen(addr any, config ...ListenConfig) error {
cfg := ListenConfigDefault(config...)
func (app *App) Listen(addr string, config ...ListenConfig) error {
cfg := listenConfigDefault(config...)

// Configure TLS
var tlsConfig *tls.Config = nil
Expand Down Expand Up @@ -173,39 +173,58 @@ func (app *App) Listen(addr any, config ...ListenConfig) error {
go app.gracefulShutdown(ctx, cfg)
}

var ln net.Listener
var err error
// Start prefork
if cfg.EnablePrefork {
return app.prefork(addr, tlsConfig, cfg)
}

switch addr := addr.(type) {
case string:
// Start prefork
if cfg.EnablePrefork {
return app.prefork(addr, tlsConfig, cfg)
}
// Configure Listener
ln, err := app.createListener(addr, tlsConfig, cfg)
if err != nil {
return err
}

// Configure Listener
ln, err = app.createListener(addr, tlsConfig, cfg)
if err != nil {
// prepare the server for the start
app.startupProcess()

// Print startup message & routes
app.printMessages(cfg, ln)

// Serve
if cfg.BeforeServeFunc != nil {
if err := cfg.BeforeServeFunc(app); err != nil {
return err
}
case net.Listener:
// Prefork is supported for custom listeners
if cfg.EnablePrefork {
newAddr, tlsConfig := lnMetadata(cfg.ListenerNetwork, addr)
}

return app.prefork(newAddr, tlsConfig, cfg)
}
return app.server.Serve(ln)
}

// Listener serves HTTP requests from the given listener.
// You should enter custom ListenConfig to customize startup. (prefork, startup message, graceful shutdown...)
func (app *App) Listener(addr net.Listener, config ...ListenConfig) error {
cfg := listenConfigDefault(config...)

// Graceful shutdown
if cfg.GracefulContext != nil {
ctx, cancel := context.WithCancel(cfg.GracefulContext)
defer cancel()

go app.gracefulShutdown(ctx, cfg)
}

ln = addr
default:
panic("start: invalid handler, you must use string or net.Listener as addr type")
// Prefork is supported for custom listeners
if cfg.EnablePrefork {
newAddr, tlsConfig := lnMetadata(cfg.ListenerNetwork, addr)

return app.prefork(newAddr, tlsConfig, cfg)
}

// prepare the server for the start
app.startupProcess()

// Print startup message & routes
app.printMessages(cfg, ln)
app.printMessages(cfg, addr)

// Serve
if cfg.BeforeServeFunc != nil {
Expand All @@ -214,7 +233,7 @@ func (app *App) Listen(addr any, config ...ListenConfig) error {
}
}

return app.server.Serve(ln)
return app.server.Serve(addr)
}

// Create listener function.
Expand Down
20 changes: 10 additions & 10 deletions listen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Test_Listen_Graceful_Shutdown(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond)
defer cancel()

err := app.Listen(ln, ListenConfig{
err := app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
GracefulContext: ctx,
OnShutdownSuccess: func() {
Expand Down Expand Up @@ -203,8 +203,8 @@ func Test_Listen_MutualTLS_Prefork(t *testing.T) {

}

// go test -run Test_Listen_CustomListener
func Test_Listen_CustomListener(t *testing.T) {
// go test -run Test_Listener
func Test_Listener(t *testing.T) {
app := New()

go func() {
Expand All @@ -213,21 +213,21 @@ func Test_Listen_CustomListener(t *testing.T) {
}()

ln := fasthttputil.NewInmemoryListener()
require.Nil(t, app.Listen(ln))
require.Nil(t, app.Listener(ln))
}

// go test -run Test_Listen_CustomListener_Prefork
func Test_Listen_CustomListener_Prefork(t *testing.T) {
// go test -run Test_Listener_Prefork
func Test_Listener_Prefork(t *testing.T) {
testPreforkMaster = true

app := New()

ln := fasthttputil.NewInmemoryListener()
require.Nil(t, app.Listen(ln, ListenConfig{DisableStartupMessage: true, EnablePrefork: true}))
require.Nil(t, app.Listener(ln, ListenConfig{DisableStartupMessage: true, EnablePrefork: true}))
}

// go test -run Test_Listen_CustomTLSListener
func Test_Listen_CustomTLSListener(t *testing.T) {
// go test -run Test_Listener_CustomTLS
func Test_Listener_CustomTLS(t *testing.T) {
// Create tls certificate
cer, err := tls.LoadX509KeyPair("./.github/testdata/ssl.pem", "./.github/testdata/ssl.key")
if err != nil {
Expand All @@ -245,7 +245,7 @@ func Test_Listen_CustomTLSListener(t *testing.T) {
require.Nil(t, app.Shutdown())
}()

require.Nil(t, app.Listen(ln))
require.Nil(t, app.Listener(ln))
}

// go test -run Test_Listen_TLSConfigFunc
Expand Down
8 changes: 4 additions & 4 deletions middleware/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func createProxyTestServer(handler fiber.Handler, t *testing.T) (*fiber.App, str
addr := ln.Addr().String()

go func() {
require.Nil(t, target.Listen(ln, fiber.ListenConfig{
require.Nil(t, target.Listener(ln, fiber.ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -119,7 +119,7 @@ func Test_Proxy_Balancer_WithTlsConfig(t *testing.T) {
}))

go func() {
require.Nil(t, app.Listen(ln, fiber.ListenConfig{
require.Nil(t, app.Listener(ln, fiber.ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -154,7 +154,7 @@ func Test_Proxy_Forward_WithTlsConfig_To_Http(t *testing.T) {
app.Use(Forward("http://" + targetAddr))

go func() {
require.Nil(t, app.Listen(proxyServerLn, fiber.ListenConfig{
require.Nil(t, app.Listener(proxyServerLn, fiber.ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down Expand Up @@ -216,7 +216,7 @@ func Test_Proxy_Forward_WithTlsConfig(t *testing.T) {
app.Use(Forward("https://" + addr + "/tlsfwd"))

go func() {
require.Nil(t, app.Listen(ln, fiber.ListenConfig{
require.Nil(t, app.Listener(ln, fiber.ListenConfig{
DisableStartupMessage: true,
}))
}()
Expand Down
Loading

1 comment on commit db23bd9

@ReneWerner87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: db23bd9 Previous: aef7ea5 Ratio
Benchmark_Cache 12147 ns/op 49369 B/op 6 allocs/op 297.9 ns/op 16 B/op 2 allocs/op 40.78
Benchmark_Cache_AdditionalHeaders 1286 ns/op 592 B/op 9 allocs/op 358.7 ns/op 16 B/op 2 allocs/op 3.59
Benchmark_Limiter 666.1 ns/op 72 B/op 2 allocs/op 275.2 ns/op 8 B/op 1 allocs/op 2.42

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.