databus.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "time"
  6. "go-common/library/log"
  7. )
  8. // Play push databus .
  9. func (d *Dao) Play(c context.Context, plat, aid, cid, part, mid, level, ftime, stime, did, ip, agent, buvid, cookieSid, refer, typeID, subType, sid, epid, playMode, platform, device, mobiAapp, autoPlay, session string) {
  10. bf := d.bfp.Get().(*bytes.Buffer)
  11. bf.WriteString(plat)
  12. bf.Write(d.spliter)
  13. bf.WriteString(aid)
  14. bf.Write(d.spliter)
  15. bf.WriteString(cid)
  16. bf.Write(d.spliter)
  17. bf.WriteString(part)
  18. bf.Write(d.spliter)
  19. bf.WriteString(mid)
  20. bf.Write(d.spliter)
  21. bf.WriteString(level)
  22. bf.Write(d.spliter)
  23. bf.WriteString(ftime)
  24. bf.Write(d.spliter)
  25. bf.WriteString(stime)
  26. bf.Write(d.spliter)
  27. bf.WriteString(did)
  28. bf.Write(d.spliter)
  29. bf.WriteString(ip)
  30. bf.Write(d.spliter)
  31. bf.WriteString(agent)
  32. bf.Write(d.spliter)
  33. bf.WriteString(buvid)
  34. bf.Write(d.spliter)
  35. bf.WriteString(cookieSid)
  36. bf.Write(d.spliter)
  37. bf.WriteString(refer)
  38. bf.Write(d.spliter)
  39. bf.WriteString(typeID)
  40. bf.Write(d.spliter)
  41. bf.WriteString(subType)
  42. bf.Write(d.spliter)
  43. bf.WriteString(sid)
  44. bf.Write(d.spliter)
  45. bf.WriteString(epid)
  46. bf.Write(d.spliter)
  47. bf.WriteString(playMode)
  48. bf.Write(d.spliter)
  49. bf.WriteString(platform)
  50. bf.Write(d.spliter)
  51. bf.WriteString(device)
  52. bf.Write(d.spliter)
  53. bf.WriteString(mobiAapp)
  54. bf.Write(d.spliter)
  55. bf.WriteString(autoPlay)
  56. bf.Write(d.spliter)
  57. bf.WriteString(session)
  58. buf := make([]byte, len(bf.Bytes()))
  59. copy(buf, bf.Bytes())
  60. select {
  61. case d.msgs <- buf:
  62. default:
  63. log.Warn("d.Play() msgs is full !")
  64. }
  65. bf.Reset()
  66. d.bfp.Put(bf)
  67. }
  68. // pubproc send history to databus.
  69. func (d *Dao) pubproc() {
  70. var (
  71. msg []byte
  72. ms [][]byte
  73. ticker = time.NewTicker(time.Second)
  74. )
  75. for {
  76. select {
  77. case msg = <-d.msgs:
  78. if len(msg) == 0 {
  79. continue
  80. }
  81. if d.spliter[0] != msg[0] {
  82. ms = append(ms, msg)
  83. if len(ms) < 100 {
  84. continue
  85. }
  86. }
  87. case <-ticker.C:
  88. }
  89. if len(ms) == 0 {
  90. continue
  91. }
  92. d.mergePub(ms)
  93. ms = make([][]byte, 0, 100)
  94. }
  95. }
  96. func (d *Dao) mergePub(ms [][]byte) {
  97. key := string(ms[0][:50])
  98. for j := 0; j < 3; j++ {
  99. if err := d.merge.Send(context.Background(), key, ms); err == nil {
  100. break
  101. }
  102. log.Error("d.merge.Send(%+v)", ms)
  103. }
  104. }