mysql_update_model.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package model
  2. import (
  3. "rocommon"
  4. "rocommon/util"
  5. "roserver/baseserver/set"
  6. "roserver/serverproto"
  7. "runtime/debug"
  8. "sync"
  9. "time"
  10. )
  11. var (
  12. updateList []interface{}
  13. MysqlUpdateMag *MysqlUpdateManager
  14. )
  15. //MysqlUpdatePool
  16. type MysqlUpdatePool struct {
  17. qList chan int
  18. }
  19. func NewPool(maxSize int) *MysqlUpdatePool {
  20. return &MysqlUpdatePool{qList: make(chan int, maxSize)}
  21. }
  22. func (this *MysqlUpdatePool) Acquire() {
  23. this.qList <- 1
  24. }
  25. func (this *MysqlUpdatePool) Release() {
  26. <-this.qList
  27. }
  28. /////////////////////////
  29. type mysqlPlayerBriefInfo struct {
  30. uid uint64
  31. briefInfo *serverproto.CommonPlayerBriefInfo
  32. bCreate bool
  33. activeCode string
  34. openId string
  35. subPlatform string
  36. }
  37. type mysqlResInfo struct {
  38. uid uint64
  39. saveInfo *serverproto.RoleRes2MysqlInfo
  40. }
  41. type mysqlBanInfo struct {
  42. uid uint64
  43. banTime uint64
  44. banType int32 //1封号,2禁言
  45. }
  46. type mysqlChatInfo struct {
  47. uid uint64
  48. targetUid uint64
  49. msgType int32
  50. content *serverproto.ChatMessageInfo
  51. }
  52. //DBUpdate
  53. type DBUpdate struct {
  54. rocommon.UpdateModule //eventqueue.go
  55. initTime uint64
  56. }
  57. func (this *DBUpdate) Init() {
  58. this.initTime = util.GetTimeMilliseconds() + 2*1000
  59. MysqlUpdateMag = NewMysqlUpdateManager()
  60. MysqlUpdateMag.Init()
  61. }
  62. func (this *DBUpdate) Update(ms uint64) {
  63. }
  64. //MysqlUpdateManager
  65. type MysqlUpdateManager struct {
  66. rocommon.UpdateModule
  67. dataLock sync.Mutex
  68. mysqlProcessList set.Interface
  69. mysqlPlayerBriefInfoList map[uint64]*mysqlPlayerBriefInfo
  70. mysqlResInfoList map[uint64]*mysqlResInfo
  71. mysqlBanInfoList map[uint64]*mysqlBanInfo
  72. mysqlLogInfoList []*serverproto.SSRoleLogData
  73. mysqlChatInfoList []*mysqlChatInfo
  74. mysqlUpdatePool *MysqlUpdatePool
  75. }
  76. func NewMysqlUpdateManager() *MysqlUpdateManager {
  77. rm := &MysqlUpdateManager{
  78. mysqlProcessList: set.New(set.NonThreadSafe),
  79. mysqlPlayerBriefInfoList: map[uint64]*mysqlPlayerBriefInfo{},
  80. mysqlResInfoList: map[uint64]*mysqlResInfo{},
  81. mysqlBanInfoList: map[uint64]*mysqlBanInfo{},
  82. }
  83. //rm.updateTimer = util.NewDurationTimer(util.GetCurrentTime(), 1000*30)
  84. return rm
  85. }
  86. func (this *MysqlUpdateManager) Update(ms uint64) {
  87. }
  88. func (this *MysqlUpdateManager) Init() {
  89. this.mysqlUpdatePool = NewPool(20)
  90. go func() {
  91. defer func() {
  92. //打印奔溃信息
  93. if err := recover(); err != nil {
  94. util.InfoF("onError data=%v \n%s\n", err, string(debug.Stack()))
  95. }
  96. }()
  97. delayTimer := time.NewTimer(5 * time.Second)
  98. for {
  99. delayTimer.Reset(5 * time.Second)
  100. select {
  101. case <-delayTimer.C:
  102. case this.mysqlUpdatePool.qList <- 1:
  103. if !this.doMysqlProcess() {
  104. time.Sleep(50 * time.Millisecond)
  105. }
  106. }
  107. }
  108. }()
  109. }
  110. func (this *MysqlUpdateManager) doMysqlProcess() bool {
  111. //util.InfoF("doMysqlProcess...")
  112. bProcess := false
  113. //
  114. //this.dataLock.Lock()
  115. //if this.mysqlProcessList.Size() > 0 {
  116. // processDataInfo := this.mysqlProcessList.Pop()
  117. // switch item := processDataInfo.(type) {
  118. // case *mysqlPlayerBriefInfo:
  119. // doUpdatePlayerBriefInfo2MysqlORM(this, item.uid, item.briefInfo, item.bCreate, item.activeCode, item.openId, item.subPlatform)
  120. // bProcess = true
  121. // case *mysqlResInfo:
  122. // doUpdatePlayerRes2MysqlORM(this, item.uid, item.saveInfo)
  123. // bProcess = true
  124. // case *mysqlBanInfo:
  125. // DoUpdatePlayerBanInfo2MysqlORM(this, item.uid, item.banTime, item.banType)
  126. // bProcess = true
  127. // }
  128. //}
  129. //this.dataLock.Unlock()
  130. //if bProcess {
  131. // return bProcess
  132. //}
  133. //mysqlPlayerBriefInfoList
  134. this.dataLock.Lock()
  135. if len(this.mysqlPlayerBriefInfoList) > 0 {
  136. for uid, item := range this.mysqlPlayerBriefInfoList {
  137. //doUpdatePlayerBriefInfo2Mysql(this, uid, item.briefInfo, item.bCreate, item.activeCode, item.openId)
  138. doUpdatePlayerBriefInfo2MysqlORM(this, uid, item.briefInfo, item.bCreate, item.activeCode, item.openId, item.subPlatform)
  139. delete(this.mysqlPlayerBriefInfoList, uid)
  140. bProcess = true
  141. break
  142. }
  143. }
  144. this.dataLock.Unlock()
  145. if bProcess {
  146. return bProcess
  147. }
  148. //mysqlResInfoList
  149. this.dataLock.Lock()
  150. if len(this.mysqlResInfoList) > 0 {
  151. for uid, item := range this.mysqlResInfoList {
  152. //doUpdatePlayerRes2Mysql(this, uid, item.saveInfo)
  153. doUpdatePlayerRes2MysqlORM(this, uid, item.saveInfo)
  154. delete(this.mysqlResInfoList, uid)
  155. bProcess = true
  156. break
  157. }
  158. }
  159. this.dataLock.Unlock()
  160. if bProcess {
  161. return bProcess
  162. }
  163. //mysqlBanInfoList
  164. this.dataLock.Lock()
  165. if len(this.mysqlBanInfoList) > 0 {
  166. for uid, item := range this.mysqlBanInfoList {
  167. //DoUpdatePlayerBanInfo2Mysql(this, uid, item.banTime, item.banType)
  168. DoUpdatePlayerBanInfo2MysqlORM(this, uid, item.banTime, item.banType)
  169. delete(this.mysqlBanInfoList, uid)
  170. bProcess = true
  171. break
  172. }
  173. }
  174. this.dataLock.Unlock()
  175. if bProcess {
  176. return bProcess
  177. }
  178. //mysqlLogInfoList
  179. this.dataLock.Lock()
  180. if len(this.mysqlLogInfoList) > 0 {
  181. if len(this.mysqlLogInfoList) > 20 {
  182. tmpList := make([]*serverproto.SSRoleLogData, 20)
  183. copy(tmpList, this.mysqlLogInfoList[:20])
  184. this.mysqlLogInfoList = this.mysqlLogInfoList[20:]
  185. //doUpdatePlayerLog2Mysql(this, &tmpList)
  186. doUpdatePlayerLog2MysqlORM(this, &tmpList)
  187. } else {
  188. tmpList := make([]*serverproto.SSRoleLogData, len(this.mysqlLogInfoList))
  189. copy(tmpList, this.mysqlLogInfoList)
  190. this.mysqlLogInfoList = this.mysqlLogInfoList[:0]
  191. //doUpdatePlayerLog2Mysql(this, &tmpList)
  192. doUpdatePlayerLog2MysqlORM(this, &tmpList)
  193. }
  194. bProcess = true
  195. }
  196. this.dataLock.Unlock()
  197. if bProcess {
  198. return bProcess
  199. }
  200. //mysqlChatInfoList
  201. this.dataLock.Lock()
  202. if len(this.mysqlChatInfoList) > 0 {
  203. //var tmpList *[]mysqlChatInfo
  204. if len(this.mysqlChatInfoList) > 20 {
  205. tmpList := make([]*mysqlChatInfo, 20)
  206. copy(tmpList, this.mysqlChatInfoList[:20])
  207. this.mysqlChatInfoList = this.mysqlChatInfoList[20:]
  208. //doUpdatePlayerChatMsg2Mysql(this, &tmpList)
  209. doUpdatePlayerChatMsg2MysqlORM(this, &tmpList)
  210. } else {
  211. tmpList := make([]*mysqlChatInfo, len(this.mysqlChatInfoList))
  212. copy(tmpList, this.mysqlChatInfoList)
  213. this.mysqlChatInfoList = this.mysqlChatInfoList[:0]
  214. //doUpdatePlayerChatMsg2Mysql(this, &tmpList)
  215. doUpdatePlayerChatMsg2MysqlORM(this, &tmpList)
  216. }
  217. bProcess = true
  218. }
  219. this.dataLock.Unlock()
  220. if bProcess {
  221. return bProcess
  222. }
  223. this.mysqlUpdatePool.Release()
  224. return bProcess
  225. }