From 99bb08b21c1854c44abc23abf129bf10ba82975b Mon Sep 17 00:00:00 2001 From: yangduo Date: Thu, 25 Jul 2024 13:26:30 +0800 Subject: [PATCH] add log --- server/gamesapi/middleware/caforward.go | 81 ++++++++++++++----------- server/gamesapi/service/sapi_forward.go | 79 ++++++++++++++++++++---- server/gamesapi/service/servicemgr.go | 2 + 3 files changed, 113 insertions(+), 49 deletions(-) diff --git a/server/gamesapi/middleware/caforward.go b/server/gamesapi/middleware/caforward.go index 7cb6f442..65a7b5d6 100644 --- a/server/gamesapi/middleware/caforward.go +++ b/server/gamesapi/middleware/caforward.go @@ -1,19 +1,20 @@ package middleware import ( + "bytes" + "errors" "f5" + "io/ioutil" + "jccommon" + "main/service" "mt" "net/http" - "q5" - "main/service" - "jccommon" - "io/ioutil" - "bytes" net_url "net/url" + "q5" "strings" - "errors" - "github.com/google/uuid" + "github.com/gin-gonic/gin" + "github.com/google/uuid" ) func CaForward(c *gin.Context) { @@ -28,6 +29,11 @@ func CaForward(c *gin.Context) { } cLock := service.SApiForward.AcquireLock(accountId) + if cLock == nil { + f5.RspErr(c, 500, "system busy") + c.Abort() + return + } defer service.SApiForward.ReleaseLock(cLock) service.SApiForward.IncTotalTimes() beginTick := q5.GetTickCount() @@ -55,39 +61,42 @@ func CaForward(c *gin.Context) { var httpRequest *http.Request var createErr error switch strings.ToUpper(c.Request.Method) { - case "GET": { - service.SApiForward.IncGetTimes() - u.Set("__sign", service.SApiForward.Sign(params, nonce, nowTime, "")) - newUrl += u.Encode() - httpRequest, createErr = http.NewRequest("GET", newUrl, nil) - if !f5.IsOnlineEnv() { - f5.GetSysLog().Info("CaForward method:%s newUrl:%s ", c.Request.Method, newUrl) - } - } - case "POST": { - service.SApiForward.IncPostTimes() - if postData, err := c.GetRawData(); err == nil { - u.Set("__sign", service.SApiForward.Sign(params, nonce, nowTime, string(postData))) + case "GET": + { + service.SApiForward.IncGetTimes() + u.Set("__sign", service.SApiForward.Sign(params, nonce, nowTime, "")) newUrl += u.Encode() - httpRequest, createErr = http.NewRequest("POST", newUrl, bytes.NewBuffer(postData)) - contentType := c.GetHeader("Content-Type") - if contentType != "" { - httpRequest.Header.Set("Content-Type", contentType) - } + httpRequest, createErr = http.NewRequest("GET", newUrl, nil) if !f5.IsOnlineEnv() { - f5.GetSysLog().Info("CaForward method:%s newUrl:%s Content-Type:%s postData:%s", - c.Request.Method, - newUrl, - contentType, - postData) + f5.GetSysLog().Info("CaForward method:%s newUrl:%s ", c.Request.Method, newUrl) } - } else { - createErr = err } - } - default: { - createErr = errors.New("method error") - } + case "POST": + { + service.SApiForward.IncPostTimes() + if postData, err := c.GetRawData(); err == nil { + u.Set("__sign", service.SApiForward.Sign(params, nonce, nowTime, string(postData))) + newUrl += u.Encode() + httpRequest, createErr = http.NewRequest("POST", newUrl, bytes.NewBuffer(postData)) + contentType := c.GetHeader("Content-Type") + if contentType != "" { + httpRequest.Header.Set("Content-Type", contentType) + } + if !f5.IsOnlineEnv() { + f5.GetSysLog().Info("CaForward method:%s newUrl:%s Content-Type:%s postData:%s", + c.Request.Method, + newUrl, + contentType, + postData) + } + } else { + createErr = err + } + } + default: + { + createErr = errors.New("method error") + } } if createErr != nil { service.SApiForward.IncCreateErrTimes() diff --git a/server/gamesapi/service/sapi_forward.go b/server/gamesapi/service/sapi_forward.go index 86ed3158..4d3f8021 100644 --- a/server/gamesapi/service/sapi_forward.go +++ b/server/gamesapi/service/sapi_forward.go @@ -1,25 +1,37 @@ package service import ( + "f5" + "mt" "q5" "sync" - "mt" "sync/atomic" + "time" ) type sApiForward struct { - userCache []*SApiForwardLockCache + userCache []*SApiForwardLockCache + insessTimes int32 + total int32 + getTimes int32 + postTimes int32 + createErrTimes int32 + doErrTimes int32 + okTimes int32 + readRspErrTimes int32 + refuseTimes int32 + maxCostTime int64 } type SApiForwardLockCache struct { - lock *sync.Mutex + lock *sync.Mutex userHash *map[string]*SApiForwardLock } type SApiForwardLock struct { accountId string lockTimes int32 - lock *sync.Mutex + lock *sync.Mutex } func (this *sApiForward) init() { @@ -37,10 +49,11 @@ func (this *sApiForward) unInit() { func (this *sApiForward) AcquireLock(accountId string) *SApiForwardLock { crc32 := q5.Crc32(accountId) - c := this.userCache[int64(crc32) % int64(len(this.userCache))] + c := this.userCache[int64(crc32)%int64(len(this.userCache))] u := this.getOrCreate(c, accountId) if atomic.AddInt32(&u.lockTimes, 1) > mt.Table.Config.GetMaxConcurrentNum() { atomic.AddInt32(&u.lockTimes, -1) + this.IncRefuseTimes() return nil } u.lock.Lock() @@ -53,43 +66,48 @@ func (this *sApiForward) ReleaseLock(l *SApiForwardLock) { } func (this *sApiForward) IncInvalidSessionTimes() { - + atomic.AddInt32(&this.insessTimes, 1) } func (this *sApiForward) IncTotalTimes() { - + atomic.AddInt32(&this.total, 1) } func (this *sApiForward) IncGetTimes() { - + atomic.AddInt32(&this.getTimes, 1) } func (this *sApiForward) IncPostTimes() { - + atomic.AddInt32(&this.postTimes, 1) } func (this *sApiForward) IncCreateErrTimes() { - + atomic.AddInt32(&this.createErrTimes, 1) } func (this *sApiForward) IncDoErrTimes() { + atomic.AddInt32(&this.doErrTimes, 1) } func (this *sApiForward) IncOkTimes() { + atomic.AddInt32(&this.okTimes, 1) } func (this *sApiForward) IncReadRspErrTimes() { + atomic.AddInt32(&this.readRspErrTimes, 1) } func (this *sApiForward) IncRefuseTimes() { - + atomic.AddInt32(&this.refuseTimes, 1) } func (this *sApiForward) UpdateCostTime(costTime int64) { - + if this.maxCostTime < costTime { + this.maxCostTime = costTime + } } func (this *sApiForward) getOrCreate(c *SApiForwardLockCache, accountId string) *SApiForwardLock { @@ -107,7 +125,7 @@ func (this *sApiForward) getOrCreate(c *SApiForwardLockCache, accountId string) func (this *sApiForward) Sign(params []*[]string, nonce string, timeStamp int64, postData string) string { signData := "" - q5.Sort(params, func (a *[]string, b *[]string) bool { + q5.Sort(params, func(a *[]string, b *[]string) bool { return (*a)[0] < (*b)[0] }) for _, v := range params { @@ -116,3 +134,38 @@ func (this *sApiForward) Sign(params []*[]string, nonce string, timeStamp int64, signData += nonce + q5.ToString(timeStamp) + postData + mt.Table.Config.GetRedirectSecretKey() return q5.Md5Str(signData) } + +func (this *sApiForward) outputMonitorLog() { + logtimes := 0 + for { + f5.GetSysLog().Info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") + f5.GetSysLog().Info("total:%d, invalid_session:%d, get:%d,post:%d, create_error:%d, do_error:%d, ok:%d, read_rsp_err:%d, refuse:%d, max_cost_time:%d", + this.total, + this.insessTimes, + this.getTimes, + this.postTimes, + this.createErrTimes, + this.doErrTimes, + this.okTimes, + this.readRspErrTimes, + this.refuseTimes, + this.maxCostTime) + f5.GetSysLog().Info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") + + logtimes++ + if logtimes > 6 { + logtimes = 0 + this.insessTimes = 0 + this.total = 0 + this.getTimes = 0 + this.postTimes = 0 + this.createErrTimes = 0 + this.doErrTimes = 0 + this.okTimes = 0 + this.readRspErrTimes = 0 + this.refuseTimes = 0 + this.maxCostTime = 0 + } + time.Sleep(time.Second * 10) + } +} diff --git a/server/gamesapi/service/servicemgr.go b/server/gamesapi/service/servicemgr.go index 24b21e32..acd67915 100644 --- a/server/gamesapi/service/servicemgr.go +++ b/server/gamesapi/service/servicemgr.go @@ -6,6 +6,8 @@ type serviceMgr struct { func (this *serviceMgr) Init() { SApiForward = new(sApiForward) SApiForward.init() + + go SApiForward.outputMonitorLog() } func (this *serviceMgr) UnInit() {