service.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "sync"
  7. "time"
  8. plmdl "go-common/app/interface/main/playlist/model"
  9. plarpc "go-common/app/interface/main/playlist/rpc/client"
  10. "go-common/app/job/main/playlist/conf"
  11. "go-common/app/job/main/playlist/dao"
  12. "go-common/app/job/main/playlist/model"
  13. "go-common/library/log"
  14. "go-common/library/queue/databus"
  15. "go-common/library/stat/prom"
  16. )
  17. const (
  18. _sharding = 10 // goroutines for dealing the stat
  19. _chanSize = 10240
  20. )
  21. // Service .
  22. type Service struct {
  23. c *conf.Config
  24. dao *dao.Dao
  25. waiter *sync.WaitGroup
  26. closed bool
  27. playlistViewSub *databus.Databus
  28. playlistFavSub *databus.Databus
  29. playlistReplySub *databus.Databus
  30. playlistShareSub *databus.Databus
  31. playlistRPC *plarpc.Service
  32. updateDbInterval int64
  33. statCh [_sharding]chan *model.StatM
  34. }
  35. // New creates a Service instance.
  36. func New(c *conf.Config) (s *Service) {
  37. s = &Service{
  38. c: c,
  39. dao: dao.New(c),
  40. waiter: new(sync.WaitGroup),
  41. playlistViewSub: databus.New(c.PlaylistViewSub),
  42. playlistFavSub: databus.New(c.PlaylistFavSub),
  43. playlistReplySub: databus.New(c.PlaylistReplySub),
  44. playlistShareSub: databus.New(c.PlaylistShareSub),
  45. playlistRPC: plarpc.New(c.PlaylistRPC),
  46. updateDbInterval: int64(time.Duration(c.Job.UpdateDbInterval) / time.Second),
  47. }
  48. for i := int64(0); i < _sharding; i++ {
  49. // for stat
  50. s.statCh[i] = make(chan *model.StatM, _chanSize)
  51. s.waiter.Add(1)
  52. go s.viewproc(i)
  53. }
  54. s.waiter.Add(1)
  55. go s.consumeView()
  56. s.waiter.Add(1)
  57. go s.consumeFav()
  58. s.waiter.Add(1)
  59. go s.consumeReply()
  60. s.waiter.Add(1)
  61. go s.consumeShare()
  62. return
  63. }
  64. // consumeView consumes playlist's view.
  65. func (s *Service) consumeView() {
  66. defer s.waiter.Done()
  67. for {
  68. if s.closed {
  69. for i := 0; i < _sharding; i++ {
  70. close(s.statCh[i])
  71. }
  72. return
  73. }
  74. msg, ok := <-s.playlistViewSub.Messages()
  75. if !ok {
  76. log.Info("databus: playlist-job view consumer exit!")
  77. time.Sleep(10 * time.Millisecond)
  78. continue
  79. }
  80. msg.Commit()
  81. viewSM := &model.StatM{}
  82. if err := json.Unmarshal(msg.Value, viewSM); err != nil {
  83. dao.PromError("service:解析计数databus消息", "json.Unmarshal(%s) error(%v)", msg.Value, err)
  84. continue
  85. }
  86. if viewSM.Type != plmdl.PlDBusType || viewSM.ID <= 0 {
  87. continue
  88. }
  89. key := viewSM.ID % _sharding
  90. s.statCh[key] <- viewSM
  91. prom.BusinessInfoCount.State(fmt.Sprintf("statChan-%v", key), int64(len(s.statCh[key])))
  92. log.Info("consumeView key:%s partition:%d offset:%d msg: %v)", msg.Key, msg.Partition, msg.Offset, viewSM.String(model.ViewCountType))
  93. }
  94. }
  95. // consumeFav consumes playlist's favorite.
  96. func (s *Service) consumeFav() {
  97. defer s.waiter.Done()
  98. var c = context.TODO()
  99. for {
  100. msg, ok := <-s.playlistFavSub.Messages()
  101. if !ok {
  102. log.Info("databus: playlist-job favorite consumer exit!")
  103. return
  104. }
  105. msg.Commit()
  106. favSM := &model.StatM{}
  107. if err := json.Unmarshal(msg.Value, favSM); err != nil {
  108. log.Error("json.Unmarshal(%s) error(%v)", msg.Value, err)
  109. continue
  110. }
  111. if favSM.Type != plmdl.PlDBusType || favSM.ID <= 0 {
  112. continue
  113. }
  114. s.upStat(c, favSM, model.FavCountType)
  115. log.Info("consumeFav key:%s partition:%d offset:%d msg: %v)", msg.Key, msg.Partition, msg.Offset, favSM.String(model.FavCountType))
  116. }
  117. }
  118. // consumeReply consumes playlist's reply.
  119. func (s *Service) consumeReply() {
  120. defer s.waiter.Done()
  121. var c = context.TODO()
  122. for {
  123. msg, ok := <-s.playlistReplySub.Messages()
  124. if !ok {
  125. log.Info("databus: playlist-job reply consumer exit!")
  126. return
  127. }
  128. msg.Commit()
  129. replySM := &model.StatM{}
  130. if err := json.Unmarshal(msg.Value, replySM); err != nil {
  131. log.Error("json.Unmarshal(%s) error(%v)", msg.Value, err)
  132. continue
  133. }
  134. if replySM.Type != plmdl.PlDBusType || replySM.ID <= 0 {
  135. continue
  136. }
  137. s.upStat(c, replySM, model.ReplyCountType)
  138. log.Info("consumeReply key:%s partition:%d offset:%d msg: %v)", msg.Key, msg.Partition, msg.Offset, replySM.String(model.ReplyCountType))
  139. }
  140. }
  141. // consumeShare consumes playlist's share.
  142. func (s *Service) consumeShare() {
  143. defer s.waiter.Done()
  144. var c = context.TODO()
  145. for {
  146. msg, ok := <-s.playlistShareSub.Messages()
  147. if !ok {
  148. log.Info("databus: playlist-job share consumer exit!")
  149. return
  150. }
  151. msg.Commit()
  152. shareSM := &model.StatM{}
  153. if err := json.Unmarshal(msg.Value, shareSM); err != nil {
  154. log.Error("json.Unmarshal(%s) error(%v)", msg.Value, err)
  155. continue
  156. }
  157. if shareSM.Type != plmdl.PlDBusType || shareSM.ID <= 0 {
  158. continue
  159. }
  160. s.upStat(c, shareSM, model.ShareCountType)
  161. log.Info("consumeShare key:%s partition:%d offset:%d msg: %v)", msg.Key, msg.Partition, msg.Offset, shareSM.String(model.ShareCountType))
  162. }
  163. }
  164. // Ping reports the heath of services.
  165. func (s *Service) Ping(c context.Context) (err error) {
  166. return s.dao.Ping(c)
  167. }
  168. // Close releases resources which owned by the Service instance.
  169. func (s *Service) Close() (err error) {
  170. defer s.waiter.Wait()
  171. s.closed = true
  172. s.playlistViewSub.Close()
  173. s.playlistFavSub.Close()
  174. s.playlistReplySub.Close()
  175. s.playlistShareSub.Close()
  176. log.Info("playlist-job has been closed.")
  177. return
  178. }