Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[httputil] add ReadHeaderTimeout #3550

Merged
merged 4 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 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 @@ -26,22 +27,17 @@ type (
)

// NewHTTPServer creates a new http server
// TODO: move timeout into config
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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comment on l30

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 3rd commit

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

// Start starts the http server
Expand Down
2 changes: 1 addition & 1 deletion pkg/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func New(port int, opts ...Option) *Server {
mux.HandleFunc("/health", readiness)
mux.Handle("/metrics", promhttp.Handler())

s.server = httputil.Server(fmt.Sprintf(":%d", port), mux)
s.server = httputil.NewServer(fmt.Sprintf(":%d", port), mux)
return s
}

Expand Down
50 changes: 40 additions & 10 deletions pkg/util/httputil/httputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,49 @@ import (

const (
Copy link
Member

@Liuhaai Liuhaai Jul 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add as the default conf instead of const?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in 2nd commit

_connectionCount = 400
_readTimeout = 35 * time.Second
_writeTimeout = 35 * time.Second
_idleTimeout = 120 * time.Second
)

// Server creates a HTTP server with time out settings.
func Server(addr string, handler http.Handler) http.Server {
type (
// ServerOption is a server option
ServerOption func(*serverConfig)

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

// DefaultServerConfig is the default server config
var DefaultServerConfig = serverConfig{
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 120 * time.Second,
}

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

// NewServer creates a HTTP server with time out settings.
func NewServer(addr string, handler http.Handler, opts ...ServerOption) http.Server {
cfg := DefaultServerConfig
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
13 changes: 7 additions & 6 deletions pkg/util/httputil/httputil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ 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: 2 * time.Second,
ReadTimeout: DefaultServerConfig.ReadTimeout,
WriteTimeout: DefaultServerConfig.WriteTimeout,
IdleTimeout: DefaultServerConfig.IdleTimeout,
Addr: addr,
Handler: handler,
}
result := Server(addr, handler)
result := NewServer(addr, handler, ReadHeaderTimeout(2*time.Second))
require.Equal(t, expectValue, result)
})
}
Expand Down
2 changes: 1 addition & 1 deletion server/itx/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func StartServer(ctx context.Context, svr *Server, probeSvr *probe.Server, cfg c
mux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))

port := fmt.Sprintf(":%d", cfg.System.HTTPAdminPort)
adminserv = httputil.Server(port, mux)
adminserv = httputil.NewServer(port, mux)
defer func() {
if err := adminserv.Shutdown(ctx); err != nil {
log.L().Error("Error when serving metrics data.", zap.Error(err))
Expand Down