consumer.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package consumer
  2. import (
  3. "errors"
  4. "fmt"
  5. "git.beejay.kim/tool/service"
  6. "github.com/confluentinc/confluent-kafka-go/v2/kafka"
  7. "github.com/google/uuid"
  8. "github.com/kelindar/bitmap"
  9. "github.com/rs/zerolog/log"
  10. "strings"
  11. "sync"
  12. "time"
  13. )
  14. const (
  15. flagSubscribed uint32 = iota
  16. flagPaused
  17. )
  18. //goland:noinspection ALL
  19. type _consumer struct {
  20. config *Config
  21. state bitmap.Bitmap
  22. handlers []service.ConsumerHandler
  23. session *kafka.Consumer
  24. }
  25. func New(cfg *Config) (service.Consumer, error) {
  26. var (
  27. c = &_consumer{
  28. config: cfg,
  29. }
  30. err error
  31. )
  32. if cfg == nil {
  33. return nil, fmt.Errorf("config must be provided")
  34. }
  35. opts := &kafka.ConfigMap{
  36. "broker.address.family": "v4",
  37. "bootstrap.servers": strings.Join(c.config.Hosts, ","),
  38. "group.id": c.config.Group,
  39. "partition.assignment.strategy": "cooperative-sticky",
  40. "auto.offset.reset": "earliest",
  41. "log_level": 0,
  42. }
  43. if c.session, err = kafka.NewConsumer(opts); err != nil {
  44. return nil, err
  45. }
  46. return c, nil
  47. }
  48. func (c *_consumer) ID() uuid.UUID {
  49. return uuid.NewSHA1(uuid.NameSpaceDNS, []byte("consumer.kafka"))
  50. }
  51. func (c *_consumer) Subscribed() bool {
  52. return c.state.Contains(flagSubscribed)
  53. }
  54. func (c *_consumer) Paused() bool {
  55. return c.state.Contains(flagPaused)
  56. }
  57. func (c *_consumer) SetPause(is bool) error {
  58. if is {
  59. c.state.Set(flagPaused)
  60. } else {
  61. c.state.Remove(flagPaused)
  62. }
  63. return nil
  64. }
  65. func (c *_consumer) RegisterHandlers(handlers ...service.ConsumerHandler) {
  66. c.handlers = append(c.handlers, handlers...)
  67. }
  68. func (c *_consumer) Subscribe(topics []string, ch chan *kafka.Message) error {
  69. if c.Subscribed() {
  70. return fmt.Errorf("illegal state: already subscribed")
  71. }
  72. if err := c.session.SubscribeTopics(topics, rebalanceCallback); err != nil {
  73. return err
  74. }
  75. c.state.Set(flagSubscribed)
  76. for c.Subscribed() {
  77. for c.Paused() {
  78. time.Sleep(100 * time.Millisecond)
  79. }
  80. message, err := c.poll()
  81. if err != nil {
  82. // silently wait for a next message
  83. if errors.Is(err, ErrNoMessage) {
  84. continue
  85. }
  86. log.Debug().
  87. Str("service", "consumer").
  88. Err(err).
  89. Send()
  90. c.state.Remove(flagSubscribed)
  91. return err
  92. }
  93. if message != nil {
  94. ch <- message
  95. }
  96. }
  97. log.Debug().Msg("consumer closed gracefully")
  98. return nil
  99. }
  100. func (c *_consumer) Close() error {
  101. if c.Subscribed() {
  102. c.state.Remove(flagSubscribed)
  103. }
  104. var wg sync.WaitGroup
  105. wg.Add(1)
  106. go func() {
  107. defer wg.Done()
  108. if c.session != nil {
  109. time.Sleep(time.Second)
  110. _ = c.session.Close() //nolint:errcheck
  111. }
  112. }()
  113. wg.Wait()
  114. return nil
  115. }
  116. func (c *_consumer) poll() (*kafka.Message, error) {
  117. var (
  118. ev = c.session.Poll(c.config.Timeout)
  119. err error
  120. )
  121. switch e := ev.(type) {
  122. case *kafka.Message:
  123. for i := range c.handlers {
  124. if err = c.handlers[i](e); err != nil {
  125. return nil, err
  126. }
  127. }
  128. return e, nil
  129. case kafka.Error:
  130. log.Debug().
  131. Str("service", "consumer").
  132. Err(e).
  133. Send()
  134. if e.Code() == kafka.ErrAllBrokersDown {
  135. c.state.Remove(flagSubscribed)
  136. }
  137. return nil, e
  138. default:
  139. if e != nil {
  140. log.Debug().
  141. Str("service", "consumer").
  142. Any("event", e).
  143. Send()
  144. }
  145. return nil, ErrNoMessage
  146. }
  147. }
  148. // rebalanceCallback is called on each group rebalance to assign additional
  149. // partitions, or remove existing partitions, from the consumer's current
  150. // assignment.
  151. //
  152. // The application may use this optional callback to inspect the assignment,
  153. // alter the initial start offset (the .Offset field of each assigned partition),
  154. // and read/write offsets to commit to an alternative store outside of Kafka.
  155. func rebalanceCallback(c *kafka.Consumer, event kafka.Event) error {
  156. switch ev := event.(type) {
  157. case kafka.AssignedPartitions:
  158. log.Debug().
  159. Str("service", "consumer").
  160. Msgf("%s rebalance: %d new partition(s) assigned: %v",
  161. c.GetRebalanceProtocol(),
  162. len(ev.Partitions),
  163. ev.Partitions)
  164. // The application may update the start .Offset of each
  165. // assigned partition and then call IncrementalAssign().
  166. if err := c.IncrementalAssign(ev.Partitions); err != nil {
  167. panic(err)
  168. }
  169. case kafka.RevokedPartitions:
  170. log.Debug().
  171. Str("service", "consumer").
  172. Msgf("%s rebalance: %d partition(s) revoked: %v",
  173. c.GetRebalanceProtocol(),
  174. len(ev.Partitions),
  175. ev.Partitions)
  176. // Usually, the rebalance callback for `RevokedPartitions` is called
  177. // just before the partitions are revoked. We can be certain that a
  178. // partition being revoked is not yet owned by any other consumer.
  179. // This way, logic like storing any pending offsets or committing
  180. // offsets can be handled.
  181. // However, there can be cases where the assignment is lost
  182. // involuntarily. In this case, the partition might already be owned
  183. // by another consumer, and operations including committing
  184. // offsets may not work.
  185. if c.AssignmentLost() {
  186. // Our consumer has been kicked out of the group and the
  187. // entire assignment is thus lost.
  188. log.Debug().
  189. Str("service", "consumer").
  190. Msg("Assignment lost involuntarily, commit may fail")
  191. }
  192. // The client automatically calls IncrementalUnassign() unless
  193. // the callback has already called that method.
  194. default:
  195. log.Debug().
  196. Str("service", "consumer").
  197. Msgf("unxpected event type: %v", event)
  198. }
  199. return nil
  200. }