Skip to content

Commit

Permalink
rpc: add graceful shutdown timeout for HTTP server (ethereum#25258)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
2 people authored and blakehhuynh committed Oct 7, 2022
1 parent 1648ff7 commit 6003378
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion node/rpcstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strings"
"sync"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rpc"
Expand Down Expand Up @@ -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)}

Expand Down Expand Up @@ -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())

Expand Down

0 comments on commit 6003378

Please sign in to comment.