client.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package kafka
  2. import (
  3. "encoding/binary"
  4. "net"
  5. )
  6. type (
  7. ApiKey int16
  8. ApiVersion int16
  9. )
  10. type API struct {
  11. key ApiKey
  12. version ApiVersion
  13. }
  14. var (
  15. REQUEST_PRODUCE = API{0, 0}
  16. REQUEST_FETCH = API{1, 0}
  17. REQUEST_OFFSET = API{2, 0}
  18. REQUEST_METADATA = API{3, 0}
  19. REQUEST_LEADER_AND_ISR = API{4, 0}
  20. REQUEST_STOP_REPLICA = API{5, 0}
  21. REQUEST_OFFSET_COMMIT = API{6, 0}
  22. REQUEST_OFFSET_FETCH = API{7, 0}
  23. )
  24. type Client struct {
  25. addr string
  26. id *string
  27. correlation_id int32
  28. conn net.Conn
  29. responses chan response
  30. }
  31. type response struct {
  32. correlation_id int32
  33. buf []byte
  34. }
  35. func NewClient(addr string) (client *Client, err error) {
  36. conn, err := net.Dial("tcp", addr)
  37. if err != nil {
  38. return nil, err
  39. }
  40. client = &Client{addr, nil, 0, conn, make(chan response)}
  41. go client.readLoop()
  42. return client, err
  43. }
  44. func (client *Client) write(buf []byte) (err error) {
  45. size := make([]byte, 4)
  46. binary.BigEndian.PutUint32(size, uint32(len(buf)))
  47. _, err = client.conn.Write(size)
  48. if err != nil {
  49. return err
  50. }
  51. _, err = client.conn.Write(buf)
  52. if err != nil {
  53. return err
  54. }
  55. return nil
  56. }
  57. func (client *Client) readLoop() {
  58. var resp response
  59. header := make([]byte, 4)
  60. for {
  61. n, err := client.conn.Read(header)
  62. if err != nil || n != 4 {
  63. close(client.responses)
  64. return
  65. }
  66. length := int32(binary.BigEndian.Uint32(header))
  67. if length <= 4 {
  68. close(client.responses)
  69. return
  70. }
  71. n, err = client.conn.Read(header)
  72. if err != nil || n != 4 {
  73. close(client.responses)
  74. return
  75. }
  76. resp.correlation_id = int32(binary.BigEndian.Uint32(header))
  77. resp.buf = make([]byte, length-4)
  78. n, err = client.conn.Read(resp.buf)
  79. if err != nil || n != int(length-4) {
  80. close(client.responses)
  81. return
  82. }
  83. client.responses <- resp
  84. }
  85. }
  86. func (client *Client) sendRequest(api API, body []byte) (err error) {
  87. idLen, err := stringLen(client.id)
  88. if err != nil {
  89. return err
  90. }
  91. buf := make([]byte, 4+idLen+len(body))
  92. off := 0
  93. binary.BigEndian.PutUint16(buf[off:], uint16(api.key))
  94. off += 2
  95. binary.BigEndian.PutUint16(buf[off:], uint16(api.version))
  96. off += 2
  97. binary.BigEndian.PutUint32(buf[off:], uint32(client.correlation_id))
  98. off += 4
  99. client.correlation_id++
  100. off, err = encodeString(buf, off, client.id)
  101. if err != nil {
  102. return err
  103. }
  104. copy(buf[off:], body)
  105. return client.write(buf)
  106. }
  107. func (client *Client) sendMetadataRequest(topics []string) (err error) {
  108. bufLen := 4
  109. for i := range topics {
  110. bufLen += len(topics[i])
  111. }
  112. buf := make([]byte, bufLen)
  113. off := 0
  114. binary.BigEndian.PutUint32(buf[off:], uint32(len(topics)))
  115. off += 4
  116. for i := range topics {
  117. off, err = encodeString(buf, off, &topics[i])
  118. if err != nil {
  119. return err
  120. }
  121. }
  122. return client.sendRequest(REQUEST_METADATA, buf)
  123. }