Skip to content

Commit

Permalink
feat: handle timeout when shutting down http server
Browse files Browse the repository at this point in the history
  • Loading branch information
thoas committed Sep 26, 2023
1 parent 50cf3a2 commit 75436a6
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,19 @@ func (s *HTTPServer) Run(ctx context.Context) error {
case err := <-cerr:
return err
case <-ctx.Done():
s.processor.Logger.Debug("Shutdown HTTP server")
return srv.Shutdown(context.Background())
s.processor.Logger.Debug("Shutting down HTTP server")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
return err
}
select {
case <-ctx.Done():
s.processor.Logger.Debug("HTTP server timeout")
}

s.processor.Logger.Debug("HTTP server existing")

return nil
}
}

0 comments on commit 75436a6

Please sign in to comment.