Skip to content

Commit 6fb1e0d

Browse files
committed
####Version 1.7.19
* feature: add SetReadTimeout\SetReadHeaderTimeout\SetIdleTimeoutSetWriteTimeout func() in HttpServer * 2021-04-20 13:00 at ShangHai
1 parent 7f371aa commit 6fb1e0d

5 files changed

Lines changed: 71 additions & 6 deletions

File tree

config/configs.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,30 @@ type (
5757
// default is 32 << 20 (32 mb), MaxBodySize use go runtime default zero value
5858
// -1 : unlimted
5959
// 0 : use default value
60-
MaxBodySize int64 `xml:"MaxBodySize,attr"` // To limit the request's body size to be read
60+
MaxBodySize int64 `xml:"MaxBodySize,attr"`
61+
62+
// To limit the request's body size to be read with Millisecond
63+
// ReadTimeout is the maximum duration for reading the entire
64+
// request, including the body.
65+
ReadTimeout int64 `xml:"ReadTimeout,attr"`
66+
67+
// ReadHeaderTimeout is the amount of time allowed to read
68+
// request headers with Millisecond. The connection's read deadline is reset
69+
// after reading the headers and the Handler can decide what
70+
// is considered too slow for the body.
71+
ReadHeaderTimeout int64 `xml:"ReadHeaderTimeout,attr"`
72+
73+
// WriteTimeout is the maximum duration before timing out
74+
// writes of the response with Millisecond. It is reset whenever a new
75+
// request's header is read. Like ReadTimeout, it does not
76+
// let Handlers make decisions on a per-request basis.
77+
WriteTimeout int64 `xml:"WriteTimeout,attr"`
78+
79+
// IdleTimeout is the maximum amount of time to wait for the
80+
// next request when keep-alives are enabled with Millisecond. If IdleTimeout
81+
// is zero, the value of ReadTimeout is used. If both are
82+
// zero, ReadHeaderTimeout is used.
83+
IdleTimeout int64 `xml:"IdleTimeout,attr"`
6184
}
6285

6386
// SessionNode dotweb app's session config

consts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotweb
33
// Global define
44
const (
55
// Version current version
6-
Version = "1.7.18"
6+
Version = "1.7.19"
77
)
88

99
// Log define

dotweb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ func (app *DotWeb) initAppConfig() {
433433
app.Config.App.RunMode = RunMode_Development
434434
}
435435

436-
app.HttpServer.initConfig()
436+
app.HttpServer.initConfig(app.Config)
437437

438438
// detailed request metrics
439439
if config.Server.EnabledDetailRequestData {

server.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package dotweb
22

33
import (
44
"compress/gzip"
5+
"github.com/devfeel/dotweb/logger"
56
"io"
67
"net/http"
78
"net/url"
89
"strings"
910
"sync"
10-
11-
"github.com/devfeel/dotweb/logger"
11+
"time"
1212

1313
"github.com/devfeel/dotweb/core"
1414
"github.com/devfeel/dotweb/session"
@@ -84,7 +84,19 @@ func NewHttpServer() *HttpServer {
8484
}
8585

8686
// initConfig init config from app config
87-
func (server *HttpServer) initConfig() {
87+
func (server *HttpServer) initConfig(config *config.Config) {
88+
if config.Server.WriteTimeout > 0 {
89+
server.stdServer.WriteTimeout = time.Duration(config.Server.WriteTimeout) * time.Millisecond
90+
}
91+
if config.Server.ReadTimeout > 0 {
92+
server.stdServer.ReadTimeout = time.Duration(config.Server.ReadTimeout) * time.Millisecond
93+
}
94+
if config.Server.ReadHeaderTimeout > 0 {
95+
server.stdServer.ReadHeaderTimeout = time.Duration(config.Server.ReadHeaderTimeout) * time.Millisecond
96+
}
97+
if config.Server.IdleTimeout > 0 {
98+
server.stdServer.IdleTimeout = time.Duration(config.Server.IdleTimeout) * time.Millisecond
99+
}
88100
}
89101

90102
// ServerConfig a shortcut for App.Config.ServerConfig
@@ -475,6 +487,31 @@ func (server *HttpServer) SetEnabledStaticFileMiddleware(isEnabled bool) {
475487
server.Logger().Debug("DotWeb:HttpServer SetEnabledStaticFileMiddleware ["+strconv.FormatBool(isEnabled)+"]", LogTarget_HttpServer)
476488
}
477489

490+
// SetReadTimeout To limit the request's body size to be read with Millisecond
491+
func (server *HttpServer) SetReadTimeout(readTimeout int64) {
492+
server.ServerConfig().ReadTimeout = readTimeout
493+
server.Logger().Debug("DotWeb:HttpServer SetReadTimeout ["+strconv.FormatInt(readTimeout, 10)+"]", LogTarget_HttpServer)
494+
}
495+
496+
// SetReadHeaderTimeout ReadHeaderTimeout is the amount of time allowed to read request headers with Millisecond
497+
func (server *HttpServer) SetReadHeaderTimeout(readHeaderTimeout int64) {
498+
server.ServerConfig().ReadHeaderTimeout = readHeaderTimeout
499+
server.Logger().Debug("DotWeb:HttpServer SetReadHeaderTimeout ["+strconv.FormatInt(readHeaderTimeout, 10)+"]", LogTarget_HttpServer)
500+
}
501+
502+
// SetIdleTimeout IdleTimeout is the maximum amount of time to wait for the next request when keep-alives are enabled with Millisecond
503+
func (server *HttpServer) SetIdleTimeout(idleTimeout int64) {
504+
server.ServerConfig().IdleTimeout = idleTimeout
505+
server.Logger().Debug("DotWeb:HttpServer SetIdleTimeout ["+strconv.FormatInt(idleTimeout, 10)+"]", LogTarget_HttpServer)
506+
}
507+
508+
// SetWriteTimeout WriteTimeout is the maximum duration before timing out
509+
// writes of the response with Millisecond
510+
func (server *HttpServer) SetWriteTimeout(writeTimeout int64) {
511+
server.ServerConfig().WriteTimeout = writeTimeout
512+
server.Logger().Debug("DotWeb:HttpServer SetWriteTimeout ["+strconv.FormatInt(writeTimeout, 10)+"]", LogTarget_HttpServer)
513+
}
514+
478515
// SetMaxBodySize set body size to limit read
479516
func (server *HttpServer) SetMaxBodySize(maxBodySize int64) {
480517
server.ServerConfig().MaxBodySize = maxBodySize

version.MD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## dotweb版本记录:
22

3+
4+
####Version 1.7.19
5+
* feature: add SetReadTimeout\SetReadHeaderTimeout\SetIdleTimeoutSetWriteTimeout func() in HttpServer
6+
* 2021-04-20 13:00 at ShangHai
7+
38
####Version 1.7.18
49
* Bug fix: fix deepcopy middleware not success
510
* 2021-04-20 13:00 at ShangHai

0 commit comments

Comments
 (0)