package model import ( "rocommon" "rocommon/util" "roserver/baseserver/set" "roserver/serverproto" "runtime/debug" "sync" "time" ) var ( updateList []interface{} MysqlUpdateMag *MysqlUpdateManager ) //MysqlUpdatePool type MysqlUpdatePool struct { qList chan int } func NewPool(maxSize int) *MysqlUpdatePool { return &MysqlUpdatePool{qList: make(chan int, maxSize)} } func (this *MysqlUpdatePool) Acquire() { this.qList <- 1 } func (this *MysqlUpdatePool) Release() { <-this.qList } ///////////////////////// type mysqlPlayerBriefInfo struct { uid uint64 briefInfo *serverproto.CommonPlayerBriefInfo bCreate bool activeCode string openId string subPlatform string } type mysqlResInfo struct { uid uint64 saveInfo *serverproto.RoleRes2MysqlInfo } type mysqlBanInfo struct { uid uint64 banTime uint64 banType int32 //1封号,2禁言 } type mysqlChatInfo struct { uid uint64 targetUid uint64 msgType int32 content *serverproto.ChatMessageInfo } //DBUpdate type DBUpdate struct { rocommon.UpdateModule //eventqueue.go initTime uint64 } func (this *DBUpdate) Init() { this.initTime = util.GetTimeMilliseconds() + 2*1000 MysqlUpdateMag = NewMysqlUpdateManager() MysqlUpdateMag.Init() } func (this *DBUpdate) Update(ms uint64) { } //MysqlUpdateManager type MysqlUpdateManager struct { rocommon.UpdateModule dataLock sync.Mutex mysqlProcessList set.Interface mysqlPlayerBriefInfoList map[uint64]*mysqlPlayerBriefInfo mysqlResInfoList map[uint64]*mysqlResInfo mysqlBanInfoList map[uint64]*mysqlBanInfo mysqlLogInfoList []*serverproto.SSRoleLogData mysqlChatInfoList []*mysqlChatInfo mysqlUpdatePool *MysqlUpdatePool } func NewMysqlUpdateManager() *MysqlUpdateManager { rm := &MysqlUpdateManager{ mysqlProcessList: set.New(set.NonThreadSafe), mysqlPlayerBriefInfoList: map[uint64]*mysqlPlayerBriefInfo{}, mysqlResInfoList: map[uint64]*mysqlResInfo{}, mysqlBanInfoList: map[uint64]*mysqlBanInfo{}, } //rm.updateTimer = util.NewDurationTimer(util.GetCurrentTime(), 1000*30) return rm } func (this *MysqlUpdateManager) Update(ms uint64) { } func (this *MysqlUpdateManager) Init() { this.mysqlUpdatePool = NewPool(20) go func() { defer func() { //打印奔溃信息 if err := recover(); err != nil { util.InfoF("onError data=%v \n%s\n", err, string(debug.Stack())) } }() delayTimer := time.NewTimer(5 * time.Second) for { delayTimer.Reset(5 * time.Second) select { case <-delayTimer.C: case this.mysqlUpdatePool.qList <- 1: if !this.doMysqlProcess() { time.Sleep(50 * time.Millisecond) } } } }() } func (this *MysqlUpdateManager) doMysqlProcess() bool { //util.InfoF("doMysqlProcess...") bProcess := false // //this.dataLock.Lock() //if this.mysqlProcessList.Size() > 0 { // processDataInfo := this.mysqlProcessList.Pop() // switch item := processDataInfo.(type) { // case *mysqlPlayerBriefInfo: // doUpdatePlayerBriefInfo2MysqlORM(this, item.uid, item.briefInfo, item.bCreate, item.activeCode, item.openId, item.subPlatform) // bProcess = true // case *mysqlResInfo: // doUpdatePlayerRes2MysqlORM(this, item.uid, item.saveInfo) // bProcess = true // case *mysqlBanInfo: // DoUpdatePlayerBanInfo2MysqlORM(this, item.uid, item.banTime, item.banType) // bProcess = true // } //} //this.dataLock.Unlock() //if bProcess { // return bProcess //} //mysqlPlayerBriefInfoList this.dataLock.Lock() if len(this.mysqlPlayerBriefInfoList) > 0 { for uid, item := range this.mysqlPlayerBriefInfoList { //doUpdatePlayerBriefInfo2Mysql(this, uid, item.briefInfo, item.bCreate, item.activeCode, item.openId) doUpdatePlayerBriefInfo2MysqlORM(this, uid, item.briefInfo, item.bCreate, item.activeCode, item.openId, item.subPlatform) delete(this.mysqlPlayerBriefInfoList, uid) bProcess = true break } } this.dataLock.Unlock() if bProcess { return bProcess } //mysqlResInfoList this.dataLock.Lock() if len(this.mysqlResInfoList) > 0 { for uid, item := range this.mysqlResInfoList { //doUpdatePlayerRes2Mysql(this, uid, item.saveInfo) doUpdatePlayerRes2MysqlORM(this, uid, item.saveInfo) delete(this.mysqlResInfoList, uid) bProcess = true break } } this.dataLock.Unlock() if bProcess { return bProcess } //mysqlBanInfoList this.dataLock.Lock() if len(this.mysqlBanInfoList) > 0 { for uid, item := range this.mysqlBanInfoList { //DoUpdatePlayerBanInfo2Mysql(this, uid, item.banTime, item.banType) DoUpdatePlayerBanInfo2MysqlORM(this, uid, item.banTime, item.banType) delete(this.mysqlBanInfoList, uid) bProcess = true break } } this.dataLock.Unlock() if bProcess { return bProcess } //mysqlLogInfoList this.dataLock.Lock() if len(this.mysqlLogInfoList) > 0 { if len(this.mysqlLogInfoList) > 20 { tmpList := make([]*serverproto.SSRoleLogData, 20) copy(tmpList, this.mysqlLogInfoList[:20]) this.mysqlLogInfoList = this.mysqlLogInfoList[20:] //doUpdatePlayerLog2Mysql(this, &tmpList) doUpdatePlayerLog2MysqlORM(this, &tmpList) } else { tmpList := make([]*serverproto.SSRoleLogData, len(this.mysqlLogInfoList)) copy(tmpList, this.mysqlLogInfoList) this.mysqlLogInfoList = this.mysqlLogInfoList[:0] //doUpdatePlayerLog2Mysql(this, &tmpList) doUpdatePlayerLog2MysqlORM(this, &tmpList) } bProcess = true } this.dataLock.Unlock() if bProcess { return bProcess } //mysqlChatInfoList this.dataLock.Lock() if len(this.mysqlChatInfoList) > 0 { //var tmpList *[]mysqlChatInfo if len(this.mysqlChatInfoList) > 20 { tmpList := make([]*mysqlChatInfo, 20) copy(tmpList, this.mysqlChatInfoList[:20]) this.mysqlChatInfoList = this.mysqlChatInfoList[20:] //doUpdatePlayerChatMsg2Mysql(this, &tmpList) doUpdatePlayerChatMsg2MysqlORM(this, &tmpList) } else { tmpList := make([]*mysqlChatInfo, len(this.mysqlChatInfoList)) copy(tmpList, this.mysqlChatInfoList) this.mysqlChatInfoList = this.mysqlChatInfoList[:0] //doUpdatePlayerChatMsg2Mysql(this, &tmpList) doUpdatePlayerChatMsg2MysqlORM(this, &tmpList) } bProcess = true } this.dataLock.Unlock() if bProcess { return bProcess } this.mysqlUpdatePool.Release() return bProcess }