This commit is contained in:
yangduo 2024-07-25 13:26:30 +08:00
parent f0c220f2ea
commit 99bb08b21c
3 changed files with 113 additions and 49 deletions

View File

@ -1,19 +1,20 @@
package middleware package middleware
import ( import (
"bytes"
"errors"
"f5" "f5"
"io/ioutil"
"jccommon"
"main/service"
"mt" "mt"
"net/http" "net/http"
"q5"
"main/service"
"jccommon"
"io/ioutil"
"bytes"
net_url "net/url" net_url "net/url"
"q5"
"strings" "strings"
"errors"
"github.com/google/uuid"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid"
) )
func CaForward(c *gin.Context) { func CaForward(c *gin.Context) {
@ -28,6 +29,11 @@ func CaForward(c *gin.Context) {
} }
cLock := service.SApiForward.AcquireLock(accountId) cLock := service.SApiForward.AcquireLock(accountId)
if cLock == nil {
f5.RspErr(c, 500, "system busy")
c.Abort()
return
}
defer service.SApiForward.ReleaseLock(cLock) defer service.SApiForward.ReleaseLock(cLock)
service.SApiForward.IncTotalTimes() service.SApiForward.IncTotalTimes()
beginTick := q5.GetTickCount() beginTick := q5.GetTickCount()
@ -55,39 +61,42 @@ func CaForward(c *gin.Context) {
var httpRequest *http.Request var httpRequest *http.Request
var createErr error var createErr error
switch strings.ToUpper(c.Request.Method) { switch strings.ToUpper(c.Request.Method) {
case "GET": { case "GET":
service.SApiForward.IncGetTimes() {
u.Set("__sign", service.SApiForward.Sign(params, nonce, nowTime, "")) service.SApiForward.IncGetTimes()
newUrl += u.Encode() u.Set("__sign", service.SApiForward.Sign(params, nonce, nowTime, ""))
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)))
newUrl += u.Encode() newUrl += u.Encode()
httpRequest, createErr = http.NewRequest("POST", newUrl, bytes.NewBuffer(postData)) httpRequest, createErr = http.NewRequest("GET", newUrl, nil)
contentType := c.GetHeader("Content-Type")
if contentType != "" {
httpRequest.Header.Set("Content-Type", contentType)
}
if !f5.IsOnlineEnv() { if !f5.IsOnlineEnv() {
f5.GetSysLog().Info("CaForward method:%s newUrl:%s Content-Type:%s postData:%s", f5.GetSysLog().Info("CaForward method:%s newUrl:%s ", c.Request.Method, newUrl)
c.Request.Method,
newUrl,
contentType,
postData)
} }
} else {
createErr = err
} }
} case "POST":
default: { {
createErr = errors.New("method error") 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 { if createErr != nil {
service.SApiForward.IncCreateErrTimes() service.SApiForward.IncCreateErrTimes()

View File

@ -1,25 +1,37 @@
package service package service
import ( import (
"f5"
"mt"
"q5" "q5"
"sync" "sync"
"mt"
"sync/atomic" "sync/atomic"
"time"
) )
type sApiForward struct { 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 { type SApiForwardLockCache struct {
lock *sync.Mutex lock *sync.Mutex
userHash *map[string]*SApiForwardLock userHash *map[string]*SApiForwardLock
} }
type SApiForwardLock struct { type SApiForwardLock struct {
accountId string accountId string
lockTimes int32 lockTimes int32
lock *sync.Mutex lock *sync.Mutex
} }
func (this *sApiForward) init() { func (this *sApiForward) init() {
@ -37,10 +49,11 @@ func (this *sApiForward) unInit() {
func (this *sApiForward) AcquireLock(accountId string) *SApiForwardLock { func (this *sApiForward) AcquireLock(accountId string) *SApiForwardLock {
crc32 := q5.Crc32(accountId) 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) u := this.getOrCreate(c, accountId)
if atomic.AddInt32(&u.lockTimes, 1) > mt.Table.Config.GetMaxConcurrentNum() { if atomic.AddInt32(&u.lockTimes, 1) > mt.Table.Config.GetMaxConcurrentNum() {
atomic.AddInt32(&u.lockTimes, -1) atomic.AddInt32(&u.lockTimes, -1)
this.IncRefuseTimes()
return nil return nil
} }
u.lock.Lock() u.lock.Lock()
@ -53,43 +66,48 @@ func (this *sApiForward) ReleaseLock(l *SApiForwardLock) {
} }
func (this *sApiForward) IncInvalidSessionTimes() { func (this *sApiForward) IncInvalidSessionTimes() {
atomic.AddInt32(&this.insessTimes, 1)
} }
func (this *sApiForward) IncTotalTimes() { func (this *sApiForward) IncTotalTimes() {
atomic.AddInt32(&this.total, 1)
} }
func (this *sApiForward) IncGetTimes() { func (this *sApiForward) IncGetTimes() {
atomic.AddInt32(&this.getTimes, 1)
} }
func (this *sApiForward) IncPostTimes() { func (this *sApiForward) IncPostTimes() {
atomic.AddInt32(&this.postTimes, 1)
} }
func (this *sApiForward) IncCreateErrTimes() { func (this *sApiForward) IncCreateErrTimes() {
atomic.AddInt32(&this.createErrTimes, 1)
} }
func (this *sApiForward) IncDoErrTimes() { func (this *sApiForward) IncDoErrTimes() {
atomic.AddInt32(&this.doErrTimes, 1)
} }
func (this *sApiForward) IncOkTimes() { func (this *sApiForward) IncOkTimes() {
atomic.AddInt32(&this.okTimes, 1)
} }
func (this *sApiForward) IncReadRspErrTimes() { func (this *sApiForward) IncReadRspErrTimes() {
atomic.AddInt32(&this.readRspErrTimes, 1)
} }
func (this *sApiForward) IncRefuseTimes() { func (this *sApiForward) IncRefuseTimes() {
atomic.AddInt32(&this.refuseTimes, 1)
} }
func (this *sApiForward) UpdateCostTime(costTime int64) { func (this *sApiForward) UpdateCostTime(costTime int64) {
if this.maxCostTime < costTime {
this.maxCostTime = costTime
}
} }
func (this *sApiForward) getOrCreate(c *SApiForwardLockCache, accountId string) *SApiForwardLock { 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 { func (this *sApiForward) Sign(params []*[]string, nonce string, timeStamp int64, postData string) string {
signData := "" signData := ""
q5.Sort(params, func (a *[]string, b *[]string) bool { q5.Sort(params, func(a *[]string, b *[]string) bool {
return (*a)[0] < (*b)[0] return (*a)[0] < (*b)[0]
}) })
for _, v := range params { 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() signData += nonce + q5.ToString(timeStamp) + postData + mt.Table.Config.GetRedirectSecretKey()
return q5.Md5Str(signData) 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)
}
}

View File

@ -6,6 +6,8 @@ type serviceMgr struct {
func (this *serviceMgr) Init() { func (this *serviceMgr) Init() {
SApiForward = new(sApiForward) SApiForward = new(sApiForward)
SApiForward.init() SApiForward.init()
go SApiForward.outputMonitorLog()
} }
func (this *serviceMgr) UnInit() { func (this *serviceMgr) UnInit() {