-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
fix(server/v2): avoid server stop get call before start for multi components #22811
Changes from 4 commits
74d3a61
357ccfb
c85b843
70751b6
59bfdd9
9db84ba
b69e5cd
dbe76eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -46,7 +46,10 @@ func New[T transaction.Tx]( | |||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
srv.config = serverCfg | ||||||||||||||||
|
||||||||||||||||
srv.httpServer = &http.Server{ | ||||||||||||||||
Addr: srv.config.Address, | ||||||||||||||||
Handler: srv.router, | ||||||||||||||||
} | ||||||||||||||||
return srv, nil | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
|
@@ -69,11 +72,6 @@ func (s *Server[T]) Start(ctx context.Context) error { | |||||||||||||||
return nil | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
s.httpServer = &http.Server{ | ||||||||||||||||
Addr: s.config.Address, | ||||||||||||||||
Handler: s.router, | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
s.logger.Info("starting HTTP server", "address", s.config.Address) | ||||||||||||||||
if err := s.httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { | ||||||||||||||||
s.logger.Error("failed to start HTTP server", "error", err) | ||||||||||||||||
|
@@ -89,6 +87,12 @@ func (s *Server[T]) Stop(ctx context.Context) error { | |||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
s.logger.Info("stopping HTTP server") | ||||||||||||||||
defer func() { | ||||||||||||||||
s.httpServer = &http.Server{ | ||||||||||||||||
Addr: s.config.Address, | ||||||||||||||||
Handler: s.router, | ||||||||||||||||
} | ||||||||||||||||
}() | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reconsider server reinitialization in Stop() The deferred reinitialization of the httpServer after shutdown could be problematic:
Consider removing the deferred reinitialization: func (s *Server[T]) Stop(ctx context.Context) error {
if !s.config.Enable {
return nil
}
s.logger.Info("stopping HTTP server")
- defer func() {
- s.httpServer = &http.Server{
- Addr: s.config.Address,
- Handler: s.router,
- }
- }()
return s.httpServer.Shutdown(ctx)
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||
return s.httpServer.Shutdown(ctx) | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
|
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.
If
httpServer
is set inNew
how come we need this?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.
If we call
Start
again later afterStop
, the old httpServer will fail since Shutdown was called.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 see, but why would you do that? What is the use case?
I think we should just add in the server component docs, like in the std http server docs something like:
Once Stop has been called on a server, it may not be reused
.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.
Before we wanted to support server reloading, so then it would have made sense, but due to the config reloading limitation, we haven't gone that way. I think it makes server components simpler to write if you know once you call stop you don't need to make it re-startable. So setting the server in New instead of Start does makes sense, but imho, we don't need to change anything else in Stop.