From 5ea70888fe2e40f525108650ae120588ecbe169d Mon Sep 17 00:00:00 2001 From: John Date: Tue, 13 Mar 2018 14:41:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Bghttp=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=A4=84=E7=90=86=EF=BC=8C=E5=B0=86=E8=AF=B7=E6=B1=82=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E5=85=B3=E9=97=AD=E6=93=8D=E4=BD=9C=E6=9C=BA=E5=88=B6?= =?UTF-8?q?=E6=94=B9=E8=BF=9B=E4=B8=BA=E5=BC=82=E6=AD=A5=E9=98=9F=E5=88=97?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/net/ghttp/http_server.go | 7 ++++++- g/net/ghttp/http_server_auto.go | 22 ++++++++++++++++++++++ g/net/ghttp/http_server_handler.go | 6 ++---- 3 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 g/net/ghttp/http_server_auto.go diff --git a/g/net/ghttp/http_server.go b/g/net/ghttp/http_server.go index f7fb34809..bb1a35030 100644 --- a/g/net/ghttp/http_server.go +++ b/g/net/ghttp/http_server.go @@ -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 diff --git a/g/net/ghttp/http_server_auto.go b/g/net/ghttp/http_server_auto.go new file mode 100644 index 000000000..3db2d0ced --- /dev/null +++ b/g/net/ghttp/http_server_auto.go @@ -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() + } + } + }() +} \ No newline at end of file diff --git a/g/net/ghttp/http_server_handler.go b/g/net/ghttp/http_server_handler.go index e3735c3a9..13d6ff5f1 100644 --- a/g/net/ghttp/http_server_handler.go +++ b/g/net/ghttp/http_server_handler.go @@ -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) } // 处理静态文件请求