Skip to content

Commit

Permalink
🐛 bug: fix onListen hooks when they are used with prefork mode (#2504)
Browse files Browse the repository at this point in the history
* 🐛 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
  • Loading branch information
efectn committed Jun 9, 2023
1 parent 9effdf8 commit d4938da
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
11 changes: 7 additions & 4 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
}
}
26 changes: 26 additions & 0 deletions hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
12 changes: 12 additions & 0 deletions listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
Expand Down Expand Up @@ -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, "")
Expand Down Expand Up @@ -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, "")
Expand Down Expand Up @@ -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, "")
Expand Down
4 changes: 4 additions & 0 deletions prefork.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ","))
Expand Down

0 comments on commit d4938da

Please sign in to comment.