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

do not fatally exit from serve commands #1016

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/opm/registry/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func serveFunc(cmd *cobra.Command, _ []string) error {

lis, err := net.Listen("tcp", ":"+port)
if err != nil {
logger.Fatalf("failed to listen: %s", err)
return fmt.Errorf("failed to listen: %s", err)
}

timeout, err := cmd.Flags().GetString("timeout-seconds")
Expand Down
45 changes: 31 additions & 14 deletions cmd/opm/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ will not be reflected in the served content.

func (s *serve) run(ctx context.Context) error {
p := newProfilerInterface(s.pprofAddr, s.logger)
p.startEndpoint()
if err := p.startEndpoint(); err != nil {
return fmt.Errorf("could not start pprof endpoint: %v", err)
}
if err := p.startCpuProfileCache(); err != nil {
return fmt.Errorf("could not start CPU profile: %v", err)
}
Expand Down Expand Up @@ -111,7 +113,7 @@ func (s *serve) run(ctx context.Context) error {

lis, err := net.Listen("tcp", ":"+s.port)
if err != nil {
s.logger.Fatalf("failed to listen: %s", err)
return fmt.Errorf("failed to listen: %s", err)
}

grpcServer := grpc.NewServer()
Expand All @@ -125,7 +127,9 @@ func (s *serve) run(ctx context.Context) error {
return grpcServer.Serve(lis)
}, func() {
grpcServer.GracefulStop()
p.stopEndpoint(p.logger.Context)
if err := p.stopEndpoint(ctx); err != nil {
s.logger.Warnf("error shutting down pprof server: %v", err)
}
})

}
Expand All @@ -143,7 +147,8 @@ type profilerInterface struct {
cacheReady bool
cacheLock sync.RWMutex

logger *logrus.Entry
logger *logrus.Entry
closeErr chan error
}

func newProfilerInterface(a string, log *logrus.Entry) *profilerInterface {
Expand All @@ -158,10 +163,10 @@ func (p *profilerInterface) isEnabled() bool {
return p.addr != ""
}

func (p *profilerInterface) startEndpoint() {
func (p *profilerInterface) startEndpoint() error {
// short-circuit if not enabled
if !p.isEnabled() {
return
return nil
}

mux := http.NewServeMux()
Expand All @@ -177,14 +182,22 @@ func (p *profilerInterface) startEndpoint() {
Handler: mux,
}

// goroutine exits with main
go func() {
lis, err := net.Listen("tcp", p.addr)
if err != nil {
return err
}

p.logger.Info("starting pprof endpoint")
if err := p.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
p.logger.Fatal(err)
}
p.closeErr = make(chan error)
go func() {
p.closeErr <- func() error {
p.logger.Info("starting pprof endpoint")
if err := p.server.Serve(lis); err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
}()
}()
return nil
}

func (p *profilerInterface) startCpuProfileCache() error {
Expand Down Expand Up @@ -218,10 +231,14 @@ func (p *profilerInterface) httpHandler(w http.ResponseWriter, r *http.Request)
w.Write(p.cache.Bytes())
}

func (p *profilerInterface) stopEndpoint(ctx context.Context) {
func (p *profilerInterface) stopEndpoint(ctx context.Context) error {
if !p.isEnabled() {
return nil
}
if err := p.server.Shutdown(ctx); err != nil {
p.logger.Fatal(err)
return err
}
return <-p.closeErr
}

func (p *profilerInterface) isCacheReady() bool {
Expand Down