frozen.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "go-common/app/job/main/vip/model"
  7. "go-common/library/log"
  8. "go-common/library/queue/databus"
  9. "github.com/pkg/errors"
  10. )
  11. func (s *Service) accloginproc() {
  12. defer func() {
  13. if r := recover(); r != nil {
  14. r = errors.WithStack(r.(error))
  15. log.Error("Runtime error caught: %+v", r)
  16. go s.accloginproc()
  17. }
  18. }()
  19. var (
  20. err error
  21. msgChan = s.accLogin.Messages()
  22. msg *databus.Message
  23. ok bool
  24. )
  25. for {
  26. msg, ok = <-msgChan
  27. log.Info("login ip msg %+v", string(msg.Value))
  28. if !ok {
  29. log.Info("accLogin msgChan closed")
  30. }
  31. if err = msg.Commit(); err != nil {
  32. log.Error("msg.Commit err(%+v)", err)
  33. }
  34. m := &model.LoginLog{}
  35. if err = json.Unmarshal([]byte(msg.Value), m); err != nil {
  36. log.Error("json.Unmarshal(%v) err(%+v)", m, err)
  37. continue
  38. }
  39. s.Frozen(context.TODO(), m)
  40. }
  41. }
  42. // Frozen handle vip frozen logic.
  43. func (s *Service) Frozen(c context.Context, ll *model.LoginLog) (err error) {
  44. var (
  45. lc int64
  46. uvs *model.VipUserInfo
  47. ctx = context.TODO()
  48. )
  49. // 判定用户是否为vip
  50. if uvs, err = s.dao.VipStatus(ctx, ll.Mid); err != nil {
  51. log.Error("s.dao.VipStatus(%d)err(%+v)", ll.Mid, err)
  52. return
  53. }
  54. if uvs == nil || uvs.Status == model.VipStatusOverTime {
  55. log.Warn("user(%d) not vip.(%+v)", ll.Mid, uvs)
  56. return
  57. }
  58. // 判定是否为15分钟4次以上不同ip登录
  59. if err = s.dao.AddLogginIP(ctx, ll.Mid, ll.IP); err != nil {
  60. log.Error("s.dao.AddLogginIP(%d)err(%+v)", ll.Mid, err)
  61. return
  62. }
  63. if lc, err = s.dao.LoginCount(ctx, ll.Mid); err != nil {
  64. log.Error("s.dao.LoginCount(%d)err(%+v)", ll.Mid, err)
  65. return
  66. }
  67. if lc >= s.c.Property.FrozenLimit {
  68. if err = s.dao.Enqueue(ctx, ll.Mid, time.Now().Add(s.frozenDate).Unix()); err != nil {
  69. log.Error("enqueue error(%+v)", err)
  70. }
  71. if err = s.dao.SetVipFrozen(ctx, ll.Mid); err != nil {
  72. log.Error("set vip frozen err(%+v)", err)
  73. }
  74. //通知业务方清理缓存
  75. s.cleanCache(ll.Mid)
  76. if err = s.notifyOldVip(ll.Mid, -1); err != nil {
  77. log.Error("del vip java frozen err(%+v)", err)
  78. }
  79. log.Info("mid(%+v) frozen success", ll.Mid)
  80. }
  81. return
  82. }
  83. // unFrozenJob timing to unFrozen vip user
  84. func (s *Service) unFrozenJob() {
  85. log.Info("unfrozen job start........................................")
  86. defer func() {
  87. if r := recover(); r != nil {
  88. r = errors.WithStack(r.(error))
  89. log.Error("recover panic error(%+v)", r)
  90. }
  91. log.Info("unfrozen job end.............................")
  92. }()
  93. var (
  94. err error
  95. mids []int64
  96. ctx = context.TODO()
  97. )
  98. if mids, err = s.dao.Dequeue(ctx); err != nil {
  99. log.Error("s.dao.Dequeue err(%+v)", err)
  100. return
  101. }
  102. for _, mid := range mids {
  103. if err = s.dao.RemQueue(ctx, mid); err != nil {
  104. log.Error("s.dao.RemQueue(%d)err(%+v)", mid, err)
  105. continue
  106. }
  107. if err = s.dao.DelCache(ctx, mid); err != nil {
  108. log.Error("del cache mid(%+v) err(%+v)", mid, err)
  109. }
  110. if err = s.dao.DelVipFrozen(ctx, mid); err != nil {
  111. log.Error("del vip frozen err(%+v)", err)
  112. }
  113. if err = s.notifyOldVip(mid, 0); err != nil {
  114. log.Error("del vip java frozen err(%+v)", err)
  115. }
  116. s.cleanCache(mid)
  117. log.Info("mid(%+v) unfrozen success", mid)
  118. }
  119. }
  120. // FIXME AFTER REMOVE JAVA.
  121. func (s *Service) notifyOldVip(mid, status int64) error {
  122. return s.dao.OldFrozenChange(mid, status)
  123. }