mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-11 00:40:00 +08:00
fix: timeout 0s not working (#4932)
Signed-off-by: kevin <wanjunfeng@gmail.com>
This commit is contained in:
@@ -28,7 +28,10 @@ var ErrSignatureConfig = errors.New("bad config for Signature")
|
||||
type engine struct {
|
||||
conf RestConf
|
||||
routes []featuredRoutes
|
||||
// timeout is the max timeout of all routes
|
||||
// timeout is the max timeout of all routes,
|
||||
// and is used to set http.Server.ReadTimeout and http.Server.WriteTimeout.
|
||||
// this network timeout is used to avoid DoS attacks by sending data slowly
|
||||
// or receiving data slowly with many connections to exhaust server resources.
|
||||
timeout time.Duration
|
||||
unauthorizedCallback handler.UnauthorizedCallback
|
||||
unsignedCallback handler.UnsignedCallback
|
||||
@@ -60,11 +63,7 @@ func (ng *engine) addRoutes(r featuredRoutes) {
|
||||
}
|
||||
ng.routes = append(ng.routes, r)
|
||||
|
||||
// need to guarantee the timeout is the max of all routes
|
||||
// otherwise impossible to set http.Server.ReadTimeout & WriteTimeout
|
||||
if r.timeout > ng.timeout {
|
||||
ng.timeout = r.timeout
|
||||
}
|
||||
ng.mightUpdateTimeout(r)
|
||||
}
|
||||
|
||||
func buildSSERoutes(routes []Route) []Route {
|
||||
@@ -192,11 +191,12 @@ func (ng *engine) checkedMaxBytes(bytes int64) int64 {
|
||||
return ng.conf.MaxBytes
|
||||
}
|
||||
|
||||
func (ng *engine) checkedTimeout(timeout time.Duration) time.Duration {
|
||||
if timeout > 0 {
|
||||
return timeout
|
||||
func (ng *engine) checkedTimeout(timeout *time.Duration) time.Duration {
|
||||
if timeout != nil {
|
||||
return *timeout
|
||||
}
|
||||
|
||||
// if timeout not set in featured routes, use global timeout
|
||||
return time.Duration(ng.conf.Timeout) * time.Millisecond
|
||||
}
|
||||
|
||||
@@ -232,6 +232,28 @@ func (ng *engine) hasTimeout() bool {
|
||||
return ng.conf.Middlewares.Timeout && ng.timeout > 0
|
||||
}
|
||||
|
||||
// mightUpdateTimeout checks if the route timeout is greater than the current,
|
||||
// and updates the engine's timeout accordingly.
|
||||
func (ng *engine) mightUpdateTimeout(r featuredRoutes) {
|
||||
// if global timeout is set to 0, it means no need to set read/write timeout
|
||||
// if route timeout is nil, no need to update ng.timeout
|
||||
if ng.timeout == 0 || r.timeout == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// if route timeout is 0 (means no timeout), cannot set read/write timeout
|
||||
if *r.timeout == 0 {
|
||||
ng.timeout = 0
|
||||
return
|
||||
}
|
||||
|
||||
// need to guarantee the timeout is the max of all routes
|
||||
// otherwise impossible to set http.Server.ReadTimeout & WriteTimeout
|
||||
if *r.timeout > ng.timeout {
|
||||
ng.timeout = *r.timeout
|
||||
}
|
||||
}
|
||||
|
||||
// notFoundHandler returns a middleware that handles 404 not found requests.
|
||||
func (ng *engine) notFoundHandler(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -333,7 +355,7 @@ func (ng *engine) start(router httpx.Router, opts ...StartOption) error {
|
||||
}
|
||||
|
||||
// make sure user defined options overwrite default options
|
||||
opts = append([]StartOption{ng.withTimeout()}, opts...)
|
||||
opts = append([]StartOption{ng.withNetworkTimeout()}, opts...)
|
||||
|
||||
if len(ng.conf.CertFile) == 0 && len(ng.conf.KeyFile) == 0 {
|
||||
return internal.StartHttp(ng.conf.Host, ng.conf.Port, router, opts...)
|
||||
@@ -356,7 +378,7 @@ func (ng *engine) use(middleware Middleware) {
|
||||
ng.middlewares = append(ng.middlewares, middleware)
|
||||
}
|
||||
|
||||
func (ng *engine) withTimeout() internal.StartOption {
|
||||
func (ng *engine) withNetworkTimeout() internal.StartOption {
|
||||
return func(svr *http.Server) {
|
||||
if !ng.hasTimeout() {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user