-
Notifications
You must be signed in to change notification settings - Fork 324
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,19 +10,49 @@ import ( | |
|
||
const ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add as the default conf instead of const? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
} | ||
|
||
// HeaderTimeout sets header timeout | ||
func HeaderTimeout(h time.Duration) ServerOption { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ReadHeaderTimeout() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in 3rd commit |
||
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, | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove comment on l30
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in 3rd commit