Skip to content

What is idiomatic way to signal fx app to stop when server errors #848

Answered by abhinav
anivanovic asked this question in Q&A
Discussion options

You must be logged in to vote

Hi there! Typically, you can use the fx.Shutdowner type to signal that the app should be shut down. This interface is provided to your container by default so you can just add it as a parameter to your provied/invoked function.

However, for the case of net/http.Server, note that per the documentation that Sever.ListenAndServe always returns a non-nil error—even if you called Shutdown or Close. So you probably want to ignore ErrServerClosed:

if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed)  {
  shutdowner.Shutdown()
}

Hope this helps!

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@anivanovic
Comment options

Answer selected by anivanovic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants