Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,8 @@ const (
ConstraintDatetime = "datetime"
ConstraintRegex = "regex"
)

// OS identifiers
const (
windowsOS = "windows"
)
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ require (
golang.org/x/text v0.36.0
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/valyala/fasthttp => github.com/ReneWerner87/fasthttp v0.0.0-20260413063825-be2ef67270a3
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/ReneWerner87/fasthttp v0.0.0-20260413063825-be2ef67270a3 h1:Xf2qNxpXT+kMFbqt6ZzNJOQ8n07zGFsteSa9DJgAYKY=
github.com/ReneWerner87/fasthttp v0.0.0-20260413063825-be2ef67270a3/go.mod h1:oDZEHHkJ/Buyklg6uURmYs19442zFSnCIfX3j1FY3pE=
github.com/andybalholm/brotli v1.2.1 h1:R+f5xP285VArJDRgowrfb9DqL18yVK0gKAW/F+eTWro=
github.com/andybalholm/brotli v1.2.1/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -28,8 +30,6 @@ github.com/tinylib/msgp v1.6.3 h1:bCSxiTz386UTgyT1i0MSCvdbWjVW+8sG3PjkGsZQt4s=
github.com/tinylib/msgp v1.6.3/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.70.0 h1:LAhMGcWk13QZWm85+eg8ZBNbrq5mnkWFGbHMUJHIdXA=
github.com/valyala/fasthttp v1.70.0/go.mod h1:oDZEHHkJ/Buyklg6uURmYs19442zFSnCIfX3j1FY3pE=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
Expand Down
28 changes: 15 additions & 13 deletions hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/valyala/bytebufferpool"
"github.com/valyala/fasthttp/prefork"

"github.com/gofiber/fiber/v3/log"
)
Expand Down Expand Up @@ -519,7 +520,8 @@ func Test_ListenData_Hook_HelperFunctions(t *testing.T) {
}

func Test_Hook_OnListenPrefork(t *testing.T) {
t.Parallel()
testPreforkMaster = true

app := New()

buf := bytebufferpool.Get()
Expand All @@ -532,12 +534,12 @@ func Test_Hook_OnListenPrefork(t *testing.T) {
return nil
})

go func() {
time.Sleep(1000 * time.Millisecond)
assert.NoError(t, app.Shutdown())
}()

require.NoError(t, app.Listen(":0", ListenConfig{DisableStartupMessage: true, EnablePrefork: true}))
err := app.Listen(":0", ListenConfig{
DisableStartupMessage: true,
EnablePrefork: true,
PreforkRecoverThreshold: 1,
})
require.ErrorIs(t, err, prefork.ErrOverRecovery)
require.Equal(t, "ready", buf.String())
}

Expand All @@ -548,17 +550,17 @@ func Test_Hook_OnHook(t *testing.T) {
testPreforkMaster = true
testOnPrefork = true

go func() {
time.Sleep(1000 * time.Millisecond)
assert.NoError(t, app.Shutdown())
}()

app.Hooks().OnFork(func(pid int) error {
require.Equal(t, 1, pid)
return nil
})

require.NoError(t, app.prefork(":0", nil, &ListenConfig{DisableStartupMessage: true, EnablePrefork: true}))
err := app.prefork(":0", nil, &ListenConfig{
DisableStartupMessage: true,
EnablePrefork: true,
PreforkRecoverThreshold: 1,
})
require.ErrorIs(t, err, prefork.ErrOverRecovery)
}

func Test_Hook_OnMount(t *testing.T) {
Expand Down
13 changes: 13 additions & 0 deletions listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
)

// ListenConfig is a struct to customize startup of Fiber.
type ListenConfig struct {

Check failure on line 44 in listen.go

View workflow job for this annotation

GitHub Actions / lint

fieldalignment: struct with 160 pointer bytes could be 128 (govet)
// GracefulContext is a field to shutdown Fiber by given context gracefully.
//
// Default: nil
Expand Down Expand Up @@ -126,6 +126,19 @@
// Default: false
EnablePrefork bool `json:"enable_prefork"`

// PreforkRecoverThreshold defines the maximum number of times a child process
// can be restarted after crashing before the master process exits with an error.
// This only applies when EnablePrefork is true.
//
// Default: runtime.GOMAXPROCS(0) / 2
PreforkRecoverThreshold int `json:"prefork_recover_threshold"`

// PreforkLogger sets a custom logger for the prefork process manager.
// This only applies when EnablePrefork is true.
//
// Default: Fiber's built-in logger (log.Infof)
PreforkLogger PreforkLoggerInterface `json:"prefork_logger"`

// If set to true, will print all routes with their method, path and handler.
//
// Default: false
Expand Down
60 changes: 31 additions & 29 deletions listen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttputil"
"github.com/valyala/fasthttp/prefork"
"golang.org/x/crypto/acme/autocert"
)

Expand Down Expand Up @@ -169,7 +170,12 @@ func Test_Listen_Prefork(t *testing.T) {

app := New()

require.NoError(t, app.Listen(":0", ListenConfig{DisableStartupMessage: true, EnablePrefork: true}))
err := app.Listen(":0", ListenConfig{
DisableStartupMessage: true,
EnablePrefork: true,
PreforkRecoverThreshold: 1,
})
require.ErrorIs(t, err, prefork.ErrOverRecovery)
}

// go test -run Test_Listen_TLSMinVersion
Expand Down Expand Up @@ -202,11 +208,13 @@ func Test_Listen_TLSMinVersion(t *testing.T) {
require.NoError(t, app.Listen(":0", ListenConfig{TLSMinVersion: tls.VersionTLS13}))

// Valid TLSMinVersion with Prefork
go func() {
time.Sleep(1000 * time.Millisecond)
assert.NoError(t, app.Shutdown())
}()
require.NoError(t, app.Listen(":0", ListenConfig{DisableStartupMessage: true, EnablePrefork: true, TLSMinVersion: tls.VersionTLS13}))
err := app.Listen(":0", ListenConfig{
DisableStartupMessage: true,
EnablePrefork: true,
TLSMinVersion: tls.VersionTLS13,
PreforkRecoverThreshold: 1,
})
require.ErrorIs(t, err, prefork.ErrOverRecovery)
}

// go test -run Test_Listen_TLS
Expand Down Expand Up @@ -244,17 +252,14 @@ func Test_Listen_TLS_Prefork(t *testing.T) {
CertKeyFile: "./.github/testdata/template.tmpl",
}))

go func() {
time.Sleep(1000 * time.Millisecond)
assert.NoError(t, app.Shutdown())
}()

require.NoError(t, app.Listen(":0", ListenConfig{
DisableStartupMessage: true,
EnablePrefork: true,
CertFile: "./.github/testdata/ssl.pem",
CertKeyFile: "./.github/testdata/ssl.key",
}))
tlsErr := app.Listen(":0", ListenConfig{
DisableStartupMessage: true,
EnablePrefork: true,
CertFile: "./.github/testdata/ssl.pem",
CertKeyFile: "./.github/testdata/ssl.key",
PreforkRecoverThreshold: 1,
})
require.ErrorIs(t, tlsErr, prefork.ErrOverRecovery)
}

// go test -run Test_Listen_MutualTLS
Expand Down Expand Up @@ -295,18 +300,15 @@ func Test_Listen_MutualTLS_Prefork(t *testing.T) {
CertClientFile: "./.github/testdata/ca-chain.cert.pem",
}))

go func() {
time.Sleep(1000 * time.Millisecond)
assert.NoError(t, app.Shutdown())
}()

require.NoError(t, app.Listen(":0", ListenConfig{
DisableStartupMessage: true,
EnablePrefork: true,
CertFile: "./.github/testdata/ssl.pem",
CertKeyFile: "./.github/testdata/ssl.key",
CertClientFile: "./.github/testdata/ca-chain.cert.pem",
}))
mtlsErr := app.Listen(":0", ListenConfig{
DisableStartupMessage: true,
EnablePrefork: true,
CertFile: "./.github/testdata/ssl.pem",
CertKeyFile: "./.github/testdata/ssl.key",
CertClientFile: "./.github/testdata/ca-chain.cert.pem",
PreforkRecoverThreshold: 1,
})
require.ErrorIs(t, mtlsErr, prefork.ErrOverRecovery)
}

// go test -run Test_Listener
Expand Down
Loading
Loading