This commit is contained in:
aozhiwei 2020-10-29 11:19:42 +08:00
parent 15ca2fef81
commit c5b71f4514

View File

@ -3,22 +3,46 @@ package f5
import ( import (
"net/http" "net/http"
"sync" "sync"
"sync/atomic"
"fmt" "fmt"
"q5" "q5"
) )
type HttpServer struct { type HttpServer struct {
totalRequestTimes int64
okTimes int64
pageNotFoundTimes int64
maxHandleTime int64
handlersMutex sync.RWMutex handlersMutex sync.RWMutex
handlers map[string]func(http.ResponseWriter, *http.Request) handlers map[string]func(http.ResponseWriter, *http.Request)
} }
func (this *HttpServer) Init() *HttpServer { func (this *HttpServer) Init(serviceName string, logOutputTime int32) *HttpServer {
this.handlers = make(map[string]func(http.ResponseWriter, *http.Request)) this.handlers = make(map[string]func(http.ResponseWriter, *http.Request))
http.HandleFunc("/webapp/index.php", this.dispatchRequest) http.HandleFunc("/webapp/index.php", this.dispatchRequest)
this.RegisterHandle("Ops", "selfChecking", func (w http.ResponseWriter,r *http.Request) { this.RegisterHandle("Ops", "selfChecking", func (w http.ResponseWriter,r *http.Request) {
w.Write([]byte(`{"errcode":0, "errmsg":"", "health":1, "max_rundelay": 10}`)) w.Write([]byte(`{"errcode":0, "errmsg":"", "health":1, "max_rundelay": 10}`))
}) })
SysLog().Info("HttpServer.Init") SysLog().Info("HttpServer.Init")
if logOutputTime > 0 {
Timer().AddRepeatTimer(
logOutputTime,
func (params* q5.XParams) {
params.Sender.SetString(serviceName)
},
func (params* q5.XParams) {
SysLog().Info("%s maxHandleTime:%d totalRequestTimes:%d okTimes:%d pageNotFoundTimes:%d",
params.Sender.GetString(),
this.maxHandleTime,
this.totalRequestTimes,
this.okTimes,
this.pageNotFoundTimes)
this.maxHandleTime = 0
this.okTimes = 0
this.pageNotFoundTimes = 0
})
}
return this return this
} }
@ -32,12 +56,20 @@ func (this *HttpServer) Start(listen_port int32) {
} }
func (this *HttpServer) dispatchRequest(w http.ResponseWriter, r *http.Request) { func (this *HttpServer) dispatchRequest(w http.ResponseWriter, r *http.Request) {
atomic.AddInt64(&this.totalRequestTimes, 1)
handleName := q5.Request(r, "c").GetString() + "$" + q5.Request(r, "a").GetString() handleName := q5.Request(r, "c").GetString() + "$" + q5.Request(r, "a").GetString()
handle := this.getHandle(handleName) handle := this.getHandle(handleName)
if handle != nil { if handle != nil {
beginTick := q5.GetTickCount()
handle(w, r) handle(w, r)
endTick := q5.GetTickCount()
if oldVal := atomic.LoadInt64(&this.maxHandleTime); beginTick - endTick > oldVal {
atomic.CompareAndSwapInt64(&this.maxHandleTime, oldVal, endTick - beginTick)
}
atomic.AddInt64(&this.okTimes, 1)
} else { } else {
w.Write([]byte(`{"errcode":404, "errmsg":"接口不存在"}`)) w.Write([]byte(`{"errcode":404, "errmsg":"接口不存在"}`))
atomic.AddInt64(&this.pageNotFoundTimes, 1)
} }
} }