From a91b23c0b1c3025ff21ea74767260a9625bb60dc Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Wed, 24 Jul 2024 18:02:31 +0800 Subject: [PATCH] 1 --- server/gamesapi/mt/Config.go | 10 ++-- server/gamesapi/mtb/mtb.auto_gen.go | 10 ++-- server/gamesapi/proto/mt.proto | 2 +- server/gamesapi/service/sapi_forward.go | 61 ++++++++++++++++++++++++- 4 files changed, 71 insertions(+), 12 deletions(-) diff --git a/server/gamesapi/mt/Config.go b/server/gamesapi/mt/Config.go index 40dce32b..840186ab 100644 --- a/server/gamesapi/mt/Config.go +++ b/server/gamesapi/mt/Config.go @@ -20,15 +20,15 @@ func (this *ConfigTable) GetGameSApiUrl() string { func (this *ConfigTable) GetSecretKey() string { return this.selfConf.GetSecretKey() - } + +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无法读取本服配置") } } - -func (this *ConfigTable) GetMaxCache() int32 { - return this.selfConf.GetMaxRequestCache() -} diff --git a/server/gamesapi/mtb/mtb.auto_gen.go b/server/gamesapi/mtb/mtb.auto_gen.go index 312d5ac6..1910015b 100644 --- a/server/gamesapi/mtb/mtb.auto_gen.go +++ b/server/gamesapi/mtb/mtb.auto_gen.go @@ -19,7 +19,7 @@ type Config struct { gm_open int32 gm_secret_key string redirect_url string - max_request_cache int32 + max_concurrent_num int32 request_over_time int32 _flags1_ uint64 @@ -90,11 +90,11 @@ func (this *Config) HasRedirectUrl() bool { return (this._flags1_ & (uint64(1) << 5)) > 0 } -func (this *Config) GetMaxRequestCache() int32 { - return this.max_request_cache +func (this *Config) GetMaxConcurrentNum() int32 { + return this.max_concurrent_num } -func (this *Config) HasMaxRequestCache() bool { +func (this *Config) HasMaxConcurrentNum() bool { return (this._flags1_ & (uint64(1) << 6)) > 0 } @@ -119,6 +119,6 @@ func (this *Config) LoadFromKv(kv map[string]interface{}) { 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_request_cache, "max_request_cache", &this._flags1_, 6, 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) } diff --git a/server/gamesapi/proto/mt.proto b/server/gamesapi/proto/mt.proto index 4929bc81..5b4f6a76 100644 --- a/server/gamesapi/proto/mt.proto +++ b/server/gamesapi/proto/mt.proto @@ -16,6 +16,6 @@ message Config optional int32 gm_open = 3; optional string gm_secret_key = 4; optional string redirect_url = 5; - optional int32 max_request_cache = 6; + optional int32 max_concurrent_num = 6; optional int32 request_over_time = 7; } diff --git a/server/gamesapi/service/sapi_forward.go b/server/gamesapi/service/sapi_forward.go index d90737b4..ed8d36b5 100644 --- a/server/gamesapi/service/sapi_forward.go +++ b/server/gamesapi/service/sapi_forward.go @@ -1,7 +1,54 @@ package service -type sApiForward struct { +import ( + "q5" + "sync" + "mt" + "sync/atomic" +) +type sApiForward struct { + userCache []*SApiForwardLockCache +} + +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.userCache, 1024, 1024) + for i := 0; i < len(this.userCache); i++ { + p := new(SApiForwardLockCache) + p.lock = new(sync.Mutex) + p.userHash = new(map[string]*SApiForwardLock) + this.userCache[i] = p + } +} + +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() { + return nil + } + u.lock.Lock() + return u +} + +func (this *sApiForward) ReleaseLock(l *SApiForwardLock) { + l.lock.Unlock() + atomic.AddInt32(&l.lockTimes, -1) } func (this *sApiForward) IncInvalidSessionTimes() { @@ -35,3 +82,15 @@ func (this *sApiForward) IncReadRspErrTimes() { func (this *sApiForward) UpdateCostTime(costTime int64) { } + +func (this *sApiForward) getOrCreate(c *SApiForwardLockCache, accountId string) *SApiForwardLock { + c.lock.Lock() + defer c.lock.Lock() + if u, ok := (*c.userHash)[accountId]; ok { + return u + } else { + u = new(SApiForwardLock) + u.accountId = accountId + return u + } +}