-
Notifications
You must be signed in to change notification settings - Fork 436
fix(cube-master): panic if http server fails to start #127
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
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 |
|---|---|---|
|
|
@@ -8,6 +8,8 @@ package server | |
| import ( | ||
| "context" | ||
| "fmt" | ||
| stdlog "log" | ||
| "net" | ||
| "net/http" | ||
| "os" | ||
| "sync" | ||
|
|
@@ -23,18 +25,19 @@ import ( | |
| metahttp "github.com/tencentcloud/CubeSandbox/CubeMaster/pkg/service/httpservice/meta" | ||
| "github.com/tencentcloud/CubeSandbox/CubeMaster/pkg/service/httpservice/middleware" | ||
| "github.com/tencentcloud/CubeSandbox/CubeMaster/pkg/service/httpservice/notify" | ||
| "github.com/tencentcloud/CubeSandbox/cubelog" | ||
| CubeLog "github.com/tencentcloud/CubeSandbox/cubelog" | ||
| ) | ||
|
|
||
| type Server struct { | ||
| InternalHttpServer *internalHttp | ||
| ErrChan chan error | ||
| } | ||
|
|
||
| func New(ctx context.Context, cfg *config.Config) (*Server, error) { | ||
| if cfg == nil || cfg.Common == nil { | ||
| return nil, errors.New("config is nil") | ||
| } | ||
| s := &Server{} | ||
| s := &Server{ErrChan: make(chan error, 1)} | ||
| var err error | ||
| s.InternalHttpServer, err = NewInternalHttp(ctx, cfg) | ||
| if err != nil { | ||
|
|
@@ -111,20 +114,34 @@ func (s *internalHttp) registerHandlers() { | |
| } | ||
|
|
||
| func (s *internalHttp) Start() error { | ||
| if err := s.ListenAndServe(); err != nil { | ||
| if err == http.ErrServerClosed { | ||
| return nil | ||
| } | ||
| return errors.WithStack(err) | ||
| addr := s.Addr | ||
| if addr == "" { | ||
| addr = ":http" | ||
| } | ||
|
|
||
| ln, err := net.Listen("tcp", addr) | ||
|
Collaborator
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. If the real issue is that the requested port is in use, we can simply panic here.
Author
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. It seems that a panic only terminates the program, while a non-panic means the process is still running. To speed up troubleshooting, it's necessary to have logs indicating whether the service started successfully. Should we write success and error messages to `/var/log/cube-sandbox-one-click/cubemaster.log? This is because when the installation script encounters an error, it prompts us to check the files in the directory.
Collaborator
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. Panic will log to stderr and should be captured, no?
Author
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. Panic will log to stderr and can be captured. but it will print the stack. Is it ok to print the stack? |
||
| if err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
|
|
||
| actualAddr := ln.Addr().String() | ||
| CubeLog.Infof("cube-master listening on %s", actualAddr) | ||
| stdlog.Printf("cube-master listening on %s", actualAddr) | ||
|
|
||
| err = s.Serve(ln) | ||
| if err == http.ErrServerClosed { | ||
| return nil | ||
| } | ||
|
|
||
| return errors.WithStack(err) | ||
| } | ||
|
|
||
| func (s *Server) Run() { | ||
| if s.InternalHttpServer != nil { | ||
| go func() { | ||
| if err := s.InternalHttpServer.Start(); err != nil { | ||
| CubeLog.Errorf("ListenAndServe:%v", err) | ||
| s.ErrChan <- err | ||
| } | ||
| }() | ||
| } | ||
|
|
||
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.
Does this change work with cubemastercli?
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 logic is ported from the Go standard library (net/http/server.go, v1.24.8).
I migrated the original implementation here to gain more granular control over the startup sequence. Specifically, it allows us to inject the startup/failure logs and port check logic directly into the serving loop, which addresses the observability requirements for cube-master.
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.
You did not answer my question.
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.
Yes, it works