service.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package service
  2. import (
  3. "context"
  4. "math/rand"
  5. "time"
  6. hisrpc "go-common/app/interface/main/history/rpc/client"
  7. tagrpc "go-common/app/interface/main/tag/rpc/client"
  8. "go-common/app/interface/openplatform/article/conf"
  9. "go-common/app/interface/openplatform/article/dao"
  10. artmdl "go-common/app/interface/openplatform/article/model"
  11. search "go-common/app/interface/openplatform/article/model/search"
  12. account "go-common/app/service/main/account/model"
  13. accrpc "go-common/app/service/main/account/rpc/client"
  14. arcrpc "go-common/app/service/main/archive/api/gorpc"
  15. arcmdl "go-common/app/service/main/archive/model/archive"
  16. coinrpc "go-common/app/service/main/coin/api/gorpc"
  17. favrpc "go-common/app/service/main/favorite/api/gorpc"
  18. filterrpc "go-common/app/service/main/filter/rpc/client"
  19. resrpc "go-common/app/service/main/resource/rpc/client"
  20. thumbuprpc "go-common/app/service/main/thumbup/rpc/client"
  21. xcache "go-common/library/cache"
  22. "go-common/library/ecode"
  23. "go-common/library/log"
  24. "go-common/library/log/anticheat"
  25. "go-common/library/net/rpc/warden"
  26. )
  27. const _segmentAddr = "127.0.01:6755"
  28. // cache proc
  29. var cache *xcache.Cache
  30. // Service service
  31. type Service struct {
  32. c *conf.Config
  33. dao *dao.Dao
  34. // rpc
  35. accountRPC *accrpc.Service3
  36. tagRPC *tagrpc.Service
  37. favRPC *favrpc.Service
  38. thumbupRPC thumbuprpc.ThumbupRPC
  39. arcRPC *arcrpc.Service2
  40. coinRPC coinrpc.RPC
  41. resRPC *resrpc.Service
  42. filterRPC *filterrpc.Service
  43. hisRPC *hisrpc.Service
  44. searchRPC search.TagboxServiceClient
  45. categoriesMap map[int64]*artmdl.Category
  46. categoriesReverseMap map[int64][]*artmdl.Category
  47. categoryParents map[int64][]*artmdl.Category
  48. primaryCategories []*artmdl.Category
  49. Categories artmdl.Categories
  50. RecommendsMap map[int64][][]*artmdl.Recommend
  51. RecommendsGroups map[int64]map[int64]bool
  52. recommendChan chan [2]int64
  53. recommendAids map[int64][]int64
  54. setting *artmdl.Setting
  55. activities map[int64]*artmdl.Activity
  56. // infoc
  57. logCh chan interface{}
  58. //banner
  59. bannersMap map[int8][]*artmdl.Banner
  60. actBannersMap map[int8][]*artmdl.Banner
  61. // rank
  62. ranksMap map[int64]bool
  63. sortLimitTime int64
  64. notices []*artmdl.Notice
  65. CheatInfoc *anticheat.AntiCheat
  66. }
  67. // New new
  68. func New(c *conf.Config) *Service {
  69. rand.Seed(time.Now().Unix())
  70. s := &Service{
  71. c: c,
  72. dao: dao.New(c),
  73. accountRPC: accrpc.New3(c.AccountRPC),
  74. tagRPC: tagrpc.New2(c.TagRPC),
  75. favRPC: favrpc.New2(c.FavRPC),
  76. arcRPC: arcrpc.New2(c.ArcRPC),
  77. coinRPC: coinrpc.New(c.CoinRPC),
  78. resRPC: resrpc.New(c.ResRPC),
  79. thumbupRPC: thumbuprpc.New(c.ThumbupRPC),
  80. filterRPC: filterrpc.New(c.FilterRPC),
  81. hisRPC: hisrpc.New(c.HistoryRPC),
  82. searchRPC: searchRPC(c.SearchRPC),
  83. categoriesMap: make(map[int64]*artmdl.Category),
  84. categoriesReverseMap: make(map[int64][]*artmdl.Category),
  85. categoryParents: make(map[int64][]*artmdl.Category),
  86. logCh: make(chan interface{}, 1024),
  87. recommendChan: make(chan [2]int64, 10240),
  88. recommendAids: make(map[int64][]int64),
  89. ranksMap: make(map[int64]bool),
  90. sortLimitTime: int64(time.Duration(c.Article.SortLimitTime) / time.Second),
  91. CheatInfoc: anticheat.New(c.CheatInfoc),
  92. RecommendsGroups: make(map[int64]map[int64]bool),
  93. }
  94. s.loadCategories()
  95. s.loadSettings()
  96. s.loadRanks()
  97. go s.loadCategoriesproc()
  98. go s.loadSettingsproc()
  99. go s.loadNoticeproc()
  100. go s.infocproc()
  101. go s.loadRecommendsproc()
  102. go s.loadBannersproc()
  103. go s.loadActBannersproc()
  104. go s.deleteRecommendproc()
  105. go s.loadActivityproc()
  106. return s
  107. }
  108. func (s *Service) loadRecommendsproc() {
  109. for {
  110. now := time.Now().Unix()
  111. c := context.TODO()
  112. if (s.RecommendsMap == nil) || (now%s.dao.UpdateRecommendsInterval == 0) {
  113. err := s.UpdateRecommends(c)
  114. if err != nil {
  115. dao.PromError("service:更新推荐数据")
  116. time.Sleep(time.Second)
  117. continue
  118. }
  119. if err = s.groupRecommend(c); err != nil {
  120. log.Error("s.groupRecommend error(%+v)", err)
  121. }
  122. }
  123. // 这里不是每秒钟一更新
  124. time.Sleep(time.Second)
  125. }
  126. }
  127. // Close close dao.
  128. func (s *Service) Close() {
  129. s.dao.Close()
  130. }
  131. // Ping check connection success.
  132. func (s *Service) Ping(c context.Context) (err error) {
  133. err = s.dao.Ping(c)
  134. return
  135. }
  136. // UserDisabled check user is disabled
  137. func (s *Service) UserDisabled(c context.Context, mid int64) (res bool, level int, err error) {
  138. arg := account.ArgMid{Mid: mid}
  139. card, err := s.accountRPC.Card3(c, &arg)
  140. if (err == ecode.UserNotExist) || (err == ecode.MemberNotExist) {
  141. return false, 0, nil
  142. }
  143. if err != nil {
  144. dao.PromError("service:用户封禁状态")
  145. log.Error("s.accountRPC.Card2(%+v) err: %+v", arg, err)
  146. return
  147. }
  148. if card.Silence == 1 {
  149. res = true
  150. }
  151. level = int(card.Level)
  152. return
  153. }
  154. func (s *Service) isUpper(c context.Context, mid int64) (res bool, err error) {
  155. arg := &arcmdl.ArgUpCount2{Mid: mid}
  156. var count int
  157. if count, err = s.arcRPC.UpCount2(c, arg); err != nil {
  158. dao.PromError("service:up主投稿")
  159. log.Error("s.arcRPC.UpCount2(%v) err: %+v", mid, err)
  160. return
  161. }
  162. if count > 0 {
  163. res = true
  164. }
  165. return
  166. }
  167. func (s *Service) loadActivityproc() {
  168. for {
  169. if acts, err := s.dao.Activity(context.TODO()); err == nil {
  170. s.activities = acts
  171. }
  172. time.Sleep(time.Minute)
  173. }
  174. }
  175. func init() {
  176. cache = xcache.New(1, 1024)
  177. }
  178. func searchRPC(cfg *warden.ClientConfig) search.TagboxServiceClient {
  179. cc, err := warden.NewClient(cfg).Dial(context.Background(), "discovery://default/search.tagbox")
  180. if err != nil {
  181. panic(err)
  182. }
  183. return search.NewTagboxServiceClient(cc)
  184. }