redpointmgr.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package redpoint
  2. import (
  3. "bet24.com/log"
  4. "sync"
  5. )
  6. type redpointmgr struct {
  7. lock *sync.RWMutex
  8. user_list map[int]*user_redPoint
  9. }
  10. func newRedpointMgr() *redpointmgr {
  11. ret := new(redpointmgr)
  12. ret.lock = &sync.RWMutex{}
  13. ret.user_list = make(map[int]*user_redPoint)
  14. log.Debug("redpoint manager running")
  15. return ret
  16. }
  17. func (this *redpointmgr) check(userId int) *redPoint {
  18. user := this.getUser(userId)
  19. if user == nil {
  20. log.Debug("redpointmgr.check userId[%d] is not exist", userId)
  21. return nil
  22. }
  23. return user.check()
  24. }
  25. func (this *redpointmgr) getUser(userId int) *user_redPoint {
  26. this.lock.RLock()
  27. user, ok := this.user_list[userId]
  28. this.lock.RUnlock()
  29. if !ok {
  30. user = newUserRedpoint(userId)
  31. if user == nil {
  32. return nil
  33. }
  34. this.lock.Lock()
  35. this.user_list[userId] = user
  36. this.lock.Unlock()
  37. }
  38. return user
  39. }
  40. func (this *redpointmgr) onUserEnter(userId int) {
  41. this.lock.Lock()
  42. defer this.lock.Unlock()
  43. _, ok := this.user_list[userId]
  44. if !ok {
  45. this.user_list[userId] = newUserRedpoint(userId)
  46. }
  47. }
  48. func (this *redpointmgr) onUserExit(userId int) {
  49. this.lock.Lock()
  50. defer this.lock.Unlock()
  51. u, _ := this.user_list[userId]
  52. if u != nil {
  53. u.destructor()
  54. delete(this.user_list, userId)
  55. }
  56. }
  57. func (this *redpointmgr) dump(param string) {
  58. log.Release("-------------------------------")
  59. log.Release("redpointmgr.dump")
  60. defer func() {
  61. log.Release("+++++++++++++++++++++++++++++++")
  62. log.Release("")
  63. }()
  64. this.lock.RLock()
  65. log.Release(" total user count[%d]", len(this.user_list))
  66. for k, v := range this.user_list {
  67. log.Release(" %d,userId[%d],counter[%d]", k, v.userId, v.counter)
  68. }
  69. this.lock.RUnlock()
  70. }