改进ghttp请求处理,将请求资源关闭操作机制改进为异步队列处理

This commit is contained in:
John
2018-03-13 14:41:49 +08:00
parent 7497c37204
commit 5ea70888fe
3 changed files with 30 additions and 5 deletions

View File

@ -21,6 +21,7 @@ import (
"gitee.com/johng/gf/g/net/grouter"
"gitee.com/johng/gf/g/util/gidgen"
"gitee.com/johng/gf/g/container/gmap"
"gitee.com/johng/gf/g/container/gqueue"
)
const (
@ -40,6 +41,7 @@ type Server struct {
handlerMap HandlerMap // 所有注册的回调函数
methodsMap map[string]bool // 所有支持的HTTP Method
idgen *gidgen.Gen // 请求ID生成器
closeQueue *gqueue.Queue // 请求结束的关闭队列(存放的是需要异步关闭处理的*Request对象)
Router *grouter.Router // 路由管理对象
}
@ -73,7 +75,8 @@ func GetServer(names...string) (*Server) {
name : name,
handlerMap : make(HandlerMap),
methodsMap : make(map[string]bool),
idgen : gidgen.New(20000),
idgen : gidgen.New(50000),
closeQueue : gqueue.New(),
Router : grouter.New(),
}
for _, v := range strings.Split(gHTTP_METHODS, ",") {
@ -103,6 +106,8 @@ func (s *Server) Run() error {
IdleTimeout : s.config.IdleTimeout,
MaxHeaderBytes : s.config.MaxHeaderBytes,
}
// 开启异步处理队列处理循环
s.startCloseQueueLoop()
// 执行端口监听
if err := s.server.ListenAndServe(); err != nil {
return err

View File

@ -0,0 +1,22 @@
// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
package ghttp
// 开启异步队列处理循环该异步线程与Server同生命周期
func (s *Server) startCloseQueueLoop() {
go func() {
for {
if v := s.closeQueue.PopFront(); v != nil {
r := v.(*Request)
// 关闭当前会话的Cookie
r.Cookie.Close()
// 更新Sssion会话超时时间
r.Session.UpdateExpire()
}
}
}()
}

View File

@ -85,10 +85,8 @@ func (s *Server)callHandler(h *HandlerItem, r *Request) {
// 输出缓冲区
r.Response.OutputBuffer()
// 关闭当前会话的Cookie
go r.Cookie.Close()
// 更新Sssion会话超时时间
go r.Session.UpdateExpire()
// 将Request对象指针丢到队列中异步处理
s.closeQueue.PushBack(r)
}
// 处理静态文件请求