Skip to content

Commit

Permalink
Set read/write timeouts for HTTP servers (ssvlabs#991)
Browse files Browse the repository at this point in the history
Co-authored-by: Lior Rutenberg <[email protected]>
  • Loading branch information
nkryuchkov and Lior Rutenberg authored Jun 7, 2023
1 parent 20150b5 commit b7ccbe0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
20 changes: 14 additions & 6 deletions exporter/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"net/http"
"time"

"github.com/bloxapp/ssv/logging"
"github.com/bloxapp/ssv/logging/fields"
"github.com/bloxapp/ssv/utils/tasks"
"github.com/gorilla/websocket"
"github.com/prysmaticlabs/prysm/async/event"
"go.uber.org/zap"

"github.com/bloxapp/ssv/logging"
"github.com/bloxapp/ssv/logging/fields"
"github.com/bloxapp/ssv/utils/tasks"
)

const (
Expand Down Expand Up @@ -70,9 +71,16 @@ func (ws *wsServer) Start(logger *zap.Logger, addr string) error {

logger.Info("starting", fields.Address(addr), zap.Strings("endPoints", []string{"/query", "/stream"}))

// TODO: enable lint (G114: Use of net/http serve function that has no support for setting timeouts (gosec))
// nolint: gosec
err := http.ListenAndServe(addr, ws.router)
const timeout = 3 * time.Second

httpServer := &http.Server{
Addr: addr,
Handler: ws.router,
ReadTimeout: timeout,
WriteTimeout: timeout,
}

err := httpServer.ListenAndServe()
if err != nil {
logger.Warn("could not start", zap.Error(err))
}
Expand Down
20 changes: 15 additions & 5 deletions monitoring/metrics/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
http_pprof "net/http/pprof"
"runtime"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand Down Expand Up @@ -85,11 +86,20 @@ func (mh *metricsHandler) Start(logger *zap.Logger, mux *http.ServeMux, addr str
mux.HandleFunc("/database/count-by-collection", mh.handleCountByCollection)
mux.HandleFunc("/health", mh.handleHealth)

// TODO: enable lint (G114: Use of net/http serve function that has no support for setting timeouts (gosec))
// nolint: gosec
if err := http.ListenAndServe(addr, mux); err != nil {
return fmt.Errorf("listen to %s: %w", addr, err)
}
go func() {
const timeout = 3 * time.Second

httpServer := &http.Server{
Addr: addr,
Handler: mux,
ReadTimeout: timeout,
WriteTimeout: timeout,
}

if err := httpServer.ListenAndServe(); err != nil {
return fmt.Errorf("listen to %s: %w", addr, err)
}
}()

return nil
}
Expand Down
21 changes: 14 additions & 7 deletions utils/boot_node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import (
"net"
"net/http"
"path/filepath"

"github.com/bloxapp/ssv/logging"
"github.com/bloxapp/ssv/logging/fields"
"github.com/bloxapp/ssv/networkconfig"
"time"

"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/enode"
Expand All @@ -23,6 +20,9 @@ import (
"go.uber.org/zap"

"github.com/bloxapp/ssv/beacon/goclient"
"github.com/bloxapp/ssv/logging"
"github.com/bloxapp/ssv/logging/fields"
"github.com/bloxapp/ssv/networkconfig"
"github.com/bloxapp/ssv/utils"
)

Expand Down Expand Up @@ -121,9 +121,16 @@ func (n *bootNode) Start(ctx context.Context, logger *zap.Logger) error {
mux := http.NewServeMux()
mux.HandleFunc("/p2p", handler.httpHandler(logger))

// TODO: enable lint (G114: Use of net/http serve function that has no support for setting timeouts (gosec))
// nolint: gosec
if err := http.ListenAndServe(fmt.Sprintf(":%d", n.tcpPort), mux); err != nil {
const timeout = 3 * time.Second

httpServer := &http.Server{
Addr: fmt.Sprintf(":%d", n.tcpPort),
Handler: mux,
ReadTimeout: timeout,
WriteTimeout: timeout,
}

if err := httpServer.ListenAndServe(); err != nil {
log.Fatalf("Failed to start server %v", err)
}

Expand Down

0 comments on commit b7ccbe0

Please sign in to comment.