mirror of
https://gitlab.com/pulsechaincom/go-pulse.git
synced 2024-12-22 03:30:35 +00:00
rpc: add graceful shutdown timeout for HTTP server (#25258)
This change ensures the HTTP server will always terminate within at most 5s, even when all connections are busy and do not become idle. Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
parent
ae8ce72022
commit
d839515434
@ -27,6 +27,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
@ -81,6 +82,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)}
|
||||
|
||||
@ -261,7 +266,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())
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user