Skip to content

Commit

Permalink
Cap max wait time for Stop
Browse files Browse the repository at this point in the history
  • Loading branch information
sevein committed Jun 28, 2024
1 parent f504e62 commit d792d23
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
5 changes: 1 addition & 4 deletions hack/ccp/internal/api/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,7 @@ func (s *Server) ListProcessingConfigurationFields(ctx context.Context, req *con
}), nil
}

func (s *Server) Close() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

func (s *Server) Close(ctx context.Context) error {
if s.server != nil {
if err := s.server.Shutdown(ctx); err != nil {
return err
Expand Down
20 changes: 15 additions & 5 deletions hack/ccp/internal/cmd/servercmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,12 @@ func (s *Server) Run() error {
func (s *Server) Close() error {
var errs error

ctx := context.Background()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

s.logger.Info("Shutting down...")

s.cancel()
s.cancel() // Cancel the root context.

if s.store != nil && s.store.Running() {
errs = errors.Join(errs, s.store.Close())
Expand All @@ -183,18 +184,27 @@ func (s *Server) Close() error {
}

if s.admin != nil {
errs = errors.Join(errs, s.admin.Close())
errs = errors.Join(errs, s.admin.Close(ctx))
}

if s.webui != nil {
errs = errors.Join(errs, s.webui.Close())
errs = errors.Join(errs, s.webui.Close(ctx))
}

if s.metrics != nil {
errs = errors.Join(errs, s.metrics.Close(ctx))
}

s.gearman.Stop()
// TODO: gearmin's Stop method can block indefinitely under certain circumstances.
done := make(chan struct{}, 1)
go func() {
s.gearman.Stop()
done <- struct{}{}
}()
select {
case <-time.After(time.Second / 2):
case <-done:
}

return errs
}
Expand Down
5 changes: 1 addition & 4 deletions hack/ccp/internal/webui/webui.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ func (s *Server) health(w http.ResponseWriter, r *http.Request) {
s.json(w, http.StatusOK, map[string]string{"status": "OK"})
}

func (s *Server) Close() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

func (s *Server) Close(ctx context.Context) error {
return s.server.Shutdown(ctx)
}

0 comments on commit d792d23

Please sign in to comment.