Skip to content

Commit

Permalink
Apply shutdown timeout to http server to limit reload delay
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonip committed Oct 11, 2024
1 parent 5a82ecc commit ed864d5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions internal/beater/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ func (h *httpServer) start() error {
return h.Serve(h.httpListener)
}

func (h *httpServer) stop() {
func (h *httpServer) stop(ctx context.Context) {
h.logger.Infof("Stop listening on: %s", h.Server.Addr)
if err := h.Shutdown(context.Background()); err != nil {
if err := h.Shutdown(ctx); err != nil {
h.logger.Errorf("error stopping http server: %s", err.Error())
if err := h.Close(); err != nil {
h.logger.Errorf("error closing http server: %s", err.Error())
Expand Down
9 changes: 5 additions & 4 deletions internal/beater/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,11 @@ func (s server) run(ctx context.Context) error {
})
g.Go(func() error {
<-ctx.Done()
// httpServer should stop before grpcServer to avoid a panic caused by placing a new connection into
// a closed grpc connection channel during shutdown.
// See https://github.com/elastic/gmux/issues/13
s.httpServer.stop()
stopctx, cancel := context.WithTimeout(context.Background(), s.cfg.ShutdownTimeout)
defer cancel()
s.httpServer.stop(stopctx)
// FIXME: will httpServer.stop terminate the grpc connections, making grpcServer.GracefulStop a no-op?
// Will it be better to grpcServer.GracefulStop first as the gmux panic is fixed, so that GOAWAY is sent?
s.grpcServer.GracefulStop()
return nil
})
Expand Down

0 comments on commit ed864d5

Please sign in to comment.