mirror of https://github.com/gofiber/fiber.git
🐛 bug: fix onListen hooks when they are used with prefork mode (#2504)
* 🐛 bug: fix onListen hooks when they are used with prefork mode 🐛 bug: fix onListen hooks when they are used with prefork mode * 🐛 bug: fix onListen hooks when they are used with prefork modepull/2505/head
parent
9effdf829a
commit
d4938dad6c
11
app.go
11
app.go
|
@ -1092,10 +1092,6 @@ func (app *App) serverErrorHandler(fctx *fasthttp.RequestCtx, err error) {
|
||||||
|
|
||||||
// startupProcess Is the method which executes all the necessary processes just before the start of the server.
|
// startupProcess Is the method which executes all the necessary processes just before the start of the server.
|
||||||
func (app *App) startupProcess() *App {
|
func (app *App) startupProcess() *App {
|
||||||
if err := app.hooks.executeOnListenHooks(); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
app.mutex.Lock()
|
app.mutex.Lock()
|
||||||
defer app.mutex.Unlock()
|
defer app.mutex.Unlock()
|
||||||
|
|
||||||
|
@ -1106,3 +1102,10 @@ func (app *App) startupProcess() *App {
|
||||||
|
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run onListen hooks. If they return an error, panic.
|
||||||
|
func (app *App) runOnListenHooks() {
|
||||||
|
if err := app.hooks.executeOnListenHooks(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -224,6 +224,32 @@ func Test_Hook_OnListen(t *testing.T) {
|
||||||
utils.AssertEqual(t, "ready", buf.String())
|
utils.AssertEqual(t, "ready", buf.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Hook_OnListenPrefork(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
app := New(Config{
|
||||||
|
DisableStartupMessage: true,
|
||||||
|
Prefork: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
buf := bytebufferpool.Get()
|
||||||
|
defer bytebufferpool.Put(buf)
|
||||||
|
|
||||||
|
app.Hooks().OnListen(func() error {
|
||||||
|
_, err := buf.WriteString("ready")
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
time.Sleep(1000 * time.Millisecond)
|
||||||
|
utils.AssertEqual(t, nil, app.Shutdown())
|
||||||
|
}()
|
||||||
|
utils.AssertEqual(t, nil, app.Listen(":9000"))
|
||||||
|
|
||||||
|
utils.AssertEqual(t, "ready", buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
func Test_Hook_OnHook(t *testing.T) {
|
func Test_Hook_OnHook(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
|
||||||
|
|
12
listen.go
12
listen.go
|
@ -30,6 +30,9 @@ func (app *App) Listener(ln net.Listener) error {
|
||||||
// prepare the server for the start
|
// prepare the server for the start
|
||||||
app.startupProcess()
|
app.startupProcess()
|
||||||
|
|
||||||
|
// run hooks
|
||||||
|
app.runOnListenHooks()
|
||||||
|
|
||||||
// Print startup message
|
// Print startup message
|
||||||
if !app.config.DisableStartupMessage {
|
if !app.config.DisableStartupMessage {
|
||||||
app.startupMessage(ln.Addr().String(), getTLSConfig(ln) != nil, "")
|
app.startupMessage(ln.Addr().String(), getTLSConfig(ln) != nil, "")
|
||||||
|
@ -68,6 +71,9 @@ func (app *App) Listen(addr string) error {
|
||||||
// prepare the server for the start
|
// prepare the server for the start
|
||||||
app.startupProcess()
|
app.startupProcess()
|
||||||
|
|
||||||
|
// run hooks
|
||||||
|
app.runOnListenHooks()
|
||||||
|
|
||||||
// Print startup message
|
// Print startup message
|
||||||
if !app.config.DisableStartupMessage {
|
if !app.config.DisableStartupMessage {
|
||||||
app.startupMessage(ln.Addr().String(), false, "")
|
app.startupMessage(ln.Addr().String(), false, "")
|
||||||
|
@ -130,6 +136,9 @@ func (app *App) ListenTLSWithCertificate(addr string, cert tls.Certificate) erro
|
||||||
// prepare the server for the start
|
// prepare the server for the start
|
||||||
app.startupProcess()
|
app.startupProcess()
|
||||||
|
|
||||||
|
// run hooks
|
||||||
|
app.runOnListenHooks()
|
||||||
|
|
||||||
// Print startup message
|
// Print startup message
|
||||||
if !app.config.DisableStartupMessage {
|
if !app.config.DisableStartupMessage {
|
||||||
app.startupMessage(ln.Addr().String(), true, "")
|
app.startupMessage(ln.Addr().String(), true, "")
|
||||||
|
@ -202,6 +211,9 @@ func (app *App) ListenMutualTLSWithCertificate(addr string, cert tls.Certificate
|
||||||
// prepare the server for the start
|
// prepare the server for the start
|
||||||
app.startupProcess()
|
app.startupProcess()
|
||||||
|
|
||||||
|
// run hooks
|
||||||
|
app.runOnListenHooks()
|
||||||
|
|
||||||
// Print startup message
|
// Print startup message
|
||||||
if !app.config.DisableStartupMessage {
|
if !app.config.DisableStartupMessage {
|
||||||
app.startupMessage(ln.Addr().String(), true, "")
|
app.startupMessage(ln.Addr().String(), true, "")
|
||||||
|
|
|
@ -126,6 +126,10 @@ func (app *App) prefork(network, addr string, tlsConfig *tls.Config) error {
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run onListen hooks
|
||||||
|
// Hooks have to be run here as different as non-prefork mode due to they should run as child or master
|
||||||
|
app.runOnListenHooks()
|
||||||
|
|
||||||
// Print startup message
|
// Print startup message
|
||||||
if !app.config.DisableStartupMessage {
|
if !app.config.DisableStartupMessage {
|
||||||
app.startupMessage(addr, tlsConfig != nil, ","+strings.Join(pids, ","))
|
app.startupMessage(addr, tlsConfig != nil, ","+strings.Join(pids, ","))
|
||||||
|
|
Loading…
Reference in New Issue