service.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "sync"
  8. "time"
  9. "go-common/app/interface/main/dm2/conf"
  10. "go-common/app/interface/main/dm2/dao"
  11. "go-common/app/interface/main/dm2/model"
  12. "go-common/app/interface/main/dm2/model/oplog"
  13. accountCli "go-common/app/service/main/account/api"
  14. arcCli "go-common/app/service/main/archive/api/gorpc"
  15. assMdl "go-common/app/service/main/assist/model/assist"
  16. assCli "go-common/app/service/main/assist/rpc/client"
  17. coinCli "go-common/app/service/main/coin/api/gorpc"
  18. figureCli "go-common/app/service/main/figure/rpc/client"
  19. filterCli "go-common/app/service/main/filter/api/grpc/v1"
  20. locCli "go-common/app/service/main/location/rpc/client"
  21. memberCli "go-common/app/service/main/member/api/gorpc"
  22. relCli "go-common/app/service/main/relation/rpc/client"
  23. seqMdl "go-common/app/service/main/seq-server/model"
  24. seqCli "go-common/app/service/main/seq-server/rpc/client"
  25. spyCli "go-common/app/service/main/spy/rpc/client"
  26. thumbupApi "go-common/app/service/main/thumbup/api"
  27. ugcPayCli "go-common/app/service/main/ugcpay/api/grpc/v1"
  28. seasonCli "go-common/app/service/openplatform/pgc-season/api/grpc/season/v1"
  29. "go-common/library/ecode"
  30. "go-common/library/log"
  31. "go-common/library/log/infoc"
  32. "go-common/library/sync/pipeline/fanout"
  33. "go-common/library/xstr"
  34. "golang.org/x/sync/singleflight"
  35. )
  36. func keySubject(tp int32, oid int64) string {
  37. return fmt.Sprintf("sub_local_%d_%d", tp, oid)
  38. }
  39. func keyXML(tp int32, oid int64) string {
  40. return fmt.Sprintf("%d_%d", tp, oid)
  41. }
  42. func keySeg(tp int32, oid, cnt, num int64) string {
  43. return fmt.Sprintf("%d_%d_%d_%d", tp, oid, cnt, num)
  44. }
  45. func keyDuration(tp int32, oid int64) string {
  46. return fmt.Sprintf("d_%d_%d", tp, oid)
  47. }
  48. type broadcast struct {
  49. Aid int64
  50. Rnd int64
  51. *model.DM
  52. }
  53. // Service dm2 service
  54. type Service struct {
  55. conf *conf.Config
  56. dao *dao.Dao
  57. arcRPC *arcCli.Service2
  58. accountRPC accountCli.AccountClient
  59. assRPC *assCli.Service
  60. coinRPC *coinCli.Service
  61. relRPC *relCli.Service
  62. thumbupRPC thumbupApi.ThumbupClient
  63. locationRPC *locCli.Service
  64. cache *fanout.Fanout
  65. realname map[int64]int64 // key:分区id,value:cid,即该分区中大于cid的视频开启实名制
  66. singleGroup singleflight.Group
  67. arcTypes map[int16]int16
  68. broadcastChan chan *broadcast
  69. // seq serer
  70. seqDmArg *seqMdl.ArgBusiness
  71. seqSubtitleArg *seqMdl.ArgBusiness
  72. seqRPC *seqCli.Service2
  73. // dm xml and dm seg local cache
  74. localCache map[string][]byte
  75. assistLogChan chan *assMdl.ArgAssistLogAdd
  76. // send operation log with infoc2
  77. dmOperationLogSvc *infoc.Infoc
  78. opsLogCh chan *oplog.Infoc
  79. // dm monitor merge proc
  80. moniOidMap map[int64]struct{}
  81. oidLock sync.Mutex
  82. // subtitle singleFlight
  83. subtitleSingleGroup singleflight.Group
  84. subtitleLans model.SubtitleLans
  85. // block
  86. memberRPC *memberCli.Service
  87. // filter
  88. filterRPC filterCli.FilterClient
  89. //figure
  90. figureRPC *figureCli.Service
  91. // ugc pay
  92. ugcPayRPC ugcPayCli.UGCPayClient
  93. // spy RPC
  94. spyRPC *spyCli.Service
  95. // season
  96. seasonRPC seasonCli.SeasonClient
  97. // garbageDanmu
  98. garbageDanmu bool
  99. // broadcast limit
  100. broadcastLimit int
  101. broadcastlimitInterval int
  102. // view localcache
  103. localViewCache map[string]*model.ViewDm
  104. // bnj shield
  105. aidSheild map[int64]struct{}
  106. midsSheild map[int64]struct{}
  107. }
  108. // New return a service instance.
  109. func New(c *conf.Config) (s *Service) {
  110. s = &Service{
  111. conf: c,
  112. dao: dao.New(c),
  113. arcRPC: arcCli.New2(c.ArchiveRPC),
  114. assRPC: assCli.New(c.AssistRPC),
  115. coinRPC: coinCli.New(c.CoinRPC),
  116. relRPC: relCli.New(c.RelationRPC),
  117. locationRPC: locCli.New(c.LocationRPC),
  118. cache: fanout.New("cache", fanout.Worker(1), fanout.Buffer(1024)),
  119. realname: make(map[int64]int64),
  120. arcTypes: make(map[int16]int16),
  121. broadcastChan: make(chan *broadcast, 1024),
  122. localCache: make(map[string][]byte),
  123. seqDmArg: &seqMdl.ArgBusiness{BusinessID: c.Seq.DM.BusinessID, Token: c.Seq.DM.Token},
  124. seqSubtitleArg: &seqMdl.ArgBusiness{BusinessID: c.Seq.Subtitle.BusinessID, Token: c.Seq.Subtitle.Token},
  125. seqRPC: seqCli.New2(c.SeqRPC),
  126. assistLogChan: make(chan *assMdl.ArgAssistLogAdd, 1024),
  127. dmOperationLogSvc: infoc.New(c.Infoc2),
  128. opsLogCh: make(chan *oplog.Infoc, 1024),
  129. moniOidMap: make(map[int64]struct{}),
  130. memberRPC: memberCli.New(c.MemberRPC),
  131. figureRPC: figureCli.New(c.FigureRPC),
  132. spyRPC: spyCli.New(c.SpyRPC),
  133. garbageDanmu: c.Switch.GarbageDanmu,
  134. broadcastLimit: c.BroadcastLimit.Limit,
  135. broadcastlimitInterval: c.BroadcastLimit.Interval,
  136. localViewCache: make(map[string]*model.ViewDm),
  137. aidSheild: make(map[int64]struct{}),
  138. midsSheild: make(map[int64]struct{}),
  139. }
  140. for idStr, cid := range s.conf.Realname.Threshold {
  141. ids, err := xstr.SplitInts(idStr)
  142. if err != nil {
  143. panic(err)
  144. }
  145. for _, id := range ids {
  146. if _, ok := s.realname[id]; !ok {
  147. s.realname[id] = cid
  148. }
  149. }
  150. }
  151. accountRPC, err := accountCli.NewClient(c.AccountRPC)
  152. if err != nil {
  153. panic(err)
  154. }
  155. s.accountRPC = accountRPC
  156. ugcPayRPC, err := ugcPayCli.NewClient(c.UgcPayRPC)
  157. if err != nil {
  158. panic(err)
  159. }
  160. s.ugcPayRPC = ugcPayRPC
  161. filterRPC, err := filterCli.NewClient(s.conf.FilterRPC)
  162. if err != nil {
  163. panic(err)
  164. }
  165. s.filterRPC = filterRPC
  166. seasonRPC, err := seasonCli.NewClient(c.SeasonRPC)
  167. if err != nil {
  168. panic(fmt.Sprintf("seasonCli.NewClient.error(%v)", err))
  169. }
  170. s.seasonRPC = seasonRPC
  171. thumbupRPC, err := thumbupApi.NewClient(c.ThumbupRPC)
  172. if err != nil {
  173. panic(err)
  174. }
  175. s.thumbupRPC = thumbupRPC
  176. subtitleLans, err := s.dao.SubtitleLans(context.Background())
  177. if err != nil {
  178. panic(err)
  179. }
  180. s.subtitleLans = model.SubtitleLans(subtitleLans)
  181. go s.broadcastproc()
  182. go s.archiveTypeproc()
  183. go s.localcacheproc()
  184. go s.assistLogproc()
  185. go s.oplogproc()
  186. go s.monitorproc()
  187. go s.viewProc()
  188. go s.shieldProc()
  189. return
  190. }
  191. // Ping service ping.
  192. func (s *Service) Ping(c context.Context) (err error) {
  193. return s.dao.Ping(c)
  194. }
  195. func (s *Service) archiveTypeproc() {
  196. var mu sync.Mutex
  197. for {
  198. rmap, err := s.dao.TypeMapping(context.TODO())
  199. if err != nil {
  200. log.Error("load archive types error(%v)", err)
  201. } else {
  202. mu.Lock()
  203. s.arcTypes = rmap
  204. mu.Unlock()
  205. }
  206. time.Sleep(5 * time.Minute)
  207. }
  208. }
  209. func (s *Service) broadcastproc() {
  210. broadcastFmt := `["%.2f,%d,%d,%d,%d,%d,%d,%s,%d","%s"]`
  211. for m := range s.broadcastChan {
  212. if m.Pool == model.PoolSpecial {
  213. continue
  214. }
  215. if m.State != model.StateNormal && m.State != model.StateMonitorAfter {
  216. continue
  217. }
  218. if err := s.dao.BroadcastLimit(context.TODO(), m.Oid, m.Type, s.broadcastLimit, s.broadcastlimitInterval); err != nil {
  219. if err != ecode.LimitExceed {
  220. log.Error("dao.BroadcastLimit(oid:%v) error(%v)", m.Oid, err)
  221. }
  222. continue
  223. }
  224. hash := model.Hash(m.Mid, uint32(m.Content.IP))
  225. msg := strings.Replace(m.Content.Msg, `\`, `\\`, -1)
  226. msg = strings.Replace(msg, `"`, `\"`, -1)
  227. info := fmt.Sprintf(broadcastFmt, float32(m.Progress)/1000.0, m.Content.Mode, m.Content.FontSize,
  228. m.Content.Color, m.Ctime, m.Rnd, m.Pool, hash, m.ID, msg)
  229. if err := s.dao.BroadcastInGoim(context.TODO(), m.Oid, m.Aid, []byte(info)); err != nil {
  230. log.Error("dao.BroadcastInGoim(cid:%d,aid:%d,info:%s) error(%v)", m.Oid, m.Aid, info, err)
  231. } else {
  232. log.Info("BroadcastInGoim(%s) succeed", info)
  233. }
  234. if err := s.dao.Broadcast(context.Background(), m.Oid, m.Aid, info); err != nil {
  235. log.Error("dao.Broadcast(cid:%d,aid:%d,info:%s) error(%v)", m.Oid, m.Aid, info, err)
  236. } else {
  237. log.Info("broadcast(%s) succeed", info)
  238. }
  239. }
  240. }
  241. func (s *Service) localcacheproc() {
  242. for {
  243. s.loadLocalcache(s.conf.Localcache.Oids)
  244. time.Sleep(time.Duration(s.conf.Localcache.Expire))
  245. }
  246. }
  247. func (s *Service) oplogproc() {
  248. for opLog := range s.opsLogCh {
  249. if len(opLog.Subject) == 0 || len(opLog.CurrentVal) == 0 || opLog.Source <= 0 ||
  250. opLog.Operator <= 0 || opLog.OperatorType <= 0 {
  251. log.Warn("oplogproc() it is an illegal log, warn(%v, %v, %v)", opLog.Subject, opLog.Subject, opLog.CurrentVal)
  252. continue
  253. } else {
  254. for _, dmid := range opLog.DMIds {
  255. if dmid > 0 {
  256. s.dmOperationLogSvc.Info(opLog.Subject, strconv.FormatInt(opLog.Oid, 10), strconv.Itoa(opLog.Type),
  257. strconv.FormatInt(dmid, 10), opLog.Source.String(), opLog.OriginVal,
  258. opLog.CurrentVal, strconv.FormatInt(opLog.Operator, 10), opLog.OperatorType.String(),
  259. opLog.OperationTime, opLog.Remark)
  260. } else {
  261. log.Warn("oplogproc() it is an illegal log, for dmid value, warn(%d, %+v)", dmid, opLog)
  262. }
  263. }
  264. }
  265. }
  266. }
  267. // OpLog put a new infoc format operation log into the channel
  268. func (s *Service) OpLog(c context.Context, cid, operator, OperationTime int64, typ int, dmids []int64, subject, originVal, currentVal, remark string, source oplog.Source, operatorType oplog.OperatorType) (err error) {
  269. infoLog := new(oplog.Infoc)
  270. infoLog.Oid = cid
  271. infoLog.Type = typ
  272. infoLog.DMIds = dmids
  273. infoLog.Subject = subject
  274. infoLog.OriginVal = originVal
  275. infoLog.CurrentVal = currentVal
  276. infoLog.OperationTime = strconv.FormatInt(OperationTime, 10)
  277. infoLog.Source = source
  278. infoLog.OperatorType = operatorType
  279. infoLog.Operator = operator
  280. infoLog.Remark = remark
  281. select {
  282. case s.opsLogCh <- infoLog:
  283. default:
  284. err = fmt.Errorf("opsLogCh full")
  285. log.Error("opsLogCh full (%v)", infoLog)
  286. }
  287. return
  288. }
  289. func (s *Service) monitorproc() {
  290. for {
  291. time.Sleep(3 * time.Second)
  292. s.oidLock.Lock()
  293. oidMap := s.moniOidMap
  294. s.moniOidMap = make(map[int64]struct{})
  295. s.oidLock.Unlock()
  296. for oid := range oidMap {
  297. sub, err := s.dao.Subject(context.TODO(), model.SubTypeVideo, oid)
  298. if err != nil || sub == nil {
  299. continue
  300. }
  301. if err := s.updateMonitorCnt(context.TODO(), sub); err != nil {
  302. log.Error("s.updateMonitorCnt(%+v) error(%v)", sub, err)
  303. }
  304. }
  305. }
  306. }