Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Jul 15, 2022
1 parent 6f4b538 commit b29714f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewHTTPServer(route string, port int, handler http.Handler) *HTTPServer {
mux := http.NewServeMux()
mux.Handle("/"+route, handler)

svr := httputil.Server(":"+strconv.Itoa(port), mux, httputil.HeaderTimeout(10*time.Second))
svr := httputil.NewServer(":"+strconv.Itoa(port), mux, httputil.HeaderTimeout(10*time.Second))
return &HTTPServer{
svr: &svr,
}
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
24 changes: 11 additions & 13 deletions pkg/util/httputil/httputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import (
)

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

type (
Expand All @@ -28,21 +24,23 @@ type (
}
)

var DefaultServerConfig = serverConfig{
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 120 * time.Second,
}

// 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, opts ...ServerOption) http.Server {
cfg := serverConfig{
ReadHeaderTimeout: _readHeaderTimeout,
ReadTimeout: _readTimeout,
WriteTimeout: _writeTimeout,
IdleTimeout: _idleTimeout,
}
// 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)
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/util/httputil/httputil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net/http"
"testing"
"time"

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

expectValue := http.Server{
ReadHeaderTimeout: _readHeaderTimeout,
ReadTimeout: _readTimeout,
WriteTimeout: _writeTimeout,
IdleTimeout: _idleTimeout,
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, HeaderTimeout(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

0 comments on commit b29714f

Please sign in to comment.