archive.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package service
  2. import (
  3. "context"
  4. "math"
  5. "sync"
  6. "go-common/app/interface/main/dm2/model"
  7. "go-common/app/service/main/archive/api"
  8. arcMdl "go-common/app/service/main/archive/model/archive"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. "go-common/library/net/metadata"
  12. "go-common/library/sync/errgroup"
  13. )
  14. func (s *Service) archiveInfos(c context.Context, aids []int64) (archiveInfos map[int64]*api.Arc, err error) {
  15. var (
  16. pagesize = 100
  17. wg errgroup.Group
  18. mu sync.Mutex
  19. )
  20. archiveInfos = make(map[int64]*api.Arc)
  21. if len(aids) <= 0 {
  22. return
  23. }
  24. page := int(math.Ceil(float64(len(aids)) / float64(pagesize)))
  25. for i := 0; i < page; i++ {
  26. start := i * pagesize
  27. end := (i + 1) * pagesize
  28. if end > len(aids) {
  29. end = len(aids)
  30. }
  31. wg.Go(func() (err error) {
  32. arg := &arcMdl.ArgAids2{Aids: aids[start:end]}
  33. infos, err := s.arcRPC.Archives3(c, arg)
  34. if err != nil {
  35. log.Error("s.arcRPC.Archives3(%v) error(%v)", arg, err)
  36. return
  37. }
  38. for _, info := range infos {
  39. mu.Lock()
  40. archiveInfos[info.Aid] = info
  41. mu.Unlock()
  42. }
  43. return
  44. })
  45. }
  46. err = wg.Wait()
  47. return
  48. }
  49. // videoDuration return video duration cid.
  50. func (s *Service) videoDuration(c context.Context, aid, cid int64) (duration int64, err error) {
  51. var cache = true
  52. if duration, err = s.dao.DurationCache(c, cid); err != nil {
  53. log.Error("dao.Duration(cid:%d) error(%v)", cid, err)
  54. err = nil
  55. cache = false
  56. } else if duration != model.NotFound {
  57. return
  58. }
  59. arg := &arcMdl.ArgVideo2{Aid: aid, Cid: cid, RealIP: metadata.String(c, metadata.RemoteIP)}
  60. page, err := s.arcRPC.Video3(c, arg)
  61. if err != nil {
  62. if ecode.Cause(err).Code() == ecode.NothingFound.Code() {
  63. duration = 0
  64. err = nil
  65. log.Warn("acvSvc.Video3(%v) error(duration not exist)", arg)
  66. } else {
  67. log.Error("acvSvc.Video3(%v) error(%v)", arg, err)
  68. }
  69. } else {
  70. duration = page.Duration * 1000
  71. }
  72. if cache {
  73. s.cache.Do(c, func(ctx context.Context) {
  74. s.dao.SetDurationCache(ctx, cid, duration)
  75. })
  76. }
  77. return
  78. }