check_task.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package service
  2. import (
  3. "context"
  4. "sync"
  5. "time"
  6. "go-common/app/job/main/identify/model"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/log"
  9. "go-common/library/stat/prom"
  10. )
  11. func (s *Service) queryCookieDeleted() {
  12. var wg sync.WaitGroup
  13. ticker := time.NewTicker(time.Duration(s.c.CheckConf.Ticker))
  14. for {
  15. now := time.Now()
  16. for i := 0; i < 2; i++ {
  17. wg.Add(1)
  18. go func(i int, now time.Time) {
  19. defer wg.Done()
  20. s.readCookieData(dateFormat(addMonth(now, -i)))
  21. }(i, now)
  22. }
  23. wg.Wait()
  24. <-ticker.C
  25. }
  26. }
  27. func (s *Service) readCookieData(moth string) {
  28. var start int64
  29. for {
  30. cookies, err := s.d.CookieDeleted(context.Background(), start, s.c.CheckConf.Count, moth)
  31. if err != nil {
  32. log.Error("fail to get CookieDeleted error(%+v)", err)
  33. time.Sleep(100 * time.Millisecond)
  34. continue
  35. }
  36. if len(cookies) == 0 {
  37. log.Info("check cookie(%s) finished!", moth)
  38. return
  39. }
  40. maxID := int64(0)
  41. for _, a := range cookies {
  42. s.cookieCh[a.ID%int64(s.c.CheckConf.ChanNum)] <- a
  43. if maxID < a.ID {
  44. maxID = a.ID
  45. }
  46. }
  47. start = maxID
  48. }
  49. }
  50. func (s *Service) queryTokenDeleted() {
  51. var wg sync.WaitGroup
  52. ticker := time.NewTicker(time.Duration(s.c.CheckConf.Ticker))
  53. for {
  54. now := time.Now()
  55. for i := 0; i < 3; i++ {
  56. wg.Add(1)
  57. go func(i int, now time.Time) {
  58. defer wg.Done()
  59. s.readTokenData(dateFormat(addMonth(now, -i)))
  60. }(i, now)
  61. }
  62. wg.Wait()
  63. <-ticker.C
  64. }
  65. }
  66. func (s *Service) readTokenData(moth string) {
  67. var start int64
  68. for {
  69. tokens, err := s.d.TokenDeleted(context.Background(), start, s.c.CheckConf.Count, moth)
  70. if err != nil {
  71. log.Error("fail to get TokenDeleted error(%+v)", err)
  72. time.Sleep(100 * time.Millisecond)
  73. continue
  74. }
  75. if len(tokens) == 0 {
  76. log.Info("check token(%s) finished!", moth)
  77. return
  78. }
  79. maxID := int64(0)
  80. for _, a := range tokens {
  81. s.tokenCh[a.ID%int64(s.c.CheckConf.ChanNum)] <- a
  82. if maxID < a.ID {
  83. maxID = a.ID
  84. }
  85. }
  86. start = maxID
  87. }
  88. }
  89. func (s *Service) checkCookie(c chan *model.AuthCookie) {
  90. count := 0
  91. for {
  92. cookie, ok := <-c
  93. if !ok {
  94. log.Error("cookieChan closed")
  95. return
  96. }
  97. count = count + 1
  98. if count%10000 == 0 {
  99. count = 0
  100. time.Sleep(100 * time.Millisecond)
  101. }
  102. // auth
  103. for {
  104. res, err := s.d.CookieCache(context.Background(), cookie.Session)
  105. if err != nil {
  106. log.Error("fail to get cookie(%+v) cache from auth , error(%+v)", cookie, err)
  107. time.Sleep(100 * time.Millisecond)
  108. continue
  109. }
  110. if res != nil {
  111. prom.BusinessErrCount.Incr("auth:cacheNotDeleted")
  112. log.Error("auth cache not deleted, session(%s) mid(%d)", cookie.Session, cookie.Mid)
  113. for {
  114. err = s.d.DelCookieCache(context.Background(), cookie.Session)
  115. if err == nil {
  116. break
  117. }
  118. time.Sleep(100 * time.Millisecond)
  119. }
  120. }
  121. break
  122. }
  123. // identify
  124. s.checkIdentifyCache(cookie.Session, cookie.Mid)
  125. }
  126. }
  127. func (s *Service) checkToken(c chan *model.AuthToken) {
  128. count := 0
  129. for {
  130. token, ok := <-c
  131. if !ok {
  132. log.Error("tokenChan closed")
  133. return
  134. }
  135. count = count + 1
  136. if count%10000 == 0 {
  137. count = 0
  138. time.Sleep(100 * time.Millisecond)
  139. }
  140. // auth
  141. for {
  142. res, err := s.d.TokenCache(context.Background(), token.Token)
  143. if err != nil {
  144. log.Error("fail to get token(%+v) cache from auth , error(%+v)", token, err)
  145. time.Sleep(100 * time.Millisecond)
  146. continue
  147. }
  148. if res != nil {
  149. prom.BusinessErrCount.Incr("auth:cacheNotDeleted")
  150. log.Error("auth cache not deleted, token(%s) mid(%d)", token.Token, token.Mid)
  151. for {
  152. err = s.d.DelTokenCache(context.Background(), token.Token)
  153. if err == nil {
  154. break
  155. }
  156. time.Sleep(100 * time.Millisecond)
  157. }
  158. }
  159. break
  160. }
  161. // identify
  162. s.checkIdentifyCache(token.Token, token.Mid)
  163. }
  164. }
  165. func (s *Service) checkIdentifyCache(k string, mid int64) {
  166. for name, p := range s.poolm {
  167. mcc, ok := s.c.Memcaches[name]
  168. if !ok || mcc == nil {
  169. return
  170. }
  171. key := mcc.Prefix + k
  172. for {
  173. conn := p.Get(context.Background())
  174. res, err := conn.Get(key)
  175. conn.Close()
  176. if err != nil {
  177. if err == memcache.ErrNotFound {
  178. break
  179. }
  180. log.Error("fail to get cache(%s) from identify , error(%+v)", key, err)
  181. time.Sleep(100 * time.Millisecond)
  182. continue
  183. }
  184. if res != nil {
  185. prom.BusinessErrCount.Incr("identify:cacheNotDeleted")
  186. log.Error("identify cache not deleted, key(%s) mid(%d) cache(%s)", key, mid, name)
  187. for {
  188. conn := p.Get(context.Background())
  189. err := conn.Delete(key)
  190. conn.Close()
  191. if err == nil || err == memcache.ErrNotFound {
  192. break
  193. }
  194. log.Error("dao.DelCache(%s) error(%+v)", key, err)
  195. time.Sleep(100 * time.Millisecond)
  196. }
  197. }
  198. break
  199. }
  200. }
  201. }
  202. func dateFormat(t time.Time) string {
  203. return t.Format("200601")
  204. }
  205. func addMonth(t time.Time, delta int) time.Time {
  206. if delta == 0 {
  207. return t
  208. }
  209. year, month, _ := t.Date()
  210. thisMonthFirstDay := time.Date(year, month, 1, 1, 1, 1, 1, t.Location())
  211. return thisMonthFirstDay.AddDate(0, delta, 0)
  212. }