node: add graceful shutdown timeout for HTTP server #25258 (#1144)

This commit is contained in:
JukLee0ira 2025-06-24 15:52:50 +08:00 committed by GitHub
parent a347ff7839
commit 30e0bfe71e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -28,6 +28,7 @@ import (
"strings"
"sync"
"sync/atomic"
"time"
"github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/rpc"
@ -88,6 +89,10 @@ type httpServer struct {
handlerNames map[string]string
}
const (
shutdownTimeout = 5 * time.Second
)
func newHTTPServer(log log.Logger, timeouts rpc.HTTPTimeouts) *httpServer {
h := &httpServer{log: log, timeouts: timeouts, handlerNames: make(map[string]string)}
h.httpHandler.Store((*rpcHandler)(nil))
@ -270,7 +275,13 @@ func (h *httpServer) doStop() {
h.wsHandler.Store((*rpcHandler)(nil))
wsHandler.server.Stop()
}
h.server.Shutdown(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
err := h.server.Shutdown(ctx)
if err == ctx.Err() {
h.log.Warn("HTTP server graceful shutdown timed out")
h.server.Close()
}
h.listener.Close()
h.log.Info("HTTP server stopped", "endpoint", h.listener.Addr())