account.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "go-common/app/job/main/passport-game-cloud/model"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _memberTableUpdateDuration = time.Second * 1
  11. )
  12. func (s *Service) processMemberInfo(bmsg *model.BMsg) {
  13. n := new(model.Info)
  14. if err := json.Unmarshal(bmsg.New, n); err != nil {
  15. log.Error("failed to parse binlog new, json.Unmarshal(%s) error(%v)", string(bmsg.New), err)
  16. return
  17. }
  18. s.memberInterval.Prom(context.TODO(), bmsg.MTS)
  19. switch bmsg.Action {
  20. case "insert":
  21. s.addMemberInfo(context.TODO(), n)
  22. s.delInfoCache(context.TODO(), n.Mid)
  23. case "update":
  24. old := new(model.Info)
  25. if err := json.Unmarshal(bmsg.Old, old); err != nil {
  26. log.Error("failed to parse binlog old, json.Unmarshal(%s) error(%v)", string(bmsg.Old), err)
  27. return
  28. }
  29. if n.Equals(old) {
  30. return
  31. }
  32. s.addMemberInfo(context.TODO(), n)
  33. s.delInfoCache(context.TODO(), n.Mid)
  34. case "delete":
  35. s.delMemberInfo(context.TODO(), n.Mid)
  36. s.delInfoCache(context.TODO(), n.Mid)
  37. }
  38. }
  39. func (s *Service) addMemberInfo(c context.Context, info *model.Info) (err error) {
  40. for {
  41. if _, err = s.d.AddMemberInfo(c, info); err == nil {
  42. break
  43. }
  44. time.Sleep(_memberTableUpdateDuration)
  45. }
  46. return
  47. }
  48. func (s *Service) delMemberInfo(c context.Context, mid int64) (err error) {
  49. for {
  50. if _, err = s.d.DelMemberInfo(c, mid); err == nil {
  51. break
  52. }
  53. time.Sleep(_memberTableUpdateDuration)
  54. }
  55. return
  56. }
  57. func (s *Service) delInfoCache(c context.Context, mid int64) (err error) {
  58. for i := 0; i < _accountCacheRetryCount; i++ {
  59. if err = s.d.DelInfoCache(c, mid); err == nil {
  60. break
  61. }
  62. time.Sleep(_accountCacheRetryDuration)
  63. }
  64. return
  65. }