完成ghttp.Server热重启特性开发以及测试工作

This commit is contained in:
John
2018-05-13 14:23:14 +08:00
parent 85020dfcc2
commit d8bd344b66
5 changed files with 96 additions and 36 deletions

View File

@ -0,0 +1,57 @@
// 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.
// pprof封装.
package ghttp
import (
"strings"
"gitee.com/johng/gf/g/os/gview"
)
// 用于服务管理的对象
type utilAdmin struct {}
// 服务管理首页
func (p *utilAdmin) Index(r *Request) {
data := map[string]interface{}{
"uri" : strings.TrimRight(r.URL.Path, "/"),
}
buffer, _ := gview.ParseContent(`
<html>
<head>
<title>gf ghttp admin</title>
</head>
<body>
<p><a href="{{$.uri}}/restart">restart</a></p>
<p><a href="{{$.uri}}/shutdown">shutdown</a></p>
</body>
</html>
`, data)
r.Response.Write(buffer)
}
// 服务重启
func (p *utilAdmin) Restart(r *Request) {
r.Response.Write("restart server")
r.Server.Restart()
}
// 服务关闭
func (p *utilAdmin) Shutdown(r *Request) {
r.Response.Write("shutdown server")
r.Server.Shutdown()
}
// 开启服务管理支持
func (s *Server) EnableAdmin(pattern...string) {
p := "/debug/admin"
if len(pattern) > 0 {
p = pattern[0]
}
s.BindObject(p, &utilAdmin{})
}

View File

@ -1,25 +0,0 @@
// 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)
s.callHookHandler(r, "BeforeClose")
// 关闭当前会话的Cookie
r.Cookie.Close()
// 更新Session会话超时时间
r.Session.UpdateExpire()
s.callHookHandler(r, "AfterClose")
}
}
}()
}

View File

@ -16,8 +16,8 @@ import (
"net/url"
"net/http"
"gitee.com/johng/gf/g/os/gfile"
"gitee.com/johng/gf/g/encoding/ghtml"
"gitee.com/johng/gf/g/os/gtime"
"gitee.com/johng/gf/g/encoding/ghtml"
)
// 默认HTTP Server处理入口http包底层默认使用了gorutine异步处理请求所以这里不再异步执行
@ -169,3 +169,20 @@ func (s *Server)listDir(r *Request, f http.File) {
}
r.Response.Write("</pre>\n")
}
// 开启异步队列处理循环该异步线程与Server同生命周期
func (s *Server) startCloseQueueLoop() {
go func() {
for {
if v := s.closeQueue.PopFront(); v != nil {
r := v.(*Request)
s.callHookHandler(r, "BeforeClose")
// 关闭当前会话的Cookie
r.Cookie.Close()
// 更新Session会话超时时间
r.Session.UpdateExpire()
s.callHookHandler(r, "AfterClose")
}
}
}()
}

View File

@ -11,13 +11,13 @@ import (
"strings"
runpprof "runtime/pprof"
netpprof "net/http/pprof"
"gitee.com/johng/gf/g/frame/gins"
"gitee.com/johng/gf/g/os/gview"
)
// 用于pprof的对象
type utilpprof struct {}
type utilPprof struct {}
func (p *utilpprof) Index(r *Request) {
func (p *utilPprof) Index(r *Request) {
profiles := runpprof.Profiles()
action := r.Get("action")
data := map[string]interface{}{
@ -25,8 +25,7 @@ func (p *utilpprof) Index(r *Request) {
"profiles" : profiles,
}
if len(action) == 0 {
view := gins.View()
buffer, _ := view.ParseContent(`
buffer, _ := gview.ParseContent(`
<html>
<head>
<title>gf ghttp pprof</title>
@ -52,19 +51,19 @@ func (p *utilpprof) Index(r *Request) {
}
}
func (p *utilpprof) Cmdline(r *Request) {
func (p *utilPprof) Cmdline(r *Request) {
netpprof.Cmdline(r.Response.Writer, &r.Request)
}
func (p *utilpprof) Profile(r *Request) {
func (p *utilPprof) Profile(r *Request) {
netpprof.Profile(r.Response.Writer, &r.Request)
}
func (p *utilpprof) Symbol(r *Request) {
func (p *utilPprof) Symbol(r *Request) {
netpprof.Symbol(r.Response.Writer, &r.Request)
}
func (p *utilpprof) Trace(r *Request) {
func (p *utilPprof) Trace(r *Request) {
netpprof.Trace(r.Response.Writer, &r.Request)
}
@ -74,7 +73,7 @@ func (s *Server) EnablePprof(pattern...string) {
if len(pattern) > 0 {
p = pattern[0]
}
up := &utilpprof{}
up := &utilPprof{}
_, _, uri, _ := s.parsePattern(p)
uri = strings.TrimRight(uri, "/")
s.BindHandler(uri + "/*action", up.Index)

View File

@ -0,0 +1,12 @@
package main
import (
"gitee.com/johng/gf/g"
)
func main() {
s := g.Server()
s.EnableAdmin()
s.SetPort(8199)
s.Run()
}