Skip to content

Commit

Permalink
✨ v3 (feature): merge Listen methods & ListenConfig (#1930)
Browse files Browse the repository at this point in the history
* ✨ v3: new Start method for app

* ✨ v3: new Start method for app

* ✨ v3: new Start method for app

* ✨ v3: new Start method for app

* ✨ v3: new Start method for app

* ✨ v3: new Start method for app

* fix tests

* improve graceful shutdown

* update

* Start -> Listen

* rename test funcs.

* Add Test_Listen_Graceful_Shutdown test.

* add OnShutdownSuccess

* fix tests

* fix tests

* split listen & listener

* typo

* Add retry logic to tests

* Add retry logic to tests

* Add retry logic to tests

* Add retry logic to tests

Co-authored-by: René Werner <[email protected]>
  • Loading branch information
efectn and ReneWerner87 authored Sep 8, 2022
1 parent ca0f663 commit 281e2f0
Show file tree
Hide file tree
Showing 12 changed files with 714 additions and 336 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ jobs:
- name: Fetch Repository
uses: actions/checkout@v3
- name: Run Test
run: go test ./... -v -race
uses: nick-fields/retry@v2
with:
max_attempts: 3
timeout_minutes: 15
command: go test ./... -v -race
26 changes: 2 additions & 24 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,6 @@ type App struct {

// Config is a struct holding the server settings.
type Config struct {
// When set to true, this will spawn multiple Go processes listening on the same port.
//
// Default: false
Prefork bool `json:"prefork"`

// Enables the "Server: value" HTTP header.
//
// Default: ""
Expand Down Expand Up @@ -270,11 +265,6 @@ type Config struct {
// Default: false
DisableHeaderNormalizing bool `json:"disable_header_normalizing"`

// When set to true, it will not print out the «Fiber» ASCII art and listening address.
//
// Default: false
DisableStartupMessage bool `json:"disable_startup_message"`

// This function allows to setup app name for the app
//
// Default: nil
Expand Down Expand Up @@ -332,12 +322,6 @@ type Config struct {
// Default: xml.Marshal
XMLEncoder utils.XMLMarshal `json:"-"`

// Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only)
// WARNING: When prefork is set to true, only "tcp4" and "tcp6" can be chose.
//
// Default: NetworkTCP4
Network string

// If you find yourself behind some sort of proxy, like a load balancer,
// then certain header information may be sent to you using special X-Forwarded-* headers or the Forwarded header.
// For example, the Host HTTP header is usually used to return the requested host.
Expand Down Expand Up @@ -374,10 +358,6 @@ type Config struct {
// Default: false
EnableIPValidation bool `json:"enable_ip_validation"`

// If set to true, will print all routes with their method, path and handler.
// Default: false
EnablePrintRoutes bool `json:"enable_print_routes"`

// You can define custom color scheme. They'll be used for startup message, route list and some middlewares.
//
// Optional. Default: DefaultColors
Expand Down Expand Up @@ -533,9 +513,6 @@ func New(config ...Config) *App {
if app.config.XMLEncoder == nil {
app.config.XMLEncoder = xml.Marshal
}
if app.config.Network == "" {
app.config.Network = NetworkTCP4
}

app.config.trustedProxiesMap = make(map[string]struct{}, len(app.config.TrustedProxies))
for _, ipAddress := range app.config.TrustedProxies {
Expand Down Expand Up @@ -831,7 +808,8 @@ func (app *App) HandlersCount() uint32 {
// Shutdown does not close keepalive connections so its recommended to set ReadTimeout to something else than 0.
func (app *App) Shutdown() error {
if app.hooks != nil {
defer app.hooks.executeOnShutdownHooks()
// TODO: check should be defered?
app.hooks.executeOnShutdownHooks()
}

app.mutex.Lock()
Expand Down
31 changes: 11 additions & 20 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,16 +599,14 @@ func Test_App_New(t *testing.T) {

func Test_App_Config(t *testing.T) {
app := New(Config{
DisableStartupMessage: true,
StrictRouting: true,
})
require.True(t, app.Config().DisableStartupMessage)
require.True(t, app.Config().StrictRouting)
}

func Test_App_Shutdown(t *testing.T) {
t.Run("success", func(t *testing.T) {
app := New(Config{
DisableStartupMessage: true,
})
app := New()
require.True(t, app.Shutdown() == nil)
})

Expand Down Expand Up @@ -1098,7 +1096,6 @@ func Test_App_Deep_Group(t *testing.T) {
// go test -run Test_App_Next_Method
func Test_App_Next_Method(t *testing.T) {
app := New()
app.config.DisableStartupMessage = true

app.Use(func(c Ctx) error {
require.Equal(t, MethodGet, c.Method())
Expand Down Expand Up @@ -1140,7 +1137,6 @@ func Test_NewError(t *testing.T) {
// go test -run Test_Test_Timeout
func Test_Test_Timeout(t *testing.T) {
app := New()
app.config.DisableStartupMessage = true

app.Get("/", testEmptyHandler)

Expand All @@ -1166,7 +1162,6 @@ func (errorReader) Read([]byte) (int, error) {
// go test -run Test_Test_DumpError
func Test_Test_DumpError(t *testing.T) {
app := New()
app.config.DisableStartupMessage = true

app.Get("/", testEmptyHandler)

Expand Down Expand Up @@ -1236,10 +1231,9 @@ func Test_App_HandlersCount(t *testing.T) {
// go test -run Test_App_ReadTimeout
func Test_App_ReadTimeout(t *testing.T) {
app := New(Config{
ReadTimeout: time.Nanosecond,
IdleTimeout: time.Minute,
DisableStartupMessage: true,
DisableKeepalive: true,
ReadTimeout: time.Nanosecond,
IdleTimeout: time.Minute,
DisableKeepalive: true,
})

app.Get("/read-timeout", func(c Ctx) error {
Expand All @@ -1266,14 +1260,12 @@ func Test_App_ReadTimeout(t *testing.T) {
require.Nil(t, app.Shutdown())
}()

require.Nil(t, app.Listen(":4004"))
require.Nil(t, app.Listen(":4004", ListenConfig{DisableStartupMessage: true}))
}

// go test -run Test_App_BadRequest
func Test_App_BadRequest(t *testing.T) {
app := New(Config{
DisableStartupMessage: true,
})
app := New()

app.Get("/bad-request", func(c Ctx) error {
return c.SendString("I should not be sent")
Expand All @@ -1298,14 +1290,13 @@ func Test_App_BadRequest(t *testing.T) {
require.Nil(t, app.Shutdown())
}()

require.Nil(t, app.Listen(":4005"))
require.Nil(t, app.Listen(":4005", ListenConfig{DisableStartupMessage: true}))
}

// go test -run Test_App_SmallReadBuffer
func Test_App_SmallReadBuffer(t *testing.T) {
app := New(Config{
ReadBufferSize: 1,
DisableStartupMessage: true,
ReadBufferSize: 1,
})

app.Get("/small-read-buffer", func(c Ctx) error {
Expand All @@ -1322,7 +1313,7 @@ func Test_App_SmallReadBuffer(t *testing.T) {
require.Nil(t, app.Shutdown())
}()

require.Nil(t, app.Listen(":4006"))
require.Nil(t, app.Listen(":4006", ListenConfig{DisableStartupMessage: true}))
}

func Test_App_Server(t *testing.T) {
Expand Down
Loading

1 comment on commit 281e2f0

@ReneWerner87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 281e2f0 Previous: 012a2b1 Ratio
Benchmark_AcquireCtx 1911 ns/op 1568 B/op 5 allocs/op 613.8 ns/op 1568 B/op 5 allocs/op 3.11
Benchmark_Cache 13864 ns/op 49370 B/op 6 allocs/op 303.4 ns/op 16 B/op 2 allocs/op 45.70
Benchmark_Cache_AdditionalHeaders 1202 ns/op 592 B/op 9 allocs/op 409.6 ns/op 16 B/op 2 allocs/op 2.93
Benchmark_Limiter 668.3 ns/op 72 B/op 2 allocs/op 316 ns/op 8 B/op 1 allocs/op 2.11

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.