diff --git a/config/configs.go b/config/configs.go index d94dbf9..b975644 100644 --- a/config/configs.go +++ b/config/configs.go @@ -57,7 +57,30 @@ type ( // default is 32 << 20 (32 mb), MaxBodySize use go runtime default zero value // -1 : unlimted // 0 : use default value - MaxBodySize int64 `xml:"MaxBodySize,attr"` // To limit the request's body size to be read + MaxBodySize int64 `xml:"MaxBodySize,attr"` + + // To limit the request's body size to be read with Millisecond + // ReadTimeout is the maximum duration for reading the entire + // request, including the body. + ReadTimeout int64 `xml:"ReadTimeout,attr"` + + // ReadHeaderTimeout is the amount of time allowed to read + // request headers with Millisecond. The connection's read deadline is reset + // after reading the headers and the Handler can decide what + // is considered too slow for the body. + ReadHeaderTimeout int64 `xml:"ReadHeaderTimeout,attr"` + + // WriteTimeout is the maximum duration before timing out + // writes of the response with Millisecond. It is reset whenever a new + // request's header is read. Like ReadTimeout, it does not + // let Handlers make decisions on a per-request basis. + WriteTimeout int64 `xml:"WriteTimeout,attr"` + + // IdleTimeout is the maximum amount of time to wait for the + // next request when keep-alives are enabled with Millisecond. If IdleTimeout + // is zero, the value of ReadTimeout is used. If both are + // zero, ReadHeaderTimeout is used. + IdleTimeout int64 `xml:"IdleTimeout,attr"` } // SessionNode dotweb app's session config diff --git a/consts.go b/consts.go index e7a4036..1d9b109 100644 --- a/consts.go +++ b/consts.go @@ -3,7 +3,7 @@ package dotweb // Global define const ( // Version current version - Version = "1.7.18" + Version = "1.7.19" ) // Log define diff --git a/dotweb.go b/dotweb.go index 13902b2..03c8bd7 100644 --- a/dotweb.go +++ b/dotweb.go @@ -433,7 +433,7 @@ func (app *DotWeb) initAppConfig() { app.Config.App.RunMode = RunMode_Development } - app.HttpServer.initConfig() + app.HttpServer.initConfig(app.Config) // detailed request metrics if config.Server.EnabledDetailRequestData { diff --git a/server.go b/server.go index c7bf79f..63d1bf7 100644 --- a/server.go +++ b/server.go @@ -2,13 +2,13 @@ package dotweb import ( "compress/gzip" + "github.com/devfeel/dotweb/logger" "io" "net/http" "net/url" "strings" "sync" - - "github.com/devfeel/dotweb/logger" + "time" "github.com/devfeel/dotweb/core" "github.com/devfeel/dotweb/session" @@ -84,7 +84,19 @@ func NewHttpServer() *HttpServer { } // initConfig init config from app config -func (server *HttpServer) initConfig() { +func (server *HttpServer) initConfig(config *config.Config) { + if config.Server.WriteTimeout > 0 { + server.stdServer.WriteTimeout = time.Duration(config.Server.WriteTimeout) * time.Millisecond + } + if config.Server.ReadTimeout > 0 { + server.stdServer.ReadTimeout = time.Duration(config.Server.ReadTimeout) * time.Millisecond + } + if config.Server.ReadHeaderTimeout > 0 { + server.stdServer.ReadHeaderTimeout = time.Duration(config.Server.ReadHeaderTimeout) * time.Millisecond + } + if config.Server.IdleTimeout > 0 { + server.stdServer.IdleTimeout = time.Duration(config.Server.IdleTimeout) * time.Millisecond + } } // ServerConfig a shortcut for App.Config.ServerConfig @@ -475,6 +487,31 @@ func (server *HttpServer) SetEnabledStaticFileMiddleware(isEnabled bool) { server.Logger().Debug("DotWeb:HttpServer SetEnabledStaticFileMiddleware ["+strconv.FormatBool(isEnabled)+"]", LogTarget_HttpServer) } +// SetReadTimeout To limit the request's body size to be read with Millisecond +func (server *HttpServer) SetReadTimeout(readTimeout int64) { + server.ServerConfig().ReadTimeout = readTimeout + server.Logger().Debug("DotWeb:HttpServer SetReadTimeout ["+strconv.FormatInt(readTimeout, 10)+"]", LogTarget_HttpServer) +} + +// SetReadHeaderTimeout ReadHeaderTimeout is the amount of time allowed to read request headers with Millisecond +func (server *HttpServer) SetReadHeaderTimeout(readHeaderTimeout int64) { + server.ServerConfig().ReadHeaderTimeout = readHeaderTimeout + server.Logger().Debug("DotWeb:HttpServer SetReadHeaderTimeout ["+strconv.FormatInt(readHeaderTimeout, 10)+"]", LogTarget_HttpServer) +} + +// SetIdleTimeout IdleTimeout is the maximum amount of time to wait for the next request when keep-alives are enabled with Millisecond +func (server *HttpServer) SetIdleTimeout(idleTimeout int64) { + server.ServerConfig().IdleTimeout = idleTimeout + server.Logger().Debug("DotWeb:HttpServer SetIdleTimeout ["+strconv.FormatInt(idleTimeout, 10)+"]", LogTarget_HttpServer) +} + +// SetWriteTimeout WriteTimeout is the maximum duration before timing out +// writes of the response with Millisecond +func (server *HttpServer) SetWriteTimeout(writeTimeout int64) { + server.ServerConfig().WriteTimeout = writeTimeout + server.Logger().Debug("DotWeb:HttpServer SetWriteTimeout ["+strconv.FormatInt(writeTimeout, 10)+"]", LogTarget_HttpServer) +} + // SetMaxBodySize set body size to limit read func (server *HttpServer) SetMaxBodySize(maxBodySize int64) { server.ServerConfig().MaxBodySize = maxBodySize diff --git a/version.MD b/version.MD index b6a4a6b..9be7416 100644 --- a/version.MD +++ b/version.MD @@ -1,6 +1,11 @@ ## dotweb版本记录: -####Version 1.7.18 + +####Version 1.7.19 +* feature: add SetReadTimeout\SetReadHeaderTimeout\SetIdleTimeoutSetWriteTimeout func() in HttpServer +* 2021-04-20 13:00 at ShangHai + +####Version 1.7.18 * Bug fix: fix deepcopy middleware not success * 2021-04-20 13:00 at ShangHai