service.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package archive
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/creative/conf"
  6. "go-common/app/interface/main/creative/dao/account"
  7. "go-common/app/interface/main/creative/dao/activity"
  8. "go-common/app/interface/main/creative/dao/appeal"
  9. "go-common/app/interface/main/creative/dao/archive"
  10. "go-common/app/interface/main/creative/dao/coin"
  11. gD "go-common/app/interface/main/creative/dao/game"
  12. "go-common/app/interface/main/creative/dao/order"
  13. "go-common/app/interface/main/creative/dao/search"
  14. "go-common/app/interface/main/creative/dao/tag"
  15. "go-common/app/interface/main/creative/dao/template"
  16. actmdl "go-common/app/interface/main/creative/model/activity"
  17. "go-common/app/interface/main/creative/model/game"
  18. "go-common/app/interface/main/creative/service"
  19. "go-common/library/log"
  20. "go-common/library/stat/prom"
  21. )
  22. //Service struct
  23. type Service struct {
  24. c *conf.Config
  25. arc *archive.Dao
  26. acc *account.Dao
  27. sear *search.Dao
  28. act *activity.Dao
  29. tpl *template.Dao
  30. coin *coin.Dao
  31. order *order.Dao
  32. ap *appeal.Dao
  33. tag *tag.Dao
  34. game *gD.Dao
  35. p *service.Public
  36. prom *prom.Prom
  37. missch chan func()
  38. pCacheHit *prom.Prom
  39. pCacheMiss *prom.Prom
  40. // cache
  41. orderUps map[int64]int64
  42. gameMap map[int64]*game.ListItem
  43. ArcTip string
  44. }
  45. //New get service
  46. func New(c *conf.Config, rpcdaos *service.RPCDaos, p *service.Public) *Service {
  47. s := &Service{
  48. c: c,
  49. arc: rpcdaos.Arc,
  50. acc: rpcdaos.Acc,
  51. sear: search.New(c),
  52. act: activity.New(c),
  53. tpl: template.New(c),
  54. tag: tag.New(c),
  55. coin: coin.New(c),
  56. order: order.New(c),
  57. ap: appeal.New(c),
  58. game: gD.New(c),
  59. p: p,
  60. prom: prom.BusinessInfoCount,
  61. missch: make(chan func(), 1024),
  62. pCacheHit: prom.CacheHit,
  63. pCacheMiss: prom.CacheMiss,
  64. ArcTip: c.Host.ArcTip,
  65. }
  66. s.loadOrderUps()
  67. s.loadAllGameMap()
  68. go s.loadproc()
  69. go s.cacheproc()
  70. return s
  71. }
  72. // TopAct fn
  73. func (s *Service) TopAct() (ret []*actmdl.Activity) {
  74. return s.p.TopActCache
  75. }
  76. func (s *Service) loadOrderUps() {
  77. orderUps, err := s.order.Ups(context.TODO())
  78. if err != nil {
  79. return
  80. }
  81. s.orderUps = orderUps
  82. }
  83. func (s *Service) loadAllGameMap() {
  84. list, err := s.game.List(context.TODO(), "", "")
  85. if err != nil || list == nil || len(list) == 0 {
  86. return
  87. }
  88. s.gameMap = make(map[int64]*game.ListItem)
  89. for _, v := range list {
  90. s.gameMap[v.GameBaseID] = v
  91. }
  92. log.Info("s.loadAllGameMap: s.gameMapLen(%d)", len(s.gameMap))
  93. }
  94. // loadproc
  95. func (s *Service) loadproc() {
  96. for {
  97. time.Sleep(5 * time.Minute)
  98. s.loadOrderUps()
  99. s.loadAllGameMap()
  100. }
  101. }
  102. // AllowOrderUps 检查用户商单信息
  103. func (s *Service) AllowOrderUps(mid int64) (ok bool) {
  104. _, ok = s.orderUps[mid]
  105. return
  106. }
  107. // AddCache add to chan for cache
  108. func (s *Service) addCache(f func()) {
  109. select {
  110. case s.missch <- f:
  111. default:
  112. log.Warn("cacheproc chan full")
  113. }
  114. }
  115. // cacheproc is a routine for execute closure.
  116. func (s *Service) cacheproc() {
  117. for {
  118. f := <-s.missch
  119. f()
  120. }
  121. }
  122. // Ping service
  123. func (s *Service) Ping(c context.Context) (err error) {
  124. if err = s.arc.Ping(c); err != nil {
  125. log.Error("s.archive.Dao.PingDb err(%v)", err)
  126. }
  127. return
  128. }
  129. // Close dao
  130. func (s *Service) Close() {
  131. s.arc.Close()
  132. }