diff --git a/bin/gamesapi/config/config.json b/bin/gamesapi/config/config.json index bc09a7e3..7bf05787 100644 --- a/bin/gamesapi/config/config.json +++ b/bin/gamesapi/config/config.json @@ -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 } diff --git a/server/gamesapi/middleware/caauth.go b/server/gamesapi/middleware/caauth.go index 8d3dfc36..21e5f156 100644 --- a/server/gamesapi/middleware/caauth.go +++ b/server/gamesapi/middleware/caauth.go @@ -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) - } - } - } -} diff --git a/server/gamesapi/middleware/caforward.go b/server/gamesapi/middleware/caforward.go index 8d6f166c..4c5520eb 100644 --- a/server/gamesapi/middleware/caforward.go +++ b/server/gamesapi/middleware/caforward.go @@ -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) - } - } } diff --git a/server/gamesapi/router/routermgr.go b/server/gamesapi/router/routermgr.go index 22094baa..ab334f7c 100644 --- a/server/gamesapi/router/routermgr.go +++ b/server/gamesapi/router/routermgr.go @@ -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() {