Skip to content

Commit 408c579

Browse files
committed
#### Version 0.3.21
* 新增HttpServer.ListenAndServer(addr string) 支持原生host监听 * Context增加Context() & SetTimeoutContext() & WithContext() * Request增加自动生成unique RequestID,通过RequestID()获取,默认置入dotweb.Context.context中,以RequestID为key * 2017-06-14 06:30
1 parent 35074d7 commit 408c579

6 files changed

Lines changed: 51 additions & 21 deletions

File tree

config/configs.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ type (
4747
EnabledAutoHEAD bool `xml:"enabledautohead,attr"` //设置是否自动启用Head路由,若设置该项,则会为除Websocket\HEAD外所有路由方式默认添加HEAD路由,默认不开启
4848
EnabledAutoCORS bool `xml:"enabledautocors,attr"` //设置是否自动跨域支持,若设置,默认“GET, POST, PUT, DELETE, OPTIONS”全部请求均支持跨域
4949
Port int `xml:"port,attr"` //端口
50-
RequestTimeOut int `xml:"requestrimeout,attr"` //请求超时时间,单位毫秒,默认30000毫秒
5150
}
5251

5352
SessionNode struct {
@@ -189,9 +188,7 @@ func InitConfig(configFile string, confType ...interface{}) (config *Config, err
189188
}
190189

191190
func dealConfigDefaultSet(c *Config) {
192-
if c.Server.RequestTimeOut <= 0 {
193-
c.Server.RequestTimeOut = DefaultRequestTimeOut
194-
}
191+
195192
}
196193

197194
//初始化配置文件(xml)

context.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ const (
2222
type (
2323
Context interface {
2424
Context() context.Context
25-
Cancle()
25+
SetTimeoutContext(timeout time.Duration) context.Context
26+
WithContext(runCtx context.Context)
2627
HttpServer() *HttpServer
2728
Response() *Response
2829
Request() *Request
@@ -71,7 +72,8 @@ type (
7172
}
7273

7374
HttpContext struct {
74-
context context.Context
75+
context context.Context
76+
//暂未启用
7577
cancle context.CancelFunc
7678
request *Request
7779
routerNode RouterNode
@@ -128,14 +130,30 @@ func (ctx *HttpContext) release() {
128130
ctx.startTime = time.Time{}
129131
}
130132

133+
// Context return context.Context
131134
func (ctx *HttpContext) Context() context.Context {
132135
return ctx.context
133136
}
134137

135-
func (ctx *HttpContext) Cancle() {
136-
ctx.cancle()
138+
// SetTimeoutContext set new Timeout Context
139+
// set Context & cancle
140+
// withvalue RequestID
141+
func (ctx *HttpContext) SetTimeoutContext(timeout time.Duration) context.Context {
142+
ctx.context, ctx.cancle = context.WithTimeout(context.Background(), timeout)
143+
ctx.context = context.WithValue(ctx.context, "RequestID", ctx.Request().RequestID())
144+
return ctx.context
145+
}
146+
147+
// WithContext set Context with RequestID
148+
func (ctx *HttpContext) WithContext(runCtx context.Context) {
149+
if runCtx == nil {
150+
panic("nil context")
151+
}
152+
ctx.context = runCtx
153+
ctx.context = context.WithValue(ctx.context, "RequestID", ctx.Request().RequestID())
137154
}
138155

156+
// HttpServer return HttpServer
139157
func (ctx *HttpContext) HttpServer() *HttpServer {
140158
return ctx.httpServer
141159
}

example/httpmodule/main.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ func main() {
4343
app.AppContext.Set("gstring", "gvalue")
4444
app.AppContext.Set("gint", 1)
4545

46-
app.HttpServer.ServerConfig.RequestTimeOut = 2000
47-
4846
// 开始服务
4947
port := 8080
5048
fmt.Println("dotweb.StartServer => " + strconv.Itoa(port))
@@ -60,15 +58,25 @@ func Index(ctx dotweb.Context) error {
6058
}
6159

6260
func CtxTimeOut(ctx dotweb.Context) error {
63-
fmt.Println(sleep(ctx.Context()))
61+
ctx.SetTimeoutContext(time.Second * 3)
62+
err := sleepCtx(ctx.Context())
63+
ctx.WriteString(time.Now(), err)
6464
return nil
6565
}
6666

6767
func sleep(runCtx context.Context) error {
68+
fmt.Println(runCtx.Value("RequestID"))
69+
time.Sleep(time.Second * 5)
70+
fmt.Println(time.Now(), "sleep time end")
71+
return errors.New("test")
72+
}
73+
74+
func sleepCtx(runCtx context.Context) error {
75+
fmt.Println(runCtx.Value("RequestID"))
6876
c := make(chan error, 1)
6977
go func() {
70-
time.Sleep(time.Second * 10)
71-
fmt.Println("sleep time end")
78+
time.Sleep(time.Second * 5)
79+
fmt.Println(time.Now(), "sleep time end")
7280
c <- errors.New("test")
7381
}()
7482
select {

request.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dotweb
22

33
import (
4+
"TechService/framework/crypto"
45
"io/ioutil"
56
"net/http"
67
"net/url"
@@ -11,23 +12,29 @@ type Request struct {
1112
*http.Request
1213
postBody []byte
1314
isReadBody bool
15+
requestID string
1416
}
1517

1618
//reset response attr
1719
func (req *Request) reset(r *http.Request) {
1820
req.Request = r
1921
req.isReadBody = false
22+
req.requestID = cryptos.GetGuid()
2023
}
2124

2225
func (req *Request) release() {
2326
req.Request = nil
2427
req.isReadBody = false
2528
req.postBody = nil
29+
req.requestID = ""
2630
}
2731

28-
/*
29-
* 返回查询字符串map表示
30-
*/
32+
// RequestID get unique ID with current request
33+
func (req *Request) RequestID() string {
34+
return req.requestID
35+
}
36+
37+
// QueryStrings 返回查询字符串map表示
3138
func (req *Request) QueryStrings() url.Values {
3239
return req.URL.Query()
3340
}

server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"time"
1414

1515
"compress/gzip"
16-
"context"
1716
"github.com/devfeel/dotweb/config"
1817
"github.com/devfeel/dotweb/feature"
1918
"github.com/devfeel/dotweb/logger"
@@ -284,7 +283,6 @@ func (server *HttpServer) wrapRouterHandle(handler HttpHandle, isHijack bool) Ro
284283
req.reset(r)
285284
httpCtx := server.pool.context.Get().(*HttpContext)
286285
httpCtx.reset(res, req, server, vnode.Node, vnode.Params, handler)
287-
httpCtx.context, httpCtx.cancle = context.WithTimeout(context.Background(), time.Duration(server.ServerConfig.RequestTimeOut)*time.Millisecond)
288286

289287
//gzip
290288
if server.ServerConfig.EnabledGzip {
@@ -363,7 +361,9 @@ func (server *HttpServer) wrapRouterHandle(handler HttpHandle, isHijack bool) Ro
363361
}
364362

365363
//cancle Context
366-
httpCtx.Cancle()
364+
if httpCtx.cancle != nil {
365+
httpCtx.cancle()
366+
}
367367

368368
//release response
369369
res.release()

version.MD

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
#### Version 0.3.21
44
* 新增HttpServer.ListenAndServer(addr string) 支持原生host监听
5-
* Context增加Context() & Cancle() 以通过context.Context实现请求超时机制
6-
* 配置文件Server节点增加RequestTimeOut设置,表示请求超时时间,单位毫秒,默认30000毫秒
5+
* Context增加Context() & SetTimeoutContext() & WithContext()
6+
* Request增加自动生成unique RequestID,通过RequestID()获取,默认置入dotweb.Context.context中,以RequestID为key
77
* 2017-06-14 06:30
88

99
#### Version 0.3.20

0 commit comments

Comments
 (0)