Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ShutdownWithTimeout function #2228

Merged
merged 6 commits into from Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions app.go
Expand Up @@ -9,6 +9,7 @@ package fiber

import (
"bufio"
"context"
"encoding/json"
"encoding/xml"
"errors"
Expand Down Expand Up @@ -839,12 +840,30 @@ func (app *App) HandlersCount() uint32 {
}

// Shutdown gracefully shuts down the server without interrupting any active connections.
// Shutdown works by first closing all open listeners and then waiting indefinitely for all connections to return to idle and then shut down.
// Shutdown works by first closing all open listeners and then waiting indefinitely for all connections to return to idle before shutting down.
efectn marked this conversation as resolved.
Show resolved Hide resolved
//
// Make sure the program doesn't exit and waits instead for Shutdown to return.
//
// Shutdown does not close keepalive connections so its recommended to set ReadTimeout to something else than 0.
func (app *App) Shutdown() error {
return app.shutdownWithContext(context.Background())
}

// ShutdownWithTimeout gracefully shuts down the server without interrupting any active connections. However, if the timeout is exceeded,
efectn marked this conversation as resolved.
Show resolved Hide resolved
// ShutdownWithTimeout will forcefully close any active connections.
// ShutdownWithTimeout works by first closing all open listeners and then waiting for all connections to return to idle before shutting down.
//
// Make sure the program doesn't exit and waits instead for ShutdownWithTimeout to return.
//
// ShutdownWithTimeout does not close keepalive connections so its recommended to set ReadTimeout to something else than 0.
func (app *App) ShutdownWithTimeout(timeout time.Duration) error {
efectn marked this conversation as resolved.
Show resolved Hide resolved
li-jin-gou marked this conversation as resolved.
Show resolved Hide resolved
efectn marked this conversation as resolved.
Show resolved Hide resolved
ctx, cancelFunc := context.WithTimeout(context.Background(), timeout)
defer cancelFunc()
return app.shutdownWithContext(ctx)
}

// shutdownWithContext shutsdown the server including by force if the context's deadline is exceeded.
func (app *App) shutdownWithContext(ctx context.Context) error {
efectn marked this conversation as resolved.
Show resolved Hide resolved
if app.hooks != nil {
defer app.hooks.executeOnShutdownHooks()
}
Expand All @@ -854,7 +873,7 @@ func (app *App) Shutdown() error {
if app.server == nil {
return fmt.Errorf("shutdown: server is not running")
}
return app.server.Shutdown()
return app.server.ShutdownWithContext(ctx)
}

// Server returns the underlying fasthttp server
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -6,8 +6,8 @@ require (
github.com/mattn/go-colorable v0.1.13
github.com/mattn/go-isatty v0.0.16
github.com/mattn/go-runewidth v0.0.14
github.com/valyala/fasthttp v1.42.0
github.com/valyala/bytebufferpool v1.0.0
github.com/valyala/fasthttp v1.41.0
pjebs marked this conversation as resolved.
Show resolved Hide resolved
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab
)

Expand Down