package service 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() { atomic.AddInt32(&u.lockTimes, -1) 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) IncTotalTimes() { } func (this *sApiForward) IncGetTimes() { } func (this *sApiForward) IncPostTimes() { } func (this *sApiForward) IncCreateErrTimes() { } func (this *sApiForward) IncDoErrTimes() { } func (this *sApiForward) IncOkTimes() { } 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 } }