1
This commit is contained in:
parent
cfef3efaa8
commit
a91b23c0b1
@ -20,15 +20,15 @@ func (this *ConfigTable) GetGameSApiUrl() string {
|
|||||||
|
|
||||||
func (this *ConfigTable) GetSecretKey() string {
|
func (this *ConfigTable) GetSecretKey() string {
|
||||||
return this.selfConf.GetSecretKey()
|
return this.selfConf.GetSecretKey()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *ConfigTable) GetMaxConcurrentNum() int32 {
|
||||||
|
return this.selfConf.GetMaxConcurrentNum()
|
||||||
|
}
|
||||||
|
|
||||||
func (this *ConfigTable) PostInit1() {
|
func (this *ConfigTable) PostInit1() {
|
||||||
this.selfConf = this.GetById(int64(0))
|
this.selfConf = this.GetById(int64(0))
|
||||||
if this.selfConf == nil {
|
if this.selfConf == nil {
|
||||||
panic("gamesapi config无法读取本服配置")
|
panic("gamesapi config无法读取本服配置")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ConfigTable) GetMaxCache() int32 {
|
|
||||||
return this.selfConf.GetMaxRequestCache()
|
|
||||||
}
|
|
||||||
|
@ -19,7 +19,7 @@ type Config struct {
|
|||||||
gm_open int32
|
gm_open int32
|
||||||
gm_secret_key string
|
gm_secret_key string
|
||||||
redirect_url string
|
redirect_url string
|
||||||
max_request_cache int32
|
max_concurrent_num int32
|
||||||
request_over_time int32
|
request_over_time int32
|
||||||
|
|
||||||
_flags1_ uint64
|
_flags1_ uint64
|
||||||
@ -90,11 +90,11 @@ func (this *Config) HasRedirectUrl() bool {
|
|||||||
return (this._flags1_ & (uint64(1) << 5)) > 0
|
return (this._flags1_ & (uint64(1) << 5)) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Config) GetMaxRequestCache() int32 {
|
func (this *Config) GetMaxConcurrentNum() int32 {
|
||||||
return this.max_request_cache
|
return this.max_concurrent_num
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Config) HasMaxRequestCache() bool {
|
func (this *Config) HasMaxConcurrentNum() bool {
|
||||||
return (this._flags1_ & (uint64(1) << 6)) > 0
|
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_open, "gm_open", &this._flags1_, 3, kv)
|
||||||
f5.ReadMetaTableField(&this.gm_secret_key, "gm_secret_key", &this._flags1_, 4, 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.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)
|
f5.ReadMetaTableField(&this.request_over_time, "request_over_time", &this._flags1_, 7, kv)
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,6 @@ message Config
|
|||||||
optional int32 gm_open = 3;
|
optional int32 gm_open = 3;
|
||||||
optional string gm_secret_key = 4;
|
optional string gm_secret_key = 4;
|
||||||
optional string redirect_url = 5;
|
optional string redirect_url = 5;
|
||||||
optional int32 max_request_cache = 6;
|
optional int32 max_concurrent_num = 6;
|
||||||
optional int32 request_over_time = 7;
|
optional int32 request_over_time = 7;
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,54 @@
|
|||||||
package service
|
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() {
|
func (this *sApiForward) IncInvalidSessionTimes() {
|
||||||
@ -35,3 +82,15 @@ func (this *sApiForward) IncReadRspErrTimes() {
|
|||||||
func (this *sApiForward) UpdateCostTime(costTime int64) {
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user