-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
✨ feature: add callShutdownHooksOnSigInt function #1834
Conversation
Do we need that? Some users may not want to use SIGTERM with app.Shutdown. I think it should be optional, we can do this in the app. What do you think? @hi019 @ReneWerner87 |
Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
I think this is a good addition. It allows for graceful shutdown by default -- could be useful for things like closing DB connections or removing temporary files, which would need to be done whether the app is killed or shutsdown regularly. Uber's Fx does the same thing: https://pkg.go.dev/go.uber.org/fx#App.Run |
Should I add an option to OnShutdown hook? |
I added |
I'll look at this later today |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. What about putting the signal code in app.startupProcess
? Just to reduce duplication
// Handlers define a function to create hooks for Fiber. | ||
type OnRouteHandler = func(Route) error | ||
type OnNameHandler = OnRouteHandler | ||
type OnGroupHandler = func(Group) error | ||
type OnGroupNameHandler = OnGroupHandler | ||
type OnListenHandler = func() error | ||
type OnShutdownHandler = OnListenHandler | ||
type OnShutdownHandler = func(*os.Signal) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
// prepare the server for the start | ||
app.startupProcess() | ||
|
||
sigChan := make(chan os.Signal) | ||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) |
There was a problem hiding this comment.
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
I believe graceful shutdown should be implemented by the user... |
Hi @Streamer272. v3 now has more detailed and user-defined automatic graceful shutdown processing with #1930. Thanks for the great idea 🚀 |
callShutdownHooksOnSigInt
function to AppOnShutdownHandler
to take*os.Signal
argumentWhen fiber app is killed by SIGINT or SIGTERM, fiber doesn't call shutdown hooks when should.
This pull request solves it by adding a channel to
Listen
,ListenTLS
andListenMutualTLS
and callingcallShutdownHooksOnSigInt
as a goroutine that listens on SIGINT and SIGTERM signals thanks to go's build inos/signal
.After calling the shutdown hooks, function exists the program with
os.Exit(0)
.When calling all the on shutdown handlers, they might now want to execute when a specific signel is used to kill the app, so now they take
*os.Signal
as argument and can implement their own logic.