From a9617c1e4aa5126366bc344e6208ab561d62a742 Mon Sep 17 00:00:00 2001 From: Dustin Xie Date: Thu, 14 Jul 2022 10:03:32 -0700 Subject: [PATCH] [httputil] add ReadHeaderTimeout --- api/http.go | 15 ++++----- pkg/util/httputil/httputil.go | 51 ++++++++++++++++++++++++------ pkg/util/httputil/httputil_test.go | 12 +++---- 3 files changed, 53 insertions(+), 25 deletions(-) diff --git a/api/http.go b/api/http.go index 66579ba1bd..67e9d8b488 100644 --- a/api/http.go +++ b/api/http.go @@ -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 ( @@ -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 diff --git a/pkg/util/httputil/httputil.go b/pkg/util/httputil/httputil.go index d5dbea833b..58d464e2bb 100644 --- a/pkg/util/httputil/httputil.go +++ b/pkg/util/httputil/httputil.go @@ -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, } } diff --git a/pkg/util/httputil/httputil_test.go b/pkg/util/httputil/httputil_test.go index 29189ed983..edbdf92f4e 100644 --- a/pkg/util/httputil/httputil_test.go +++ b/pkg/util/httputil/httputil_test.go @@ -4,7 +4,6 @@ import ( "errors" "net/http" "testing" - "time" "github.com/stretchr/testify/require" ) @@ -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)