This commit is contained in:
yangduo 2024-07-22 20:08:44 +08:00
parent 663892a380
commit b837b7e820
4 changed files with 35 additions and 74 deletions

View File

@ -1,6 +1,6 @@
{
"gamesapi_url": "https://game2006sapi-test.kingsome.cn",
"redirect_url": "https://game2006api-test.kingsome.cn/",
"redirect_url": "https://game2006api-test.kingsome.cn",
"max_request_cache": 10,
"request_over_time": 30
}

View File

@ -6,7 +6,6 @@ import (
"mt"
"net/http"
"q5"
"strings"
"time"
//. "main/global"
@ -26,7 +25,6 @@ var redirectRequest = q5.ConcurrentMap[string, *RedirectInfo]{}
func CaCheck(c *gin.Context) {
accountId := c.DefaultQuery("account_id", "")
// fmt.Println("ori context:", c)
if accountId == "" || !lockAccount(accountId) {
c.JSON(http.StatusOK, gin.H{
@ -38,19 +36,12 @@ func CaCheck(c *gin.Context) {
defer unlockAccount(accountId)
action := c.DefaultQuery("a", "")
cache, exist := requestCache.Load(accountId)
if !exist {
pcache := make([]*RedirectInfo, 0, mt.Table.Config.GetMaxCache())
cache = &pcache
}
if !strings.HasSuffix(action, "S") {
CaForward(c, "", "")
return
}
info := new(RedirectInfo)
info.ori_req = c
info.sigtime = f5.GetApp().GetRealSeconds()
@ -60,6 +51,38 @@ func CaCheck(c *gin.Context) {
if len(*cache) < int(mt.Table.Config.GetMaxCache()) {
*cache = append(*cache, info)
requestCache.Store(accountId, *cache)
unlockAccount(accountId)
for {
time.Sleep(time.Millisecond * 500)
if lockAccount(accountId) {
reqlist, _ := requestCache.Load(accountId)
if len(*reqlist) > 0 && (*reqlist)[0].trace_id == info.trace_id {
req := (*reqlist)[0]
(*reqlist) = (*reqlist)[1:]
if req.sigtime+int64(mt.Table.Config.GetById(0).GetRequestOverTime()) > f5.GetApp().GetRealSeconds() {
CaForward(req.ori_req, req.sig, req.trace_id)
} else {
req.ori_req.JSON(http.StatusOK, gin.H{
"errcode": 1004,
"errmsg": "Too many players",
})
}
if len(*reqlist) > 0 {
requestCache.Store(accountId, *reqlist)
} else {
requestCache.Delete(accountId)
}
return
}
unlockAccount(accountId)
}
}
} else {
c.JSON(http.StatusOK, gin.H{
"errcode": 1004,
@ -103,54 +126,3 @@ func VerifySig(c *gin.Context) {
"errmsg": "",
})
}
func CheckRedirect() {
for {
time.Sleep(time.Second)
deletelist := map[string]bool{}
requestCache.Range(func(account string, requests []*RedirectInfo) bool {
_, exist := redirectRequest.Load(account)
if exist {
return true
}
if !lockAccount(account) {
return true
}
var req *RedirectInfo = nil
for len(requests) > 0 {
req = requests[0]
requests = requests[1:]
if req.sigtime+int64(mt.Table.Config.GetById(0).GetRequestOverTime()) > f5.GetApp().GetRealSeconds() {
break
}
req = nil
}
if req != nil {
redirectRequest.Store(account, req)
CaForward(req.ori_req, req.sig, req.trace_id)
}
if len(requests) == 0 {
deletelist[account] = true
}
requestCache.Store(account, requests)
unlockAccount(account)
return true
})
for acc := range deletelist {
list, _ := requestCache.Load(acc)
if len(*list) == 0 {
requestCache.Delete(acc)
accountMutexmap.Delete(acc)
}
}
}
}

View File

@ -21,7 +21,7 @@ func CaForward(c *gin.Context, sig string, traceid string) {
params["trace_id"] = traceid
}
fullrequrl := mt.Table.Config.GetById(0).GetRedirectUrl() + c.Request.URL.Path
fullrequrl := mt.Table.Config.GetById(0).GetRedirectUrl() + c.Request.URL.Path[5:]
cb := func(rsp f5.HttpCliResponse) {
if rsp.GetErr() != nil {
c.JSON(http.StatusOK, gin.H{
@ -31,8 +31,7 @@ func CaForward(c *gin.Context, sig string, traceid string) {
return
}
// fmt.Println("saved ori context:", c.ori_req)
c.JSON(http.StatusOK, rsp.GetRawData())
c.String(http.StatusOK, rsp.GetRawData())
}
switch c.Request.Method {
@ -49,12 +48,4 @@ func CaForward(c *gin.Context, sig string, traceid string) {
q5.GetPostBody(c.Request),
cb)
}
if sig != "" {
account := c.DefaultQuery("account_id", "")
v, exist := redirectRequest.Load(account)
if exist && (*v).trace_id == traceid {
redirectRequest.Delete(account)
}
}
}

View File

@ -17,8 +17,6 @@ func (this *routerMgr) Init() {
redirectGroup.Any("check", middleware.VerifySig)
f5.GetSysLog().Info("routerMgr.init")
go middleware.CheckRedirect()
}
func (this *routerMgr) UnInit() {