pub.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package http
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "go-common/app/infra/databus/conf"
  7. "go-common/app/infra/databus/dsn"
  8. "go-common/app/infra/databus/tcp"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. )
  12. const (
  13. _nonRetriableErr = 1
  14. _retriableErr = 2
  15. )
  16. // Record pub message
  17. type Record struct {
  18. Key string `json:"key"`
  19. Value json.RawMessage `json:"value"`
  20. }
  21. // OffsetMsg response message
  22. type OffsetMsg struct {
  23. Partition int32 `json:"partition"`
  24. Offset int64 `json:"offset"`
  25. ErrorCode int64 `json:"error_code"`
  26. }
  27. // 实现 kafka http 发布接口。
  28. //
  29. // POST /databus/pub?topic=test&group=foo&color=purple HTTP/1.1
  30. // Host: databus.bilibili.co
  31. // Authorization: Basic Zm9vOmJhcg==
  32. // Content-Type: application/json
  33. //
  34. // {
  35. // "records": [
  36. // {
  37. // "key": "somekey",
  38. // "value": {"foo": "bar"}
  39. // },
  40. // {
  41. // "key": "somekey",
  42. // "value": {"foo": "bar"}
  43. // }
  44. // ]
  45. // }
  46. //
  47. // HTTP/1.1 200 OK
  48. // Content-Type: application/json
  49. //
  50. // {
  51. // "offsets": [
  52. // {
  53. // "error_code": 0,
  54. // "partition": 1,
  55. // "offset": 100,
  56. // },
  57. // {
  58. // "error_code": 0,
  59. // "partition": 1,
  60. // "offset": 101,
  61. // }
  62. // ]
  63. // }
  64. func pub(c *bm.Context) {
  65. key, secret, _ := c.Request.BasicAuth()
  66. dsn := &dsn.DSN{
  67. Role: "pub",
  68. Key: key,
  69. Secret: secret,
  70. Topic: c.Request.Form.Get("topic"),
  71. Group: c.Request.Form.Get("group"),
  72. Color: c.Request.Form.Get("color"),
  73. }
  74. cfg, _, err := tcp.Auth(dsn, c.Request.Host)
  75. if err != nil {
  76. // TODO 根据错误选择 HTTP 状态码
  77. http.Error(c.Writer, err.Error(), http.StatusBadRequest)
  78. log.Error("auth failed, err(%v)", err)
  79. return
  80. }
  81. records, err := parseRecords(c)
  82. if err != nil {
  83. http.Error(c.Writer, err.Error(), http.StatusBadRequest)
  84. return
  85. }
  86. var d struct {
  87. Offsets []*OffsetMsg `json:"offsets"`
  88. }
  89. // 使用 OffsetMsg.Error 表示错误,此处不再处理返回的 err
  90. d.Offsets, err = pubRecords(dsn.Group, dsn.Topic, dsn.Color, cfg, records)
  91. if err != nil {
  92. http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
  93. return
  94. }
  95. rsp, err := json.Marshal(d)
  96. if err != nil {
  97. http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
  98. return
  99. }
  100. c.Writer.Header().Set("content-type", "application/json")
  101. c.Writer.Write(rsp)
  102. }
  103. func pubRecords(group, topic, color string, cfg *conf.Kafka, records []*Record) (offsets []*OffsetMsg, err error) {
  104. p, err := tcp.NewPub(nil, group, topic, color, cfg)
  105. if err != nil {
  106. return
  107. }
  108. for _, r := range records {
  109. var m OffsetMsg
  110. offsets = append(offsets, &m)
  111. // OffsetMsg 与 Record 一一对应
  112. // Publish 出错后继续循环填充
  113. if err != nil {
  114. // TODO 区分错误码
  115. m.ErrorCode = _nonRetriableErr
  116. continue
  117. }
  118. // TODO 支持 metadata
  119. m.Partition, m.Offset, err = p.Publish([]byte(r.Key), nil, []byte(r.Value))
  120. if err != nil {
  121. m.ErrorCode = _nonRetriableErr
  122. p.Close(false)
  123. }
  124. }
  125. return
  126. }
  127. func parseRecords(c *bm.Context) ([]*Record, error) {
  128. b, err := ioutil.ReadAll(c.Request.Body)
  129. if err != nil {
  130. return nil, err
  131. }
  132. defer c.Request.Body.Close()
  133. var d struct {
  134. Records []*Record
  135. }
  136. if err = json.Unmarshal(b, &d); err != nil {
  137. return nil, err
  138. }
  139. return d.Records, nil
  140. }