修改
This commit is contained in:
parent
663892a380
commit
b837b7e820
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"gamesapi_url": "https://game2006sapi-test.kingsome.cn",
|
"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,
|
"max_request_cache": 10,
|
||||||
"request_over_time": 30
|
"request_over_time": 30
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"mt"
|
"mt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"q5"
|
"q5"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
//. "main/global"
|
//. "main/global"
|
||||||
@ -26,7 +25,6 @@ var redirectRequest = q5.ConcurrentMap[string, *RedirectInfo]{}
|
|||||||
|
|
||||||
func CaCheck(c *gin.Context) {
|
func CaCheck(c *gin.Context) {
|
||||||
accountId := c.DefaultQuery("account_id", "")
|
accountId := c.DefaultQuery("account_id", "")
|
||||||
// fmt.Println("ori context:", c)
|
|
||||||
|
|
||||||
if accountId == "" || !lockAccount(accountId) {
|
if accountId == "" || !lockAccount(accountId) {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
@ -38,19 +36,12 @@ func CaCheck(c *gin.Context) {
|
|||||||
|
|
||||||
defer unlockAccount(accountId)
|
defer unlockAccount(accountId)
|
||||||
|
|
||||||
action := c.DefaultQuery("a", "")
|
|
||||||
|
|
||||||
cache, exist := requestCache.Load(accountId)
|
cache, exist := requestCache.Load(accountId)
|
||||||
if !exist {
|
if !exist {
|
||||||
pcache := make([]*RedirectInfo, 0, mt.Table.Config.GetMaxCache())
|
pcache := make([]*RedirectInfo, 0, mt.Table.Config.GetMaxCache())
|
||||||
cache = &pcache
|
cache = &pcache
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.HasSuffix(action, "S") {
|
|
||||||
CaForward(c, "", "")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
info := new(RedirectInfo)
|
info := new(RedirectInfo)
|
||||||
info.ori_req = c
|
info.ori_req = c
|
||||||
info.sigtime = f5.GetApp().GetRealSeconds()
|
info.sigtime = f5.GetApp().GetRealSeconds()
|
||||||
@ -60,6 +51,38 @@ func CaCheck(c *gin.Context) {
|
|||||||
if len(*cache) < int(mt.Table.Config.GetMaxCache()) {
|
if len(*cache) < int(mt.Table.Config.GetMaxCache()) {
|
||||||
*cache = append(*cache, info)
|
*cache = append(*cache, info)
|
||||||
requestCache.Store(accountId, *cache)
|
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 {
|
} else {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"errcode": 1004,
|
"errcode": 1004,
|
||||||
@ -103,54 +126,3 @@ func VerifySig(c *gin.Context) {
|
|||||||
"errmsg": "",
|
"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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -21,7 +21,7 @@ func CaForward(c *gin.Context, sig string, traceid string) {
|
|||||||
params["trace_id"] = traceid
|
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) {
|
cb := func(rsp f5.HttpCliResponse) {
|
||||||
if rsp.GetErr() != nil {
|
if rsp.GetErr() != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
@ -31,8 +31,7 @@ func CaForward(c *gin.Context, sig string, traceid string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// fmt.Println("saved ori context:", c.ori_req)
|
c.String(http.StatusOK, rsp.GetRawData())
|
||||||
c.JSON(http.StatusOK, rsp.GetRawData())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch c.Request.Method {
|
switch c.Request.Method {
|
||||||
@ -49,12 +48,4 @@ func CaForward(c *gin.Context, sig string, traceid string) {
|
|||||||
q5.GetPostBody(c.Request),
|
q5.GetPostBody(c.Request),
|
||||||
cb)
|
cb)
|
||||||
}
|
}
|
||||||
|
|
||||||
if sig != "" {
|
|
||||||
account := c.DefaultQuery("account_id", "")
|
|
||||||
v, exist := redirectRequest.Load(account)
|
|
||||||
if exist && (*v).trace_id == traceid {
|
|
||||||
redirectRequest.Delete(account)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,6 @@ func (this *routerMgr) Init() {
|
|||||||
redirectGroup.Any("check", middleware.VerifySig)
|
redirectGroup.Any("check", middleware.VerifySig)
|
||||||
|
|
||||||
f5.GetSysLog().Info("routerMgr.init")
|
f5.GetSysLog().Info("routerMgr.init")
|
||||||
|
|
||||||
go middleware.CheckRedirect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *routerMgr) UnInit() {
|
func (this *routerMgr) UnInit() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user