manager.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package handler
  2. import (
  3. "sync"
  4. "time"
  5. "bet24.com/servers/common"
  6. pb "bet24.com/servers/micros/dotservice/proto"
  7. "bet24.com/log"
  8. )
  9. var mgr *dotMgr
  10. type dotMgr struct {
  11. config_list []*pb.DotConfig
  12. user_list map[string]*userInfo // 用户列表
  13. lock *sync.RWMutex // 用户锁
  14. }
  15. func newDotMgr() {
  16. mgr = new(dotMgr)
  17. mgr.lock = &sync.RWMutex{}
  18. mgr.user_list = make(map[string]*userInfo)
  19. mgr.loadConfig()
  20. mgr.doCheck()
  21. return
  22. }
  23. // 轮询
  24. func (this *dotMgr) doCheck() {
  25. ticker := time.NewTicker(30 * time.Second)
  26. go func(t *time.Ticker) {
  27. for {
  28. select {
  29. case <-t.C:
  30. this.checkExpire()
  31. }
  32. }
  33. }(ticker)
  34. }
  35. // 检查用户
  36. func (this *dotMgr) checkExpire() {
  37. this.lock.Lock()
  38. defer this.lock.Unlock()
  39. // 遍历所有用户
  40. for _, u := range this.user_list {
  41. // 判断是否过期
  42. if !u.dealExpire() {
  43. continue
  44. }
  45. // 删除
  46. delete(this.user_list, u.userId)
  47. log.Debug("manager.checkExpire userId=%s 已完成清理", u.userId)
  48. }
  49. return
  50. }
  51. // 获取用户
  52. func (this *dotMgr) getUser(userId string) *userInfo {
  53. this.lock.RLock()
  54. u, ok := this.user_list[userId]
  55. this.lock.RUnlock()
  56. if ok {
  57. return u
  58. }
  59. u = newUserInfo(userId)
  60. this.lock.Lock()
  61. this.user_list[userId] = u
  62. this.lock.Unlock()
  63. return u
  64. }
  65. // 添加打点
  66. func (this *dotMgr) addDot(userId string, scope pb.DotScope) {
  67. u := this.getUser(userId)
  68. if u == nil {
  69. log.Debug("manager.addDot userId=%s scope=%+v", userId, scope)
  70. return
  71. }
  72. log.Debug("manager.addDot userId=%s scope=%+v", userId, scope)
  73. // 添加打点
  74. u.add(scope)
  75. return
  76. }
  77. // 获取打点统计
  78. func (this *dotMgr) getStatList(beginTime, endTime, event string) []pb.StatInfo {
  79. list := trans_statList(beginTime, endTime, event)
  80. return list
  81. }
  82. // 获取打点任务统计
  83. func (this *dotMgr) getTaskStatList(beginTime, endTime string) []pb.TaskStatInfo {
  84. list := trans_taskStatList(beginTime, endTime)
  85. return list
  86. }
  87. // 关服前保存数据
  88. func (this *dotMgr) dumpClear() {
  89. this.lock.Lock()
  90. defer this.lock.Unlock()
  91. // 遍历所有用户
  92. for _, u := range this.user_list {
  93. // 保存并清理数据
  94. u.saveAndClearData()
  95. // 删除
  96. delete(this.user_list, u.userId)
  97. log.Debug("manager.dumpClear userId=%s 已完成清理", u.userId)
  98. }
  99. }
  100. func (this *dotMgr) dumpConfig() {
  101. log.Debug(" ^_^ 开始打印配置数据,(%d)个", len(this.config_list))
  102. for _, v := range this.config_list {
  103. log.Debug("%+v", v)
  104. }
  105. log.Debug("完成数据打印 ^_^")
  106. }
  107. func (this *dotMgr) dumpUser(param1 string) {
  108. userId := param1
  109. log.Debug(" ^_^ 开始打印用户数据,用户(%d)人", len(this.user_list))
  110. this.lock.RLock()
  111. for uid, v := range this.user_list {
  112. if userId != "" && userId != uid {
  113. continue
  114. }
  115. log.Debug("用户[%s]信息,缓存数据=%+v,过期时间[%s]:", uid, v.DotScope, time.Unix(int64(v.timeStamp), 0).Format(common.Layout))
  116. log.Debug("打点列表打印(%d)个:", len(v.dot_list))
  117. for _, d := range v.dot_list {
  118. log.Debug("%+v", d)
  119. }
  120. log.Debug("**********************************************")
  121. }
  122. this.lock.RUnlock()
  123. log.Debug("完成用户数据打印 ^_^")
  124. }