dm.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package service
  2. import (
  3. "bytes"
  4. "compress/flate"
  5. "context"
  6. "fmt"
  7. "math"
  8. "sort"
  9. "go-common/app/job/main/dm2/model"
  10. arcMdl "go-common/app/service/main/archive/model/archive"
  11. "go-common/library/log"
  12. )
  13. // Gzflate flate 压缩
  14. func (s *Service) gzflate(in []byte, level int) (out []byte, err error) {
  15. if len(in) == 0 {
  16. return
  17. }
  18. buf := new(bytes.Buffer)
  19. w, err := flate.NewWriter(buf, level)
  20. if err != nil {
  21. return
  22. }
  23. if _, err = w.Write(in); err != nil {
  24. return
  25. }
  26. if err = w.Close(); err != nil {
  27. return
  28. }
  29. out = buf.Bytes()
  30. return
  31. }
  32. func (s *Service) dmsCache(c context.Context, tp int32, oid, maxlimit int64) (dms []*model.DM, err error) {
  33. ok, err := s.dao.ExpireDMCache(c, tp, oid)
  34. if err != nil || !ok {
  35. return
  36. }
  37. values, err := s.dao.DMCache(c, tp, oid)
  38. if err != nil || len(values) == 0 {
  39. return
  40. }
  41. var (
  42. start, trimCnt int
  43. normal, protect, special []*model.DM
  44. )
  45. for _, value := range values {
  46. dm := &model.DM{}
  47. if err = dm.Unmarshal(value); err != nil {
  48. log.Error("proto.Unmarshal(%s) error(%v)", value, err)
  49. return
  50. }
  51. if dm.Pool == model.PoolNormal {
  52. if dm.AttrVal(model.AttrProtect) == model.AttrYes {
  53. protect = append(protect, dm)
  54. } else {
  55. normal = append(normal, dm)
  56. }
  57. } else {
  58. special = append(special, dm)
  59. }
  60. }
  61. // 保护弹幕
  62. if start = len(protect) - int(maxlimit); start > 0 { // 只保留maxlimit条保护弹幕
  63. trimCnt += start
  64. protect = protect[start:]
  65. }
  66. dms = append(dms, protect...)
  67. // 普通弹幕
  68. if start = len(normal) + len(protect) - int(maxlimit); start > 0 { // 保护弹幕+普通弹幕=maxlimit
  69. trimCnt += start
  70. normal = normal[start:]
  71. }
  72. dms = append(dms, normal...)
  73. // 追加字幕弹幕和特殊弹幕
  74. dms = append(dms, special...)
  75. if trimCnt > 0 {
  76. err = s.dao.TrimDMCache(c, tp, oid, int64(trimCnt))
  77. }
  78. return
  79. }
  80. // 返回所有每个弹幕池对应的弹幕列表
  81. func (s *Service) dms(c context.Context, tp int32, oid, maxlimit int64, childpool int32) (dms []*model.DM, err error) {
  82. var (
  83. count int
  84. keyprotect = "kp"
  85. dmMap = make(map[string][]*model.DM)
  86. contentSpeMap = make(map[int64]*model.ContentSpecial)
  87. )
  88. idxMap, dmids, spedmids, err := s.dao.Indexs(c, tp, oid)
  89. if err != nil {
  90. return
  91. }
  92. if len(dmids) == 0 {
  93. return
  94. }
  95. ctsMap, err := s.dao.Contents(c, oid, dmids)
  96. if err != nil {
  97. return
  98. }
  99. if len(spedmids) > 0 {
  100. if contentSpeMap, err = s.dao.ContentsSpecial(c, spedmids); err != nil {
  101. return
  102. }
  103. }
  104. for _, content := range ctsMap {
  105. if dm, ok := idxMap[content.ID]; ok {
  106. key := fmt.Sprint(dm.Pool)
  107. dm.Content = content
  108. if dm.Pool == model.PoolNormal {
  109. if dm.AttrVal(model.AttrProtect) == model.AttrYes {
  110. key = keyprotect
  111. }
  112. }
  113. if dm.Pool == model.PoolSpecial {
  114. contentSpe, ok := contentSpeMap[dm.ID]
  115. if ok {
  116. dm.ContentSpe = contentSpe
  117. }
  118. }
  119. dmMap[key] = append(dmMap[key], dm)
  120. }
  121. }
  122. // dm sort
  123. for _, dmsTmp := range dmMap {
  124. sort.Sort(model.DMSlice(dmsTmp))
  125. }
  126. // pool = 0 保护弹幕和普通弹幕总和为maxlimit
  127. if protect, ok := dmMap[keyprotect]; ok {
  128. if start := len(protect) - int(maxlimit); start > 0 { // 只保留maxlimit条保护弹幕
  129. protect = protect[start:]
  130. }
  131. dms = append(dms, protect...)
  132. count = len(protect)
  133. }
  134. if normal, ok := dmMap[fmt.Sprint(model.PoolNormal)]; ok {
  135. start := len(normal) + count - int(maxlimit)
  136. if start > 0 {
  137. normal = normal[start:]
  138. }
  139. dms = append(dms, normal...)
  140. }
  141. // pool = 1 字幕弹幕
  142. if subtitle, ok := dmMap[fmt.Sprint(model.PoolSubtitle)]; ok {
  143. dms = append(dms, subtitle...)
  144. }
  145. // pool =2 特殊弹幕
  146. if special, ok := dmMap[fmt.Sprint(model.PoolSpecial)]; ok {
  147. dms = append(dms, special...)
  148. }
  149. return
  150. }
  151. func (s *Service) genXML(c context.Context, sub *model.Subject) (xml []byte, err error) {
  152. realname := s.isRealname(c, sub.Pid, sub.Oid)
  153. buf := new(bytes.Buffer)
  154. buf.WriteString(`<?xml version="1.0" encoding="UTF-8"?><i>`)
  155. buf.WriteString(`<chatserver>chat.bilibili.com</chatserver><chatid>`)
  156. buf.WriteString(fmt.Sprint(sub.Oid))
  157. buf.WriteString(`</chatid><mission>`)
  158. buf.WriteString(fmt.Sprint(sub.AttrVal(model.AttrSubMission)))
  159. buf.WriteString(`</mission><maxlimit>`)
  160. buf.WriteString(fmt.Sprint(sub.Maxlimit))
  161. buf.WriteString(`</maxlimit>`)
  162. buf.WriteString(fmt.Sprintf(`<state>%d</state>`, sub.State))
  163. if realname {
  164. buf.WriteString(`<real_name>1</real_name>`)
  165. } else {
  166. buf.WriteString(`<real_name>0</real_name>`)
  167. }
  168. if sub.State == model.SubStateClosed {
  169. buf.WriteString(`</i>`)
  170. xml = buf.Bytes()
  171. return
  172. }
  173. dms, err := s.dmsCache(c, sub.Type, sub.Oid, sub.Maxlimit)
  174. if err != nil {
  175. return
  176. }
  177. if len(dms) > 0 {
  178. buf.WriteString(`<source>k-v</source>`)
  179. } else {
  180. buf.WriteString(`<source>e-r</source>`)
  181. if dms, err = s.dms(c, sub.Type, sub.Oid, sub.Maxlimit, int32(sub.Childpool)); err != nil {
  182. return
  183. }
  184. if err = s.dao.SetDMCache(c, sub.Type, sub.Oid, dms); err != nil { // add redis cache
  185. return
  186. }
  187. }
  188. for _, dm := range dms {
  189. buf.WriteString(dm.ToXML(realname))
  190. }
  191. buf.WriteString("</i>")
  192. xml = buf.Bytes()
  193. return
  194. }
  195. func (s *Service) isRealname(c context.Context, aid, oid int64) (realname bool) {
  196. if oid == 13196688 || oid == 290932 {
  197. realname = true
  198. return
  199. }
  200. arg := &arcMdl.ArgAid2{Aid: aid}
  201. archive, err := s.arcRPC.Archive3(c, arg)
  202. if err != nil {
  203. log.Error("arcRPC.Archive3(%v) error(%v)", arg, err)
  204. return
  205. }
  206. if v, ok := s.realname[int64(archive.TypeID)]; ok && oid >= v {
  207. realname = true
  208. } else {
  209. realname = false
  210. }
  211. return
  212. }
  213. // flushXMLSegCache 刷新每个分段的缓存,NOTE:目前只是单纯删除缓存
  214. func (s *Service) flushXMLSegCache(c context.Context, sub *model.Subject) (err error) {
  215. duration, err := s.videoDuration(c, sub.Pid, sub.Oid)
  216. if err != nil {
  217. return
  218. }
  219. seg := model.SegmentInfo(0, duration)
  220. for i := int64(1); i <= seg.Cnt; i++ {
  221. if err = s.dao.DelXMLSegCache(c, sub.Type, sub.Oid, seg.Cnt, i); err != nil {
  222. continue
  223. }
  224. }
  225. return
  226. }
  227. // rebuildDmSegCache 刷新视频每个分段弹幕缓存
  228. func (s *Service) flushAllDmSegCache(c context.Context, oid int64, tp int32) (err error) {
  229. var (
  230. sub *model.Subject
  231. duration, total int64
  232. )
  233. if sub, err = s.subject(c, tp, oid); err != nil {
  234. return
  235. }
  236. if duration, err = s.videoDuration(c, sub.Pid, sub.Oid); err != nil {
  237. return
  238. }
  239. total = int64(math.Ceil(float64(duration) / float64(model.DefaultPageSize)))
  240. for i := int64(1); i <= total; i++ {
  241. s.asyncAddFlushDMSeg(c, &model.FlushDMSeg{
  242. Type: tp,
  243. Oid: oid,
  244. Force: true,
  245. Page: &model.Page{
  246. Num: i,
  247. Size: model.DefaultPageSize,
  248. Total: total,
  249. },
  250. })
  251. }
  252. log.Info("flushAllDmSegCache oid:%v total:%v", oid, total)
  253. return
  254. }
  255. func (s *Service) asyncAddFlushDM(c context.Context, fc *model.Flush) {
  256. select {
  257. case s.flushMergeChan[fc.Oid%int64(s.routineSize)] <- fc:
  258. default:
  259. log.Warn("flush merge channel is full,flush(%+v)", fc)
  260. }
  261. }