service.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package feed
  2. import (
  3. "time"
  4. "go-common/app/interface/main/app-card/model/card/ai"
  5. "go-common/app/interface/main/app-card/model/card/live"
  6. "go-common/app/interface/main/app-card/model/card/operate"
  7. "go-common/app/interface/main/app-card/model/card/rank"
  8. "go-common/app/interface/main/app-feed/conf"
  9. accdao "go-common/app/interface/main/app-feed/dao/account"
  10. addao "go-common/app/interface/main/app-feed/dao/ad"
  11. arcdao "go-common/app/interface/main/app-feed/dao/archive"
  12. artdao "go-common/app/interface/main/app-feed/dao/article"
  13. audiodao "go-common/app/interface/main/app-feed/dao/audio"
  14. adtdao "go-common/app/interface/main/app-feed/dao/audit"
  15. bgmdao "go-common/app/interface/main/app-feed/dao/bangumi"
  16. blkdao "go-common/app/interface/main/app-feed/dao/black"
  17. bplusdao "go-common/app/interface/main/app-feed/dao/bplus"
  18. carddao "go-common/app/interface/main/app-feed/dao/card"
  19. cvgdao "go-common/app/interface/main/app-feed/dao/converge"
  20. gamedao "go-common/app/interface/main/app-feed/dao/game"
  21. livdao "go-common/app/interface/main/app-feed/dao/live"
  22. locdao "go-common/app/interface/main/app-feed/dao/location"
  23. rankdao "go-common/app/interface/main/app-feed/dao/rank"
  24. rcmdao "go-common/app/interface/main/app-feed/dao/recommend"
  25. reldao "go-common/app/interface/main/app-feed/dao/relation"
  26. rscdao "go-common/app/interface/main/app-feed/dao/resource"
  27. searchdao "go-common/app/interface/main/app-feed/dao/search"
  28. showdao "go-common/app/interface/main/app-feed/dao/show"
  29. specdao "go-common/app/interface/main/app-feed/dao/special"
  30. tabdao "go-common/app/interface/main/app-feed/dao/tab"
  31. tagdao "go-common/app/interface/main/app-feed/dao/tag"
  32. updao "go-common/app/interface/main/app-feed/dao/upper"
  33. "go-common/app/interface/main/app-feed/model"
  34. "go-common/app/interface/main/app-feed/model/feed"
  35. resource "go-common/app/service/main/resource/model"
  36. "go-common/library/log"
  37. "go-common/library/stat/prom"
  38. )
  39. var (
  40. _emptyItem = []*feed.Item{}
  41. )
  42. // Service is show service.
  43. type Service struct {
  44. c *conf.Config
  45. pHit *prom.Prom
  46. pMiss *prom.Prom
  47. // dao
  48. rcmd *rcmdao.Dao
  49. bgm *bgmdao.Dao
  50. tg *tagdao.Dao
  51. adt *adtdao.Dao
  52. blk *blkdao.Dao
  53. lv *livdao.Dao
  54. ad *addao.Dao
  55. rank *rankdao.Dao
  56. gm *gamedao.Dao
  57. sp *specdao.Dao
  58. cvg *cvgdao.Dao
  59. show *showdao.Dao
  60. tab *tabdao.Dao
  61. audio *audiodao.Dao
  62. // rpc
  63. arc *arcdao.Dao
  64. acc *accdao.Dao
  65. rel *reldao.Dao
  66. upper *updao.Dao
  67. art *artdao.Dao
  68. rsc *rscdao.Dao
  69. card *carddao.Dao
  70. search *searchdao.Dao
  71. bplus *bplusdao.Dao
  72. loc *locdao.Dao
  73. // tick
  74. tick time.Duration
  75. // audit cache
  76. auditCache map[string]map[int]struct{} // audit mobi_app builds
  77. // black cache
  78. blackCache map[int64]struct{} // black aids
  79. // ai cache
  80. rcmdCache []*ai.Item
  81. // rank
  82. rankCache []*rank.Rank
  83. // converge cache
  84. convergeCache map[int64]*operate.Converge
  85. // download cache
  86. downloadCache map[int64]*operate.Download
  87. // special cache
  88. specialCache map[int64]*operate.Special
  89. // follow cache
  90. followCache map[int64]*operate.Follow
  91. liveCardCache map[int64][]*live.Card
  92. // tab cache
  93. menuCache []*operate.Menu
  94. tabCache map[int64][]*operate.Active
  95. coverCache map[int64]string
  96. // group cache
  97. groupCache map[int64]int
  98. // cache
  99. cacheCh chan func()
  100. // infoc
  101. logCh chan interface{}
  102. // ad
  103. cmResourceMap map[int8]int64
  104. // abtest cache
  105. abtestCache map[string]*resource.AbTest
  106. // autoplay mids cache
  107. autoplayMidsCache map[int64]struct{}
  108. // follow mode list
  109. followModeList map[int64]struct{}
  110. }
  111. // New new a show service.
  112. func New(c *conf.Config) (s *Service) {
  113. s = &Service{
  114. c: c,
  115. pHit: prom.CacheHit,
  116. pMiss: prom.CacheMiss,
  117. // dao
  118. rcmd: rcmdao.New(c),
  119. bgm: bgmdao.New(c),
  120. tg: tagdao.New(c),
  121. adt: adtdao.New(c),
  122. blk: blkdao.New(c),
  123. lv: livdao.New(c),
  124. ad: addao.New(c),
  125. rank: rankdao.New(c),
  126. gm: gamedao.New(c),
  127. cvg: cvgdao.New(c),
  128. sp: specdao.New(c),
  129. show: showdao.New(c),
  130. tab: tabdao.New(c),
  131. audio: audiodao.New(c),
  132. card: carddao.New(c),
  133. search: searchdao.New(c),
  134. bplus: bplusdao.New(c),
  135. // rpc
  136. arc: arcdao.New(c),
  137. rel: reldao.New(c),
  138. acc: accdao.New(c),
  139. upper: updao.New(c),
  140. art: artdao.New(c),
  141. rsc: rscdao.New(c),
  142. loc: locdao.New(c),
  143. // tick
  144. tick: time.Duration(c.Tick),
  145. // group cache
  146. groupCache: map[int64]int{},
  147. // cache
  148. cacheCh: make(chan func(), 1024),
  149. // infoc
  150. logCh: make(chan interface{}, 1024),
  151. // abtest cache
  152. abtestCache: map[string]*resource.AbTest{},
  153. // autoplay mids cache
  154. autoplayMidsCache: map[int64]struct{}{},
  155. }
  156. var err error
  157. if s.cmResourceMap, err = s.coverCMResource(c.Feed.CMResource); err != nil {
  158. panic(err)
  159. }
  160. s.loadAuditCache()
  161. s.loadBlackCache()
  162. s.loadRcmdCache()
  163. s.loadRankCache()
  164. s.loadConvergeCache()
  165. s.loadDownloadCache()
  166. s.loadSpecialCache()
  167. s.loadTabCache()
  168. s.loadGroupCache()
  169. s.loadUpCardCache()
  170. s.loadLiveCardCache()
  171. s.loadABTestCache()
  172. s.loadAutoPlayMid()
  173. s.loadFollowModeList()
  174. go s.cacheproc()
  175. go s.auditproc()
  176. go s.blackproc()
  177. go s.rcmdproc()
  178. go s.rankproc()
  179. go s.convergeproc()
  180. go s.downloadproc()
  181. go s.specialproc()
  182. go s.tabproc()
  183. go s.groupproc()
  184. go s.infocproc()
  185. go s.upCardproc()
  186. go s.liveCardproc()
  187. go s.loadABTestCacheProc()
  188. go s.followModeListproc()
  189. return
  190. }
  191. func (s *Service) coverCMResource(resource map[string]int64) (rscm map[int8]int64, err error) {
  192. if len(resource) == 0 {
  193. return
  194. }
  195. rscm = make(map[int8]int64, len(resource))
  196. for mobiApp, r := range resource {
  197. var plat int8
  198. if mobiApp == "iphone" {
  199. plat = model.PlatIPhone
  200. } else if mobiApp == "android" {
  201. plat = model.PlatAndroid
  202. } else if mobiApp == "ipad" {
  203. plat = model.PlatIPad
  204. }
  205. rscm[plat] = r
  206. }
  207. return
  208. }
  209. func (s *Service) addCache(f func()) {
  210. select {
  211. case s.cacheCh <- f:
  212. default:
  213. log.Warn("cacheproc chan full")
  214. }
  215. }
  216. func (s *Service) cacheproc() {
  217. for {
  218. f, ok := <-s.cacheCh
  219. if !ok {
  220. log.Warn("cache proc exit")
  221. return
  222. }
  223. f()
  224. }
  225. }