From 745ceba9aa71b2daa2af426a36401c0463ecc817 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 5 Sep 2024 10:39:27 +0800 Subject: [PATCH] 1 --- server/mqproxy/app/app.go | 56 ++++++ server/mqproxy/app/export.go | 12 ++ server/mqproxy/common/types.go | 11 ++ server/mqproxy/constant/constant.go | 13 ++ server/mqproxy/controller/export.go | 5 + server/mqproxy/global/global.go | 59 +++++++ server/mqproxy/go.mod | 58 +++++++ server/mqproxy/go.sum | 105 +++++++++++ server/mqproxy/initialize/enter.go | 13 ++ server/mqproxy/main.go | 9 + server/mqproxy/makefile | 17 ++ server/mqproxy/middleware/caauth.go | 4 + server/mqproxy/middleware/caforward.go | 134 ++++++++++++++ server/mqproxy/mt/ConfDb.go | 15 ++ server/mqproxy/mt/Config.go | 56 ++++++ server/mqproxy/mt/GamesapiCluster.go | 34 ++++ server/mqproxy/mt/export.go | 29 ++++ server/mqproxy/mtb/mtb.auto_gen.go | 213 +++++++++++++++++++++++ server/mqproxy/proto/mt.proto | 33 ++++ server/mqproxy/proto/protoc-gen.bat | 2 + server/mqproxy/router/export.go | 12 ++ server/mqproxy/router/routermgr.go | 23 +++ server/mqproxy/service/export.go | 13 ++ server/mqproxy/service/sapi_forward.go | 230 +++++++++++++++++++++++++ server/mqproxy/service/servicemgr.go | 15 ++ 25 files changed, 1171 insertions(+) create mode 100644 server/mqproxy/app/app.go create mode 100644 server/mqproxy/app/export.go create mode 100644 server/mqproxy/common/types.go create mode 100644 server/mqproxy/constant/constant.go create mode 100644 server/mqproxy/controller/export.go create mode 100644 server/mqproxy/global/global.go create mode 100644 server/mqproxy/go.mod create mode 100644 server/mqproxy/go.sum create mode 100644 server/mqproxy/initialize/enter.go create mode 100644 server/mqproxy/main.go create mode 100644 server/mqproxy/makefile create mode 100644 server/mqproxy/middleware/caauth.go create mode 100644 server/mqproxy/middleware/caforward.go create mode 100644 server/mqproxy/mt/ConfDb.go create mode 100644 server/mqproxy/mt/Config.go create mode 100644 server/mqproxy/mt/GamesapiCluster.go create mode 100644 server/mqproxy/mt/export.go create mode 100644 server/mqproxy/mtb/mtb.auto_gen.go create mode 100644 server/mqproxy/proto/mt.proto create mode 100644 server/mqproxy/proto/protoc-gen.bat create mode 100644 server/mqproxy/router/export.go create mode 100644 server/mqproxy/router/routermgr.go create mode 100644 server/mqproxy/service/export.go create mode 100644 server/mqproxy/service/sapi_forward.go create mode 100644 server/mqproxy/service/servicemgr.go diff --git a/server/mqproxy/app/app.go b/server/mqproxy/app/app.go new file mode 100644 index 00000000..492d62e5 --- /dev/null +++ b/server/mqproxy/app/app.go @@ -0,0 +1,56 @@ +package app + +import ( + "f5" + "main/constant" + "main/mt" +) + +type app struct { + initCb func() + unInitCb func() +} + +func (this *app) GetPkgName() string { + return "gamesapi" +} + +func (this *app) GetHttpListenPort() int32 { + return mt.Table.GamesapiCluster.GetHttpListenPort() +} + +func (this *app) Run(initCb func(), unInitCb func()) { + this.initCb = initCb + this.unInitCb = unInitCb + f5.Run(this) +} + +func (this *app) Init() { + f5.LoadMetaTable(mt.Table) + this.registerDataSources() + this.initCb() +} + +func (this *app) UnInit() { + this.unInitCb() +} + +func (this *app) Update() { +} + +func (this *app) registerDataSources() { + f5.GetGoStyleDb().RegisterDataSource( + constant.CONF_DB, + mt.Table.ConfDb.GetById(0).GetHost(), + mt.Table.ConfDb.GetById(0).GetPort(), + mt.Table.ConfDb.GetById(0).GetUser(), + mt.Table.ConfDb.GetById(0).GetPasswd(), + mt.Table.ConfDb.GetById(0).GetDatabase(), + 1, + mt.Table.ConfDb.GetById(0).GetMaxOpenConns(), + mt.Table.ConfDb.GetById(0).GetMaxIdleConns()) +} + +func (this *app) HasTask() bool { + return false +} diff --git a/server/mqproxy/app/export.go b/server/mqproxy/app/export.go new file mode 100644 index 00000000..668918d5 --- /dev/null +++ b/server/mqproxy/app/export.go @@ -0,0 +1,12 @@ +package app + +import ( + "main/constant" + "main/global" +) + +var _app = new(app) + +func init() { + global.RegModule(constant.APP_MODULE_IDX, _app) +} diff --git a/server/mqproxy/common/types.go b/server/mqproxy/common/types.go new file mode 100644 index 00000000..e8696e44 --- /dev/null +++ b/server/mqproxy/common/types.go @@ -0,0 +1,11 @@ +package common + +type App interface { + Run(func(), func()) +} + +type RouterGroup interface { +} + +type ServiceMgr interface { +} diff --git a/server/mqproxy/constant/constant.go b/server/mqproxy/constant/constant.go new file mode 100644 index 00000000..e281935b --- /dev/null +++ b/server/mqproxy/constant/constant.go @@ -0,0 +1,13 @@ +package constant + +const ( + CONF_DB = "confdb" +) + +const ( + APP_MODULE_IDX = iota + ROUTER_MODULE_IDX + CONTROLLER_MGR_MODULE_IDX + SERVICE_MGR_MODULE_IDX + MAX_MODULE_IDX +) diff --git a/server/mqproxy/controller/export.go b/server/mqproxy/controller/export.go new file mode 100644 index 00000000..6ea7e9ae --- /dev/null +++ b/server/mqproxy/controller/export.go @@ -0,0 +1,5 @@ +package controller + +import ( + +) diff --git a/server/mqproxy/global/global.go b/server/mqproxy/global/global.go new file mode 100644 index 00000000..8fe71005 --- /dev/null +++ b/server/mqproxy/global/global.go @@ -0,0 +1,59 @@ +package global + +import ( + "fmt" + "main/common" + "main/constant" + "q5" +) + +var modules [constant.MAX_MODULE_IDX]q5.Module +var initOrders = []int32{ + constant.SERVICE_MGR_MODULE_IDX, + constant.ROUTER_MODULE_IDX, +} + +var app common.App +var serviceMgr common.ServiceMgr + +func GetApp() common.App { + return app +} + +func GetServiceMgr() common.ServiceMgr { + return serviceMgr +} + +func RegModule(idx int32, m q5.Module) { + fmt.Printf("RegModule module %d\n", idx) + modules[idx] = m + switch idx { + case constant.APP_MODULE_IDX: + { + app = m.(common.App) + } + case constant.SERVICE_MGR_MODULE_IDX: + { + serviceMgr = m.(common.ServiceMgr) + } + case constant.ROUTER_MODULE_IDX: + default: + { + panic("unknow module") + } + } +} + +func InitModules() { + for _, val := range initOrders { + fmt.Printf("init module %d\n", val) + modules[val].Init() + } +} + +func UnInitModules() { + for _, val := range initOrders { + fmt.Printf("unInit module %d", val) + modules[val].UnInit() + } +} diff --git a/server/mqproxy/go.mod b/server/mqproxy/go.mod new file mode 100644 index 00000000..105cfed0 --- /dev/null +++ b/server/mqproxy/go.mod @@ -0,0 +1,58 @@ +module mqproxy + +go 1.20 + +require q5 v1.0.0 + +require f5 v1.0.0 + +require jccommon v1.0.0 + +require main v1.0.0 + +require ( + github.com/gin-gonic/gin v1.9.1 + github.com/google/uuid v1.6.0 +) + +require ( + github.com/bytedance/sonic v1.10.1 // indirect + github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect + github.com/chenzhuoyu/iasm v0.9.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.2 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.15.4 // indirect + github.com/go-sql-driver/mysql v1.7.1 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/gomodule/redigo v1.8.3 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.5 // indirect + github.com/leodido/go-urn v1.2.4 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.1.0 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.11 // indirect + golang.org/x/arch v0.5.0 // indirect + golang.org/x/crypto v0.21.0 // indirect + golang.org/x/net v0.23.0 // indirect + golang.org/x/sys v0.18.0 // indirect + golang.org/x/text v0.14.0 // indirect + google.golang.org/protobuf v1.33.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect + gorm.io/driver/mysql v1.5.1 // indirect + gorm.io/gorm v1.25.4 // indirect +) + +replace q5 => ../../third_party/q5 + +replace f5 => ../../third_party/f5 + +replace jccommon => ../jccommon + +replace main => ./ diff --git a/server/mqproxy/go.sum b/server/mqproxy/go.sum new file mode 100644 index 00000000..73daf4be --- /dev/null +++ b/server/mqproxy/go.sum @@ -0,0 +1,105 @@ +github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= +github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= +github.com/bytedance/sonic v1.10.1 h1:7a1wuFXL1cMy7a3f7/VFcEtriuXQnUBhtoVfOZiaysc= +github.com/bytedance/sonic v1.10.1/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= +github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= +github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= +github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= +github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA= +github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo= +github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= +github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= +github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.15.4 h1:zMXza4EpOdooxPel5xDqXEdXG5r+WggpvnAKMsalBjs= +github.com/go-playground/validator/v10 v10.15.4/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= +github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/gomodule/redigo v1.8.3 h1:HR0kYDX2RJZvAup8CsiJwxB4dTCSC0AaUq6S4SiLwUc= +github.com/gomodule/redigo v1.8.3/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg= +github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= +github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= +github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= +github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.5.0 h1:jpGode6huXQxcskEIpOCvrU+tzo81b6+oFLUYXWtH/Y= +golang.org/x/arch v0.5.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= +golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= +golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= +golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= +google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/mysql v1.5.1 h1:WUEH5VF9obL/lTtzjmML/5e6VfFR/788coz2uaVCAZw= +gorm.io/driver/mysql v1.5.1/go.mod h1:Jo3Xu7mMhCyj8dlrb3WoCaRd1FhsVh+yMXb1jUInf5o= +gorm.io/gorm v1.25.1/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= +gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw= +gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/server/mqproxy/initialize/enter.go b/server/mqproxy/initialize/enter.go new file mode 100644 index 00000000..04ac6528 --- /dev/null +++ b/server/mqproxy/initialize/enter.go @@ -0,0 +1,13 @@ +package initialize + +import ( + _ "main/app" + _ "main/controller" + . "main/global" + _ "main/router" + _ "main/service" +) + +func Init() { + GetApp().Run(InitModules, UnInitModules) +} diff --git a/server/mqproxy/main.go b/server/mqproxy/main.go new file mode 100644 index 00000000..ac0e77e9 --- /dev/null +++ b/server/mqproxy/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "main/initialize" +) + +func main() { + initialize.Init() +} diff --git a/server/mqproxy/makefile b/server/mqproxy/makefile new file mode 100644 index 00000000..49de0282 --- /dev/null +++ b/server/mqproxy/makefile @@ -0,0 +1,17 @@ +compile: + @. /etc/profile + + @export GOPROXY=https://goproxy.io + @go build -gcflags=all="-N -l" -o ../../bin/mqproxy/bin + @echo "compile done" + +debug: + @. /etc/profile + + @export GOPROXY=https://goproxy.io + @go build -gcflags=all="-N -l" -ldflags "-X q5.optDebug=1" -o ../../bin/mqproxy/bin + @echo "compile done" + +clean: + @rm -f ../../bin/mqproxy/bin + @echo "clean done" diff --git a/server/mqproxy/middleware/caauth.go b/server/mqproxy/middleware/caauth.go new file mode 100644 index 00000000..b851f0da --- /dev/null +++ b/server/mqproxy/middleware/caauth.go @@ -0,0 +1,4 @@ +package middleware + +import ( +) diff --git a/server/mqproxy/middleware/caforward.go b/server/mqproxy/middleware/caforward.go new file mode 100644 index 00000000..72817d5c --- /dev/null +++ b/server/mqproxy/middleware/caforward.go @@ -0,0 +1,134 @@ +package middleware + +import ( + "bytes" + "errors" + "f5" + "io/ioutil" + "jccommon" + "main/service" + "net/http" + net_url "net/url" + "q5" + "strings" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" +) + +func CaForward(c *gin.Context) { + accountId := c.DefaultQuery("account_id", "") + sessionId := c.DefaultQuery("session_id", "") + + if !jccommon.IsValidSessionId(accountId, sessionId) { + f5.RspErr(c, 500, "invalid session_id") + c.Abort() + service.SApiForward.IncInvalidSessionTimes() + return + } + + cLock := service.SApiForward.AcquireLock(accountId) + if cLock == nil { + f5.RspErr(c, 500, "system busy") + c.Abort() + return + } + defer service.SApiForward.ReleaseLock(cLock) + service.SApiForward.IncTotalTimes() + beginTick := q5.GetTickCount() + defer func() { + costTime := q5.GetTickCount() - beginTick + service.SApiForward.UpdateCostTime(costTime) + }() + downStreamUrl, downStreamHost := service.SApiForward.GetDownStreamHost(); + newUrl := downStreamUrl + c.Request.URL.Path[5:] + if !q5.StrContains(newUrl, "?") { + newUrl = newUrl + "?" + } + params := []*[]string{} + nonce := uuid.New().String() + nowTime := f5.GetApp().GetRealSeconds() + u := net_url.Values{} + { + for k, v := range c.Request.URL.Query() { + u.Set(k, v[0]) + q5.AppendSlice(¶ms, &[]string{k, v[0]}) + } + u.Set("__nonce", nonce) + u.Set("__timestamp", q5.ToString(nowTime)) + } + + var httpRequest *http.Request + var createErr error + switch strings.ToUpper(c.Request.Method) { + case "GET": + { + service.SApiForward.IncGetTimes() + u.Set("__sign", service.SApiForward.Sign(params, nonce, nowTime, "")) + newUrl += u.Encode() + 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() + 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 { + service.SApiForward.IncCreateErrTimes() + f5.RspErr(c, 500, "create request error") + c.Abort() + f5.GetSysLog().Info("CaForward create request url:%s error:%s", newUrl, createErr) + return + } + if downStreamHost != "" { + httpRequest.Host = downStreamHost + } + client := &http.Client{} + if resp, err := client.Do(httpRequest); err == nil { + defer resp.Body.Close() + if bytes, err := ioutil.ReadAll(resp.Body); err == nil { + service.SApiForward.IncOkTimes() + c.String(200, string(bytes)) + c.Abort() + return + } else { + service.SApiForward.IncReadRspErrTimes() + f5.RspErr(c, 500, "read response error") + c.Abort() + f5.GetSysLog().Info("CaForward read response url:%s eror:%s", newUrl, err) + return + } + } else { + service.SApiForward.IncDoErrTimes() + f5.RspErr(c, 500, "client.Do error") + c.Abort() + f5.GetSysLog().Info("CaForward client.Do url:%s error:%s", newUrl, err) + return + } + +} diff --git a/server/mqproxy/mt/ConfDb.go b/server/mqproxy/mt/ConfDb.go new file mode 100644 index 00000000..ab948bf7 --- /dev/null +++ b/server/mqproxy/mt/ConfDb.go @@ -0,0 +1,15 @@ +package mt + +import ( + "f5" + "main/mtb" +) + +type ConfDb struct { + mtb.ConfDb +} + +type ConfDbTable struct { + f5.IdMetaTable[ConfDb] + selfConf *ConfDb +} diff --git a/server/mqproxy/mt/Config.go b/server/mqproxy/mt/Config.go new file mode 100644 index 00000000..0cf8b7f6 --- /dev/null +++ b/server/mqproxy/mt/Config.go @@ -0,0 +1,56 @@ +package mt + +import ( + "f5" + "main/mtb" + "net/url" +) + +type Config struct { + mtb.Config + redirectHost string +} + +type ConfigTable struct { + f5.IdMetaTable[Config] + selfConf *Config +} + +func (this *Config) Init1() { + u, err := url.Parse(this.GetRedirectUrl()) + if err != nil { + panic(err) + } + this.redirectHost = u.Host +} + +func (this *ConfigTable) GetGameSApiUrl() string { + return this.selfConf.GetGamesapiUrl() +} + +func (this *ConfigTable) GetRedirectUrl() string { + return this.selfConf.GetRedirectUrl() +} + +func (this *ConfigTable) GetRedirectHost() string { + return this.selfConf.redirectHost +} + +func (this *ConfigTable) GetSecretKey() string { + return this.selfConf.GetSecretKey() +} + +func (this *ConfigTable) GetRedirectSecretKey() string { + return this.selfConf.GetRedirectSecretKey() +} + +func (this *ConfigTable) GetMaxConcurrentNum() int32 { + return this.selfConf.GetMaxConcurrentNum() +} + +func (this *ConfigTable) PostInit1() { + this.selfConf = this.GetById(int64(0)) + if this.selfConf == nil { + panic("gamesapi config无法读取本服配置") + } +} diff --git a/server/mqproxy/mt/GamesapiCluster.go b/server/mqproxy/mt/GamesapiCluster.go new file mode 100644 index 00000000..d815dad0 --- /dev/null +++ b/server/mqproxy/mt/GamesapiCluster.go @@ -0,0 +1,34 @@ +package mt + +import ( + "f5" + "main/mtb" +) + +type GamesapiCluster struct { + mtb.GamesapiCluster +} + +type GamesapiClusterTable struct { + f5.IdMetaTable[GamesapiCluster] + selfConf *GamesapiCluster +} + +func (this *GamesapiCluster) Init1() { + +} + +func (this *GamesapiClusterTable) GetListenPort() int32 { + return this.selfConf.GetListenPort() +} + +func (this *GamesapiClusterTable) GetHttpListenPort() int32 { + return this.selfConf.GetHttpListenPort() +} + +func (this *GamesapiClusterTable) PostInit1() { + this.selfConf = this.GetById(int64(f5.GetApp().GetInstanceId())) + if this.selfConf == nil { + panic("gamesapi集群无法读取本服配置") + } +} diff --git a/server/mqproxy/mt/export.go b/server/mqproxy/mt/export.go new file mode 100644 index 00000000..c23badaa --- /dev/null +++ b/server/mqproxy/mt/export.go @@ -0,0 +1,29 @@ +package mt + +import ( + "f5" +) + +type table struct { + GamesapiCluster *GamesapiClusterTable + Config *ConfigTable + ConfDb *ConfDbTable +} + +var Table = f5.New(func(this *table) { + this.GamesapiCluster = f5.New(func(this *GamesapiClusterTable) { + this.FileName = "../config/gamesapi.cluster.json" + this.PrimKey = "instance_id" + }) + + this.Config = f5.New(func(this *ConfigTable) { + this.FileName = "../config/config.json" + this.PrimKey = "" + }) + + this.ConfDb = f5.New(func(this *ConfDbTable) { + this.FileName = "../config/confdb.mysql.json" + this.PrimKey = "" + }) + +}) diff --git a/server/mqproxy/mtb/mtb.auto_gen.go b/server/mqproxy/mtb/mtb.auto_gen.go new file mode 100644 index 00000000..337b9e43 --- /dev/null +++ b/server/mqproxy/mtb/mtb.auto_gen.go @@ -0,0 +1,213 @@ +package mtb + +import ( + "f5" +) + +type GamesapiCluster struct { + instance_id int32 + listen_port int32 + http_listen_port int32 + + _flags1_ uint64 + _flags2_ uint64 +} + +type Config struct { + gamesapi_url string + secret_key string + gm_open int32 + gm_secret_key string + redirect_url string + max_concurrent_num int32 + request_over_time int32 + redirect_secret_key string + + _flags1_ uint64 + _flags2_ uint64 +} + +type ConfDb struct { + host string + port int32 + user string + passwd string + database string + max_open_conns int32 + max_idle_conns int32 + + _flags1_ uint64 + _flags2_ uint64 +} + +func (this *GamesapiCluster) GetInstanceId() int32 { + return this.instance_id +} + +func (this *GamesapiCluster) HasInstanceId() bool { + return (this._flags1_ & (uint64(1) << 1)) > 0 +} + +func (this *GamesapiCluster) GetListenPort() int32 { + return this.listen_port +} + +func (this *GamesapiCluster) HasListenPort() bool { + return (this._flags1_ & (uint64(1) << 2)) > 0 +} + +func (this *GamesapiCluster) GetHttpListenPort() int32 { + return this.http_listen_port +} + +func (this *GamesapiCluster) HasHttpListenPort() bool { + return (this._flags1_ & (uint64(1) << 3)) > 0 +} + +func (this *Config) GetGamesapiUrl() string { + return this.gamesapi_url +} + +func (this *Config) HasGamesapiUrl() bool { + return (this._flags1_ & (uint64(1) << 1)) > 0 +} + +func (this *Config) GetSecretKey() string { + return this.secret_key +} + +func (this *Config) HasSecretKey() bool { + return (this._flags1_ & (uint64(1) << 2)) > 0 +} + +func (this *Config) GetGmOpen() int32 { + return this.gm_open +} + +func (this *Config) HasGmOpen() bool { + return (this._flags1_ & (uint64(1) << 3)) > 0 +} + +func (this *Config) GetGmSecretKey() string { + return this.gm_secret_key +} + +func (this *Config) HasGmSecretKey() bool { + return (this._flags1_ & (uint64(1) << 4)) > 0 +} + +func (this *Config) GetRedirectUrl() string { + return this.redirect_url +} + +func (this *Config) HasRedirectUrl() bool { + return (this._flags1_ & (uint64(1) << 5)) > 0 +} + +func (this *Config) GetMaxConcurrentNum() int32 { + return this.max_concurrent_num +} + +func (this *Config) HasMaxConcurrentNum() bool { + return (this._flags1_ & (uint64(1) << 6)) > 0 +} + +func (this *Config) GetRequestOverTime() int32 { + return this.request_over_time +} + +func (this *Config) HasRequestOverTime() bool { + return (this._flags1_ & (uint64(1) << 7)) > 0 +} + +func (this *Config) GetRedirectSecretKey() string { + return this.redirect_secret_key +} + +func (this *Config) HasRedirectSecretKey() bool { + return (this._flags1_ & (uint64(1) << 8)) > 0 +} + +func (this *ConfDb) GetHost() string { + return this.host +} + +func (this *ConfDb) HasHost() bool { + return (this._flags1_ & (uint64(1) << 1)) > 0 +} + +func (this *ConfDb) GetPort() int32 { + return this.port +} + +func (this *ConfDb) HasPort() bool { + return (this._flags1_ & (uint64(1) << 2)) > 0 +} + +func (this *ConfDb) GetUser() string { + return this.user +} + +func (this *ConfDb) HasUser() bool { + return (this._flags1_ & (uint64(1) << 3)) > 0 +} + +func (this *ConfDb) GetPasswd() string { + return this.passwd +} + +func (this *ConfDb) HasPasswd() bool { + return (this._flags1_ & (uint64(1) << 4)) > 0 +} + +func (this *ConfDb) GetDatabase() string { + return this.database +} + +func (this *ConfDb) HasDatabase() bool { + return (this._flags1_ & (uint64(1) << 5)) > 0 +} + +func (this *ConfDb) GetMaxOpenConns() int32 { + return this.max_open_conns +} + +func (this *ConfDb) HasMaxOpenConns() bool { + return (this._flags1_ & (uint64(1) << 6)) > 0 +} + +func (this *ConfDb) GetMaxIdleConns() int32 { + return this.max_idle_conns +} + +func (this *ConfDb) HasMaxIdleConns() bool { + return (this._flags1_ & (uint64(1) << 7)) > 0 +} + + +func (this *GamesapiCluster) LoadFromKv(kv map[string]interface{}) { + f5.ReadMetaTableField(&this.instance_id, "instance_id", &this._flags1_, 1, kv) + f5.ReadMetaTableField(&this.listen_port, "listen_port", &this._flags1_, 2, kv) + f5.ReadMetaTableField(&this.http_listen_port, "http_listen_port", &this._flags1_, 3, kv) +} + +func (this *Config) LoadFromKv(kv map[string]interface{}) { + f5.ReadMetaTableField(&this.gamesapi_url, "gamesapi_url", &this._flags1_, 1, kv) + f5.ReadMetaTableField(&this.secret_key, "secret_key", &this._flags1_, 2, kv) + f5.ReadMetaTableField(&this.gm_open, "gm_open", &this._flags1_, 3, kv) + f5.ReadMetaTableField(&this.gm_secret_key, "gm_secret_key", &this._flags1_, 4, kv) + f5.ReadMetaTableField(&this.redirect_url, "redirect_url", &this._flags1_, 5, kv) + f5.ReadMetaTableField(&this.max_concurrent_num, "max_concurrent_num", &this._flags1_, 6, kv) + f5.ReadMetaTableField(&this.request_over_time, "request_over_time", &this._flags1_, 7, kv) + f5.ReadMetaTableField(&this.redirect_secret_key, "redirect_secret_key", &this._flags1_, 8, kv) +} + +func (this *ConfDb) LoadFromKv(kv map[string]interface{}) { + f5.ReadMetaTableField(&this.host, "host", &this._flags1_, 1, kv) + f5.ReadMetaTableField(&this.port, "port", &this._flags1_, 2, kv) + f5.ReadMetaTableField(&this.user, "user", &this._flags1_, 3, kv) + f5.ReadMetaTableField(&this.passwd, "passwd", &this._flags1_, 4, kv) + f5.ReadMetaTableField(&this.database, "database", &this._flags1_, 5, kv) + f5.ReadMetaTableField(&this.max_open_conns, "max_open_conns", &this._flags1_, 6, kv) + f5.ReadMetaTableField(&this.max_idle_conns, "max_idle_conns", &this._flags1_, 7, kv) +} diff --git a/server/mqproxy/proto/mt.proto b/server/mqproxy/proto/mt.proto new file mode 100644 index 00000000..2dfd5885 --- /dev/null +++ b/server/mqproxy/proto/mt.proto @@ -0,0 +1,33 @@ +package mt; + +option go_package = ".;mt"; + +message GamesapiCluster +{ + optional int32 instance_id = 1; + optional int32 listen_port = 2; + optional int32 http_listen_port = 3; +} + +message Config +{ + optional string gamesapi_url = 1; + optional string secret_key = 2; + optional int32 gm_open = 3; + optional string gm_secret_key = 4; + optional string redirect_url = 5; + optional int32 max_concurrent_num = 6; + optional int32 request_over_time = 7; + optional string redirect_secret_key = 8; +} + +message ConfDb +{ + optional string host = 1; + optional int32 port = 2; + optional string user = 3; + optional string passwd = 4; + optional string database = 5; + optional int32 max_open_conns = 6; + optional int32 max_idle_conns = 7; +} diff --git a/server/mqproxy/proto/protoc-gen.bat b/server/mqproxy/proto/protoc-gen.bat new file mode 100644 index 00000000..61b25a95 --- /dev/null +++ b/server/mqproxy/proto/protoc-gen.bat @@ -0,0 +1,2 @@ +protoc --go_out=..\cs .\cs_*.proto +protoc --go_out=..\ss .\ss_*.proto diff --git a/server/mqproxy/router/export.go b/server/mqproxy/router/export.go new file mode 100644 index 00000000..4c2a87fa --- /dev/null +++ b/server/mqproxy/router/export.go @@ -0,0 +1,12 @@ +package router + +import ( + "main/constant" + "main/global" +) + +var _routerMgr = new(routerMgr) + +func init() { + global.RegModule(constant.ROUTER_MODULE_IDX, _routerMgr) +} diff --git a/server/mqproxy/router/routermgr.go b/server/mqproxy/router/routermgr.go new file mode 100644 index 00000000..7ab29e0f --- /dev/null +++ b/server/mqproxy/router/routermgr.go @@ -0,0 +1,23 @@ +package router + +import ( + "f5" + "main/middleware" + //. "main/global" + //"main/router/system" +) + +type routerMgr struct { + //system system.RouterGroup +} + +func (this *routerMgr) Init() { + redirectGroup := f5.GetApp().GetGinEngine().Group("/sapi") + redirectGroup.Any("webapp/index.php", middleware.CaForward) + + f5.GetSysLog().Info("routerMgr.init") +} + +func (this *routerMgr) UnInit() { + +} diff --git a/server/mqproxy/service/export.go b/server/mqproxy/service/export.go new file mode 100644 index 00000000..bbfe94fe --- /dev/null +++ b/server/mqproxy/service/export.go @@ -0,0 +1,13 @@ +package service + +import ( + "main/constant" + "main/global" +) + +var _serviceMgr = new(serviceMgr) +var SApiForward *sApiForward + +func init() { + global.RegModule(constant.SERVICE_MGR_MODULE_IDX, _serviceMgr) +} diff --git a/server/mqproxy/service/sapi_forward.go b/server/mqproxy/service/sapi_forward.go new file mode 100644 index 00000000..49eab1d1 --- /dev/null +++ b/server/mqproxy/service/sapi_forward.go @@ -0,0 +1,230 @@ +package service + +import ( + "f5" + "fmt" + "main/constant" + "main/mt" + "math/rand" + "q5" + "sync" + "sync/atomic" + "time" +) + +type downStreamHost struct { + Host string `json:"host"` + Port int32 `json:"port"` + Url string `json:"url"` +} + +type sApiForward struct { + userCache []*SApiForwardLockCache + downStreams []downStreamHost + insessTimes int32 + total int32 + getTimes int32 + postTimes int32 + createErrTimes int32 + doErrTimes int32 + okTimes int32 + readRspErrTimes int32 + refuseTimes int32 + maxCostTime int64 +} + +type SApiForwardLockCache struct { + lock *sync.Mutex + userHash *map[string]*SApiForwardLock +} + +type SApiForwardLock struct { + accountId string + lockTimes int32 + lock *sync.Mutex +} + +func (this *sApiForward) init() { + q5.NewSlice(&this.downStreams, 0, 20) + q5.NewSlice(&this.userCache, 1024, 1024) + for i := 0; i < len(this.userCache); i++ { + p := new(SApiForwardLockCache) + p.lock = new(sync.Mutex) + p.userHash = &map[string]*SApiForwardLock{} + this.userCache[i] = p + } + this.LoadDownStreams() + go func() { + time.Sleep(time.Second * 60 * 10) + this.LoadDownStreams() + }() +} + +func (this *sApiForward) unInit() { +} + +func (this *sApiForward) AcquireLock(accountId string) *SApiForwardLock { + crc32 := q5.Crc32(accountId) + c := this.userCache[int64(crc32)%int64(len(this.userCache))] + u := this.getOrCreate(c, accountId) + if atomic.AddInt32(&u.lockTimes, 1) > mt.Table.Config.GetMaxConcurrentNum() { + atomic.AddInt32(&u.lockTimes, -1) + this.IncRefuseTimes() + return nil + } + u.lock.Lock() + return u +} + +func (this *sApiForward) ReleaseLock(l *SApiForwardLock) { + l.lock.Unlock() + if atomic.AddInt32(&l.lockTimes, -1) < 1 { + crc32 := q5.Crc32(l.accountId) + c := this.userCache[int64(crc32)%int64(len(this.userCache))] + delete(*c.userHash, l.accountId) + } +} + +func (this *sApiForward) IncInvalidSessionTimes() { + atomic.AddInt32(&this.insessTimes, 1) +} + +func (this *sApiForward) IncTotalTimes() { + atomic.AddInt32(&this.total, 1) +} + +func (this *sApiForward) IncGetTimes() { + atomic.AddInt32(&this.getTimes, 1) +} + +func (this *sApiForward) IncPostTimes() { + atomic.AddInt32(&this.postTimes, 1) +} + +func (this *sApiForward) IncCreateErrTimes() { + atomic.AddInt32(&this.createErrTimes, 1) +} + +func (this *sApiForward) IncDoErrTimes() { + atomic.AddInt32(&this.doErrTimes, 1) + +} + +func (this *sApiForward) IncOkTimes() { + atomic.AddInt32(&this.okTimes, 1) + +} + +func (this *sApiForward) IncReadRspErrTimes() { + atomic.AddInt32(&this.readRspErrTimes, 1) + +} + +func (this *sApiForward) IncRefuseTimes() { + atomic.AddInt32(&this.refuseTimes, 1) +} + +func (this *sApiForward) UpdateCostTime(costTime int64) { + if this.maxCostTime < costTime { + this.maxCostTime = costTime + } +} + +func (this *sApiForward) getOrCreate(c *SApiForwardLockCache, accountId string) *SApiForwardLock { + c.lock.Lock() + defer c.lock.Unlock() + if u, ok := (*c.userHash)[accountId]; ok { + return u + } else { + u = new(SApiForwardLock) + u.accountId = accountId + u.lock = new(sync.Mutex) + (*c.userHash)[accountId] = u + return u + } +} + +func (this *sApiForward) Sign(params []*[]string, nonce string, timeStamp int64, postData string) string { + signData := "" + q5.Sort(params, func(a *[]string, b *[]string) bool { + return (*a)[0] < (*b)[0] + }) + for _, v := range params { + signData += (*v)[0] + "=" + (*v)[1] + "&" + } + signData += nonce + q5.ToString(timeStamp) + postData + mt.Table.Config.GetRedirectSecretKey() + 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) + } +} + +func (this *sApiForward) LoadDownStreams() error { + err, ds := f5.GetGoStyleDb().NewOrmSelect( + constant.CONF_DB, + "t_internal_gameapi_host", + [][]string{}) + if err == nil { + downStreams := []downStreamHost{} + q5.NewSlice(&downStreams, 0, 20) + for ds.Next() { + host := ds.GetByName("gameapi_host") + port := q5.ToInt32(ds.GetByName("gameapi_port")) + enable := q5.ToInt32(ds.GetByName("enable")) + if enable != 0 { + downSteam := q5.NewSliceElement(&downStreams) + downSteam.Host = host + downSteam.Port = port + downSteam.Url = fmt.Sprintf("http://%s:%d", downSteam.Host, downSteam.Port) + } + } + if len(downStreams) > 0 { + this.downStreams = downStreams + } + f5.GetSysLog().Info("LoadDownstreams ok %s", q5.EncodeJson(&downStreams)) + } else { + f5.GetSysLog().Info("LoadDownstreams err %s", err) + } + return err +} + +func (this *sApiForward) GetDownStreamHost() (string, string) { + downStreams := this.downStreams + if len(downStreams) <= 0 { + return mt.Table.Config.GetRedirectUrl(), "" + } + downStream := downStreams[rand.Intn(len(downStreams))] + return downStream.Url, mt.Table.Config.GetRedirectHost() +} diff --git a/server/mqproxy/service/servicemgr.go b/server/mqproxy/service/servicemgr.go new file mode 100644 index 00000000..acd67915 --- /dev/null +++ b/server/mqproxy/service/servicemgr.go @@ -0,0 +1,15 @@ +package service + +type serviceMgr struct { +} + +func (this *serviceMgr) Init() { + SApiForward = new(sApiForward) + SApiForward.init() + + go SApiForward.outputMonitorLog() +} + +func (this *serviceMgr) UnInit() { + SApiForward.unInit() +}