|
@@ -1,6 +1,7 @@
|
|
|
package router
|
|
|
|
|
|
import (
|
|
|
+ "app.yhyue.com/moapp/jybase/redis"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"log"
|
|
@@ -22,8 +23,9 @@ type Manager struct {
|
|
|
// InitRouterManager 初始化系统代理路由
|
|
|
// 支持完全匹配和正则匹配
|
|
|
func InitRouterManager() (*Manager, error) {
|
|
|
+ redis.DelByCodePattern("other", "reqLimit_tripartite_*")
|
|
|
//加载规则
|
|
|
- res := db.GateWatMySql.Query(`SELECT * FROM front_proxy`)
|
|
|
+ res := db.GateWatMySql.SelectBySql(`SELECT * FROM front_proxy`)
|
|
|
if res == nil {
|
|
|
return nil, errors.New("未发现可用路由")
|
|
|
}
|
|
@@ -36,18 +38,20 @@ func InitRouterManager() (*Manager, error) {
|
|
|
// 获取路由信息
|
|
|
router := gconv.String(row["url"])
|
|
|
routerRule := &Router{
|
|
|
+ Id: gconv.Int(row["id"]),
|
|
|
Status: gconv.Int(row["status"]),
|
|
|
ReqUrl: router,
|
|
|
TimeOut: gconv.Int64(row["timeout"]),
|
|
|
Remark: gconv.String(row["remark"]),
|
|
|
Server: gconv.String(row["server"]),
|
|
|
Price: gconv.Int64(row["price"]),
|
|
|
- ReqLimit: &util.ReqLimit{
|
|
|
- Size: gconv.Int(row["pool_size"]),
|
|
|
- Key: fmt.Sprintf("reqLimit_tripartite_%d", gconv.Int(row["id"])),
|
|
|
- },
|
|
|
+ }
|
|
|
+ routerRule.ReqLimit = &util.ReqLimit{
|
|
|
+ Size: gconv.Int(row["pool_size"]),
|
|
|
+ Key: fmt.Sprintf("reqLimit_tripartite_%d", routerRule.Id),
|
|
|
}
|
|
|
routerRule.ReqLimit.Init()
|
|
|
+ routerRule.IsFree = routerRule.Price <= 0
|
|
|
// 判断路由匹配方式是完全匹配还是正则匹配 (此处逻辑参考x-web框架)
|
|
|
if regexp.QuoteMeta(router) == router {
|
|
|
routerManager.eqRouters[router] = routerRule
|
|
@@ -93,8 +97,39 @@ func (m *Manager) InfusionContext(r *ghttp.Request) (err error) {
|
|
|
}
|
|
|
router, err = m.GetRouterRule(r.URL.Path)
|
|
|
if err != nil {
|
|
|
- r.SetCtxVar(GContextKey, GCtx)
|
|
|
- return
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ appid := r.GetQuery("appid").String()
|
|
|
+ res := db.GateWatMySql.SelectBySql(`SELECT b.* FROM USER a INNER JOIN user_api b ON (a.appid=? AND b.front_proxy_id=? AND a.id=b.user_id)`, appid, router.Id)
|
|
|
+ if res == nil || len(*res) == 0 {
|
|
|
+ return NewErrorWithCode(GLOBAL_ERR_NOPERMISSION)
|
|
|
+ }
|
|
|
+ router = &Router{
|
|
|
+ Id: router.Id,
|
|
|
+ Status: router.Status,
|
|
|
+ ReqUrl: router.ReqUrl,
|
|
|
+ TimeOut: router.TimeOut,
|
|
|
+ Remark: router.Remark,
|
|
|
+ Server: router.Server,
|
|
|
+ Price: gconv.Int64((*res)[0]["price"]),
|
|
|
+ ReqLimit: router.ReqLimit,
|
|
|
+ }
|
|
|
+ rl := &util.ReqLimit{
|
|
|
+ Size: gconv.Int((*res)[0]["pool_size"]),
|
|
|
+ Key: fmt.Sprintf("reqLimit_tripartite_%d_%d", router.Id, gconv.Int((*res)[0]["user_id"])),
|
|
|
+ }
|
|
|
+ sizeKey := router.ReqLimit.Key + "_size"
|
|
|
+ if rl.Size > 0 {
|
|
|
+ router.ReqLimit = rl
|
|
|
+ //数据库中调整了某个用户的某个接口并发数,重新初始化
|
|
|
+ if redis.GetInt("other", sizeKey) != router.ReqLimit.Size {
|
|
|
+ redis.PutCKV("other", sizeKey, router.ReqLimit.Size)
|
|
|
+ router.ReqLimit.Clear()
|
|
|
+ router.ReqLimit.Init()
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ redis.Del("other", sizeKey)
|
|
|
+ rl.Clear()
|
|
|
}
|
|
|
GCtx.RouterRule = router
|
|
|
//获取session
|