game2006go/server/gamesapi/service/sapi_forward.go
2024-07-25 13:26:30 +08:00

172 lines
3.9 KiB
Go

package service
import (
"f5"
"mt"
"q5"
"sync"
"sync/atomic"
"time"
)
type sApiForward struct {
userCache []*SApiForwardLockCache
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.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)
this.IncRefuseTimes()
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() {
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)
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)
}
}