🐛 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 mode
pull/2505/head
M. Efe Çetin 2023-06-09 23:42:50 +03:00 committed by GitHub
parent 9effdf829a
commit d4938dad6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 4 deletions

11
app.go
View File

@ -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.
func (app *App) startupProcess() *App {
if err := app.hooks.executeOnListenHooks(); err != nil {
panic(err)
}
app.mutex.Lock()
defer app.mutex.Unlock()
@ -1106,3 +1102,10 @@ func (app *App) startupProcess() *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)
}
}

View File

@ -224,6 +224,32 @@ func Test_Hook_OnListen(t *testing.T) {
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) {
app := New()

View File

@ -30,6 +30,9 @@ func (app *App) Listener(ln net.Listener) error {
// prepare the server for the start
app.startupProcess()
// run hooks
app.runOnListenHooks()
// Print startup message
if !app.config.DisableStartupMessage {
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
app.startupProcess()
// run hooks
app.runOnListenHooks()
// Print startup message
if !app.config.DisableStartupMessage {
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
app.startupProcess()
// run hooks
app.runOnListenHooks()
// Print startup message
if !app.config.DisableStartupMessage {
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
app.startupProcess()
// run hooks
app.runOnListenHooks()
// Print startup message
if !app.config.DisableStartupMessage {
app.startupMessage(ln.Addr().String(), true, "")

View File

@ -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
if !app.config.DisableStartupMessage {
app.startupMessage(addr, tlsConfig != nil, ","+strings.Join(pids, ","))