infoc.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package infoc
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/binary"
  6. "encoding/json"
  7. "net"
  8. "strconv"
  9. "sync"
  10. "time"
  11. "go-common/library/log"
  12. )
  13. var (
  14. _infoc2Magic = []byte{172, 190} // NOTE: magic 0xAC0xBE
  15. _infoc2Type = []byte{0, 0} // NOTE: type 0
  16. _infocTimeout = 500 * time.Millisecond
  17. )
  18. // Config is infoc config.
  19. type Config struct {
  20. TaskID string
  21. // udp or tcp
  22. Proto string
  23. Addr string
  24. // reporter
  25. ReporterAddr string
  26. }
  27. // Infoc infoc struct.
  28. type Infoc struct {
  29. c *Config
  30. header []byte
  31. // udp or tcp
  32. conn net.Conn
  33. lock sync.Mutex
  34. // reporter
  35. reporter *reporter
  36. }
  37. // New new infoc2 logger.
  38. func New(c *Config) (i *Infoc) {
  39. i = &Infoc{
  40. c: c,
  41. header: []byte(c.TaskID),
  42. }
  43. var err error
  44. if i.conn, err = net.Dial(i.c.Proto, i.c.Addr); err != nil {
  45. log.Error("infoc net dial error(%v)", err)
  46. }
  47. if c.ReporterAddr != "" {
  48. i.reporter = newReporter(c.TaskID, c.ReporterAddr)
  49. go i.reporter.reportproc()
  50. }
  51. return
  52. }
  53. // Rows the affected by binlog enent.
  54. func (i *Infoc) Rows(rows int64) {
  55. if i.reporter != nil {
  56. i.reporter.receiveIncr(rows)
  57. }
  58. }
  59. // Send send message.
  60. func (i *Infoc) Send(ctx context.Context, key string, v interface{}) (err error) {
  61. var b []byte
  62. if b, err = json.Marshal(v); err != nil {
  63. log.Error("json.Marshal(%v) error(%v)", v, err)
  64. return
  65. }
  66. var (
  67. res bytes.Buffer
  68. buf bytes.Buffer
  69. )
  70. res.Write(_infoc2Magic)
  71. // type and body buf, for calc length.
  72. buf.Write(_infoc2Type)
  73. buf.Write(i.header)
  74. buf.WriteString(strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10))
  75. // // append first arg
  76. if _, err = buf.WriteString(string(b)); err != nil {
  77. return
  78. }
  79. // put length
  80. var ls [4]byte
  81. binary.BigEndian.PutUint32(ls[:], uint32(buf.Len()))
  82. res.Write(ls[:]) // NOTE: write length
  83. res.Write(buf.Bytes()) // NOTE:write type and body
  84. // write
  85. if err = i.write(res.Bytes()); err != nil {
  86. log.Error("infoc write error(%v)", err)
  87. return
  88. }
  89. if i.reporter != nil {
  90. i.reporter.sendIncr(1)
  91. }
  92. return
  93. }
  94. // write write data into connection.
  95. func (i *Infoc) write(bs []byte) (err error) {
  96. defer func() {
  97. if err != nil {
  98. if i.conn != nil {
  99. i.conn.Close()
  100. }
  101. i.conn = nil
  102. }
  103. i.lock.Unlock()
  104. }()
  105. i.lock.Lock()
  106. // connection and write
  107. if i.conn == nil {
  108. if i.conn, err = net.DialTimeout(i.c.Proto, i.c.Addr, _infocTimeout); err != nil {
  109. log.Error("infoc net dial error(%v)", err)
  110. return
  111. }
  112. }
  113. if i.c.Proto == "tcp" {
  114. i.conn.SetDeadline(time.Now().Add(_infocTimeout))
  115. }
  116. if _, err = i.conn.Write(bs); err != nil {
  117. log.Error("infoc net write error(%v)", err)
  118. }
  119. return
  120. }
  121. // Flush flush reporter count.
  122. func (i *Infoc) Flush() {
  123. if i.reporter != nil {
  124. i.reporter.flush()
  125. }
  126. }
  127. // Close close resource.
  128. func (i *Infoc) Close() {
  129. if i.conn != nil {
  130. i.conn.Close()
  131. }
  132. }