MCPcopy Index your code
hub / github.com/Trendyol/kafka-cronsumer

github.com/Trendyol/kafka-cronsumer @v1.6.8

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.6.8 ↗ · + Follow
228 symbols 738 edges 37 files 22 documented · 10%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Kafka C[r]onsumer Go Reference Go Report Card

Description

Kafka Cronsumer is mainly used for retry/exception strategy management. It works based on cron expression and consumes messages in a timely manner with the power of auto pause and concurrency.

For details check our blog post

If you need a whole consumer lifecycle with exception management, check Kafka Konsumer

How Kafka Cronsumer Works

How Kafka Cronsumer Works

When to use it?

  • Iteration-based back-off strategies are applicable
  • Messages could be processed in an eventually consistent state
  • Max retry exceeded messages could be ignored and send to dead letter topic
  • To increase consumer resiliency
  • To increase consumer performance with concurrency

When to avoid?

  • Messages should be processed in order
  • Messages should be certainly processed (we discard messages if max retry is exceeded)
  • Messages should be committed (we use auto-commit interval for increasing performance)
  • Messages with TTL (Time to Live)

Guide

Installation

go get github.com/Trendyol/kafka-cronsumer@latest

Examples

You can find a number of ready-to-run examples at this directory.

After running docker-compose up command, you can run any application you want. Don't forget its cron based :)

Single Consumer

func main() {
  // ...
  var consumeFn kafka.ConsumeFn = func (message kafka.Message) error {
    fmt.Printf("consumer > Message received: %s\n", string(message.Value))
    return nil
  }

  c := cronsumer.New(kafkaConfig, consumeFn)
  c.Run()
}

Single Consumer With Dead Letter

func main() {
  // ...
  var consumeFn kafka.ConsumeFn = func (message kafka.Message) error {
    fmt.Printf("consumer > Message received: %s\n", string(message.Value))
    return errors.New("error occurred")
  }

  c := cronsumer.New(kafkaConfig, consumeFn)
  c.Run()
}

Multiple Consumers

func main() {
  // ...
  var firstConsumerFn kafka.ConsumeFn = func (message kafka.Message) error {
    fmt.Printf("First consumer > Message received: %s\n", string(message.Value))
    return nil
  }
  first := cronsumer.New(firstCfg, firstConsumerFn)
  first.Start()

  var secondConsumerFn kafka.ConsumeFn = func (message kafka.Message) error {
    fmt.Printf("Second consumer > Message received: %s\n", string(message.Value))
    return nil
  }
  second := cronsumer.New(secondCfg, secondConsumerFn)
  second.Start()
  // ...    
}

Single Consumer With Metric collector

func main() {
  // ...
  var consumeFn kafka.ConsumeFn = func(message kafka.Message) error {
    return errors.New("err occurred")
  }

  c := cronsumer.New(config, consumeFn)
  StartAPI(*config, c.GetMetricCollectors()...)
  c.Start()
  // ...    
}

func StartAPI(cfg kafka.Config, metricCollectors ...prometheus.Collector) {
  // ...
  f := fiber.New(
    fiber.Config{},
  )

  metricMiddleware, err := NewMetricMiddleware(cfg, f, metricCollectors...)

  f.Use(metricMiddleware)
  // ...
}

Configurations

config description default example
logLevel Describes log level, valid options are debug, info, warn, and error info
metricPrefix MetricPrefix is used for prometheus fq name prefix. If not provided, default metric prefix value is kafka_cronsumer. Currently, there are two exposed prometheus metrics. retried_messages_total_current and discarded_messages_total_current. So, if default metric prefix used, metrics names are kafka_cronsumer_retried_messages_total_current and kafka_cronsumer_discarded_messages_total_current kafka_cronsumer
consumer.clientId see doc
consumer.cron Cron expression when exception consumer starts to work at */1 * * * *
consumer.backOffStrategy Define consumer backoff strategy for retry topics fixed exponential, linear
consumer.duration Work duration exception consumer actively consuming messages NonStopWork (zero duration) 20s, 15m, 1h, NonStopWork (zero duration)
consumer.brokers broker address
consumer.topic Exception topic names exception-topic
consumer.groupId Exception consumer group id exception-consumer-group
consumer.maxRetry Maximum retry value for attempting to retry a message 3
consumer.concurrency Number of goroutines used at listeners 1
consumer.minBytes see doc 1
consumer.maxBytes see doc 1 MB
consumer.maxWait see doc 10s
consumer.commitInterval see doc 1s
consumer.heartbeatInterval see doc 3s
consumer.sessionTimeout see doc 30s
consumer.rebalanceTimeout see doc 30s
consumer.startOffset see doc

Extension points exported contracts — how you extend this code

Interface (Interface)
Interface is a logger that supports log levels, context and structured logging. [1 implementers]
pkg/logger/logger.go
BackoffStrategyInterface (Interface)
(no doc) [3 implementers]
pkg/kafka/backoff_strategy.go
Consumer (Interface)
(no doc) [2 implementers]
internal/consumer.go
Producer (Interface)
(no doc) [2 implementers]
internal/producer.go
ConsumeFn (FuncType)
ConsumeFn function describes how to consume messages from specified topic
pkg/kafka/cron_client.go
Cronsumer (Interface)
(no doc) [1 implementers]
pkg/kafka/cron_client.go
SkipMessageByHeaderFn (FuncType)
(no doc)
pkg/kafka/config.go

Core symbols most depended-on inside this repo

Errorf
called by 86
pkg/logger/logger.go
Run
called by 32
pkg/kafka/cron_client.go
Error
called by 25
pkg/logger/logger.go
Start
called by 16
pkg/kafka/cron_client.go
GetBackoffStrategy
called by 14
pkg/kafka/backoff_strategy.go
String
called by 10
pkg/kafka/backoff_strategy.go
Produce
called by 8
internal/producer.go
New
called by 6
pkg/logger/zap.go

Shape

Method 104
Function 88
Struct 25
Interface 6
TypeAlias 3
FuncType 2

Languages

Go100%

Modules by API surface

pkg/logger/logger.go14 symbols
pkg/kafka/cronsumer_message.go14 symbols
pkg/kafka/backoff_strategy.go13 symbols
internal/cronsumer_test.go13 symbols
examples/single-consumer-with-custom-logger/logger.go13 symbols
test/integration/integration_test.go12 symbols
pkg/kafka/config.go12 symbols
internal/producer.go11 symbols
internal/cronsumer_client.go11 symbols
internal/cronsumer.go11 symbols
internal/message.go10 symbols
pkg/kafka/cronsumer_message_test.go9 symbols

For agents

$ claude mcp add kafka-cronsumer \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact