Skip to content

Commit

Permalink
[httputil] add ReadHeaderTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Jul 15, 2022
1 parent ad61fa3 commit a9617c1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 25 deletions.
15 changes: 6 additions & 9 deletions api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

apitypes "github.com/iotexproject/iotex-core/api/types"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/util/httputil"
)

type (
Expand All @@ -31,17 +32,13 @@ func NewHTTPServer(route string, port int, handler http.Handler) *HTTPServer {
if port == 0 {
return nil
}
svr := &HTTPServer{
svr: &http.Server{
Addr: ":" + strconv.Itoa(port),
WriteTimeout: 30 * time.Second,
ReadHeaderTimeout: 10 * time.Second,
},
}
mux := http.NewServeMux()
mux.Handle("/"+route, handler)
svr.svr.Handler = mux
return svr

svr := httputil.Server(":"+strconv.Itoa(port), mux, httputil.HeaderTimeout(10*time.Second))
return &HTTPServer{
svr: &svr,
}
}

// Start starts the http server
Expand Down
51 changes: 41 additions & 10 deletions pkg/util/httputil/httputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,51 @@ import (
)

const (
_connectionCount = 400
_readTimeout = 35 * time.Second
_writeTimeout = 35 * time.Second
_idleTimeout = 120 * time.Second
_connectionCount = 400
_readHeaderTimeout = 5 * time.Second
_readTimeout = 30 * time.Second
_writeTimeout = 30 * time.Second
_idleTimeout = 120 * time.Second
)

type (
// ServerOption is a server option
ServerOption func(*serverConfig)

serverConfig struct {
ReadHeaderTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
}
)

// HeaderTimeout sets header timeout
func HeaderTimeout(h time.Duration) ServerOption {
return func(cfg *serverConfig) {
cfg.ReadHeaderTimeout = h
}
}

// Server creates a HTTP server with time out settings.
func Server(addr string, handler http.Handler) http.Server {
func Server(addr string, handler http.Handler, opts ...ServerOption) http.Server {
cfg := serverConfig{
ReadHeaderTimeout: _readHeaderTimeout,
ReadTimeout: _readTimeout,
WriteTimeout: _writeTimeout,
IdleTimeout: _idleTimeout,
}
for _, opt := range opts {
opt(&cfg)
}

return http.Server{
ReadTimeout: _readTimeout,
WriteTimeout: _writeTimeout,
IdleTimeout: _idleTimeout,
Addr: addr,
Handler: handler,
ReadHeaderTimeout: cfg.ReadHeaderTimeout,
ReadTimeout: cfg.ReadTimeout,
WriteTimeout: cfg.WriteTimeout,
IdleTimeout: cfg.IdleTimeout,
Addr: addr,
Handler: handler,
}
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/util/httputil/httputil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand All @@ -15,11 +14,12 @@ func TestServer(t *testing.T) {
addr := "myAddress"

expectValue := http.Server{
ReadTimeout: 35 * time.Second,
WriteTimeout: 35 * time.Second,
IdleTimeout: 120 * time.Second,
Addr: addr,
Handler: handler,
ReadHeaderTimeout: _readHeaderTimeout,
ReadTimeout: _readTimeout,
WriteTimeout: _writeTimeout,
IdleTimeout: _idleTimeout,
Addr: addr,
Handler: handler,
}
result := Server(addr, handler)
require.Equal(t, expectValue, result)
Expand Down

0 comments on commit a9617c1

Please sign in to comment.