Skip to content

Commit

Permalink
🐛 bug: fix onListen hooks when they are used with prefork mode
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Jun 9, 2023
1 parent 9effdf8 commit 81a4f1e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1092,8 +1092,10 @@ 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)
if !app.config.Prefork {
if err := app.hooks.executeOnListenHooks(); err != nil {
panic(err)
}
}

app.mutex.Lock()
Expand Down
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
6 changes: 6 additions & 0 deletions prefork.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ 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 don't work inside of prefork method
if err := app.hooks.executeOnListenHooks(); err != nil {
panic(err)
}

// Print startup message
if !app.config.DisableStartupMessage {
app.startupMessage(addr, tlsConfig != nil, ","+strings.Join(pids, ","))
Expand Down

0 comments on commit 81a4f1e

Please sign in to comment.