| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- 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
- }
|