8000 ✨ feature: add callShutdownHooksOnSigInt function by Streamer272 · Pull Request #1834 · gofiber/fiber · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

✨ feature: add callShutdownHooksOnSigInt function #1834

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

Closed
wants to merge 2 commits into from
Closed
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
35 changes: 33 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 8000 (
"net/http"
"net/http/httputil"
"os"
"os/signal"
"path/filepath"
"reflect"
"runtime"
Expand All @@ -26,6 +27,7 @@ import (
"strings"
"sync"
"sync/atomic"
"syscall"
"text/tabwriter"
"time"

Expand Down Expand Up @@ -780,21 +782,30 @@ func (app *App) Listen(addr string) error {
if app.config.Prefork {
return app.prefork(app.config.Network, addr, nil)
}

// Setup listener
ln, err := net.Listen(app.config.Network, addr)
if err != nil {
return err
}

// prepare the server for the start
app.startupProcess()

sigChan := make(chan os.Signal)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
Copy link
Member

Choose a reason for hiding this comment

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

I believe that we need to give autonomy to the people to treat signals according to their needs it.
Maybe we can create a method to explicit this signals default.
I created a package treat signals Golang maybe it can help you.
https://github.com/renanbastos93/ossignals

go app.callShutdownHooksOnSigInt(sigChan)

// Print startup message
if !app.config.DisableStartupMessage {
app.startupMessage(ln.Addr().String(), false, "")
}

// Print routes
if app.config.EnablePrintRoutes {
app.printRoutesMessage()
}

// Start listening
return app.server.Serve(ln)
}
Expand All @@ -809,6 +820,7 @@ func (app *App) ListenTLS(addr, certFile, keyFile string) error {
if len(certFile) == 0 || len(keyFile) == 0 {
return errors.New("tls: provide a valid cert or key path")
}

// Prefork is supported
if app.config.Prefork {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
Expand All @@ -823,21 +835,30 @@ func (app *App) ListenTLS(addr, certFile, keyFile string) error {
}
return app.prefork(app.config.Network, addr, config)
}

// Setup listener
ln, err := net.Listen(app.config.Network, addr)
if err != nil {
return err
}

// prepare the server for the start
app.startupProcess()

sigChan := make(chan os.Signal)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go app.callShutdownHooksOnSigInt(sigChan)

// Print startup message
if !app.config.DisableStartupMessage {
app.startupMessage(ln.Addr().String(), true, "")
}

// Print routes
if app.config.EnablePrintRoutes {
app.printRoutesMessage()
}

// Start listening
return app.server.ServeTLS(ln, certFile, keyFile)
}
Expand Down Expand Up @@ -888,6 +909,10 @@ func (app *App) ListenMutualTLS(addr, certFile, keyFile, clientCertFile string)
// prepare the server for the start
app.startupProcess()

sigChan := make(chan os.Signal)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go app.callShutdownHooksOnSigInt(sigChan)

// Print startup message
if !app.config.DisableStartupMessage {
app.startupMessage(ln.Addr().String(), true, "")
Expand Down Expand Up @@ -929,10 +954,10 @@ func (app *App) HandlersCount() uint32 {
//
// 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.
// Shutdown does not close keepalive connections, so it's recommended to set ReadTimeout to something else than 0.
func (app *App) Shutdown() error {
if app.hooks != nil {
defer app.hooks.executeOnShutdownHooks()
defer app.hooks.executeOnShutdownHooks(nil)
}

app.mutex.Lock()
Expand Down Expand Up 8000 @@ -1387,3 +1412,9 @@ func (app *App) printRoutesMessage() {

_ = w.Flush()
}

func (app *App) callShutdownHooksOnSigInt(sigChan chan os.Signal) {
sig := <-sigChan
app.hooks.executeOnShutdownHooks(&sig)
os.Exit(0)
}
11 changes: 6 additions & 5 deletions hooks.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package fiber

import "os"

// Handlers define a function to create hooks for Fiber.
type error
type >
type error
type >
type error
type class="x x-first x-last">OnListenHandler
type class="x x-first x-last">func(*os.Signal)
Copy link
Contributor

Choose a reason for hiding this comment

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

This would break backwards compatibility :(. Honestly I think not showing os.Signal is fine for now. Are there any situations where a hook would need to change its functionality based on the signal type?

Copy link
Member
@efectn efectn Apr 1, 2022

Choose a reason for hiding this comment

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

I think we can add DisableDefaultGraceful config instead. Otherwise, i dont think hardcoded things are good. What about? @ReneWerner87 @hi019 @Streamer272

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, works for me. What's hardcoded, though?

Copy link
Member

Choose a reason for hiding this comment

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

Sure, works for me. What's hardcoded, though?

Some users may want to add their custom signals.


type hooks struct {
// Embed app
Expand Down Expand Up @@ -76,7 +78,7 @@ func (h *hooks) OnListen(handler ...OnListenHandler) {
h.app.mutex.Unlock()
}

// OnShutdown is a hook to execute user functions after Shutdown.
// OnShutdown is a hook to execute user functions before Shutdown.
func (h *hooks) OnShutdown(handler ...OnShutdownHandler) {
h.app.mutex.Lock()
h. handler...)
Expand All @@ -94,7 +96,6 @@ func (h *hooks) executeOnRouteHooks(route Route) error {
}

func (h *hooks) executeOnNameHooks(route Route) error {

for _, v := range h.onName {
if err := v(route); err != nil {
return err
Expand Down Expand Up @@ -134,8 +135,8 @@ func (h *hooks) executeOnListenHooks() error {
return nil
}

func (h *hooks) executeOnShutdownHooks() {
func (h *hooks) executeOnShutdownHooks(signal *os.Signal) {
for _, v := range h.onShutdown {
_ = v()
v(signal)
}
}
0