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

✨ feature: add callShutdownHooksOnSigInt function #1834

Closed
wants to merge 2 commits into from
Closed

✨ feature: add callShutdownHooksOnSigInt function #1834

wants to merge 2 commits into from

Conversation

Streamer272
Copy link

@Streamer272 Streamer272 commented Mar 22, 2022

  • Adds callShutdownHooksOnSigInt function to App
  • Edits OnShutdownHandler to take *os.Signal argument

When 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 and ListenMutualTLS and calling callShutdownHooksOnSigInt as a goroutine that listens on SIGINT and SIGTERM signals thanks to go's build in os/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.

@efectn
Copy link
Member

efectn commented Mar 22, 2022

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

@welcome
Copy link

welcome bot commented Mar 22, 2022

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

@hi019
Copy link
Contributor

hi019 commented Mar 22, 2022

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

@Streamer272
Copy link
Author

Should I add an option to OnShutdown hook?

@Streamer272
Copy link
Author

I added *os.Signal argument to OnShutdownHandler, so now OnShutdownHandler is func (*os.Signal)

@hi019
Copy link
Contributor

hi019 commented Mar 24, 2022

I'll look at this later today

Copy link
Contributor

@hi019 hi019 left a 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)
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.

// 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

@deniszitu
Copy link

deniszitu commented Jun 23, 2022

I believe graceful shutdown should be implemented by the user...

@efectn
Copy link
Member

efectn commented Sep 8, 2022

Hi @Streamer272. v3 now has more detailed and user-defined automatic graceful shutdown processing with #1930. Thanks for the great idea 🚀
https://github.com/gofiber/fiber/blob/v3-beta/listen.go#L58

@efectn efectn closed this Sep 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants