clean_cache.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package service
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "encoding/hex"
  6. "encoding/json"
  7. "strings"
  8. "time"
  9. "go-common/app/job/main/passport-auth/model"
  10. "go-common/library/log"
  11. "go-common/library/queue/databus"
  12. )
  13. type authTokenBMsg struct {
  14. Action string
  15. Table string
  16. New *model.AuthToken
  17. }
  18. type authCookieBMsg struct {
  19. Action string
  20. Table string
  21. New *model.AuthCookie
  22. }
  23. func (s *Service) authConsumeProc() {
  24. // fill callbacks
  25. s.authGroup.New = func(msg *databus.Message) (res interface{}, err error) {
  26. bmsg := new(model.BMsg)
  27. if err = json.Unmarshal(msg.Value, &bmsg); err != nil {
  28. log.Error("json.Unmarshal(%s) error(%v)", msg.Value, err)
  29. return
  30. }
  31. log.Info("receive auth msg action(%s) table(%s) key(%s) partition(%d) offset(%d) timestamp(%d) New(%s) Old(%s)",
  32. bmsg.Action, bmsg.Table, msg.Key, msg.Partition, msg.Offset, msg.Timestamp, string(bmsg.New), string(bmsg.Old))
  33. if strings.HasPrefix(bmsg.Table, "user_token_") {
  34. tokenBMsg := &authTokenBMsg{
  35. Action: bmsg.Action,
  36. Table: bmsg.Table,
  37. }
  38. newToken := new(model.AuthToken)
  39. if err = json.Unmarshal(bmsg.New, &newToken); err != nil {
  40. log.Error("json.Unmarshal(%s) error(%v)", bmsg.New, err)
  41. return
  42. }
  43. tokenBMsg.New = newToken
  44. return tokenBMsg, nil
  45. } else if strings.HasPrefix(bmsg.Table, "user_cookie_") {
  46. cookieBMsg := &authCookieBMsg{
  47. Action: bmsg.Action,
  48. Table: bmsg.Table,
  49. }
  50. newCookie := new(model.AuthCookie)
  51. if err = json.Unmarshal(bmsg.New, newCookie); err != nil {
  52. log.Error("json.Unmarshal(%s) error(%v)", bmsg.New, err)
  53. return
  54. }
  55. cookieBMsg.New = newCookie
  56. return cookieBMsg, nil
  57. }
  58. return
  59. }
  60. s.authGroup.Split = func(msg *databus.Message, data interface{}) int {
  61. if t, ok := data.(*authTokenBMsg); ok {
  62. return int(t.New.Mid)
  63. } else if t, ok := data.(*authCookieBMsg); ok {
  64. return int(t.New.Mid)
  65. }
  66. return 0
  67. }
  68. s.authGroup.Do = func(msgs []interface{}) {
  69. for _, m := range msgs {
  70. if msg, ok := m.(*authTokenBMsg); ok {
  71. if msg.Action != "delete" {
  72. return
  73. }
  74. for {
  75. if err := s.cleanTokenCache(msg.New.Token, msg.New.Mid); err != nil {
  76. time.Sleep(100 * time.Millisecond)
  77. continue
  78. }
  79. break
  80. }
  81. } else if msg, ok := m.(*authCookieBMsg); ok {
  82. if msg.Action != "delete" {
  83. return
  84. }
  85. for {
  86. if err := s.cleanCookieCache(msg.New.Session, msg.New.Mid); err != nil {
  87. time.Sleep(100 * time.Millisecond)
  88. continue
  89. }
  90. break
  91. }
  92. }
  93. }
  94. }
  95. // start the group
  96. s.authGroup.Start()
  97. }
  98. func (s *Service) cleanTokenCache(tokenBase64 string, mid int64) (err error) {
  99. var bytes []byte
  100. if bytes, err = base64.StdEncoding.DecodeString(tokenBase64); err != nil {
  101. log.Error("cleanTokenCache base64 decode err %v", err)
  102. err = nil
  103. return
  104. }
  105. token := hex.EncodeToString(bytes)
  106. if err = s.authRPC.DelTokenCache(context.Background(), token); err != nil {
  107. log.Error("cleanTokenCache err, %v", err)
  108. return
  109. }
  110. if err = s.dao.AsoCleanCache(context.Background(), token, "", mid); err != nil {
  111. return
  112. }
  113. return
  114. }
  115. func (s *Service) cleanCookieCache(cookieBase64 string, mid int64) (err error) {
  116. var bytes []byte
  117. if bytes, err = base64.StdEncoding.DecodeString(cookieBase64); err != nil {
  118. log.Error("cleanCookieCache base64 decode err %v", err)
  119. err = nil
  120. return
  121. }
  122. session := string(bytes)
  123. if err = s.authRPC.DelCookieCookie(context.Background(), session); err != nil {
  124. log.Error("cleanCookieCache err, %v", err)
  125. return
  126. }
  127. if err = s.dao.AsoCleanCache(context.Background(), "", session, mid); err != nil {
  128. return
  129. }
  130. return
  131. }