Skip to content

Commit

Permalink
feat: added topic exists check (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
keremcankabadayi committed Feb 26, 2024
1 parent a73ddbe commit 72deca3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
26 changes: 26 additions & 0 deletions consumer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kafka

import (

Check failure on line 3 in consumer.go

View workflow job for this annotation

GitHub Actions / build

File is not `gofumpt`-ed (gofumpt)
"github.com/segmentio/kafka-go/topics"
"time"

Check failure on line 5 in consumer.go

View workflow job for this annotation

GitHub Actions / build

File is not `gofumpt`-ed (gofumpt)

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -55,6 +56,8 @@ func (c *consumer) GetMetricCollectors() []prometheus.Collector {
func (c *consumer) Consume() {
go c.subprocesses.Start()

c.topicExists()

c.wg.Add(1)
go c.startConsume()

Expand Down Expand Up @@ -157,3 +160,26 @@ func (c *consumer) process(message *Message) {
c.metric.TotalProcessedMessagesCounter++
}
}

func (c *consumer) topicExists() {
list, err := topics.List(c.context, &kafka.Client{
Addr: kafka.TCP(c.brokers...),
Timeout: 3 * time.Second,
})

Check failure on line 169 in consumer.go

View workflow job for this annotation

GitHub Actions / build

File is not `gofumpt`-ed (gofumpt)
if err != nil {
c.logger.Errorf("Topic Exists Function Err %s", err.Error())
}

var exist bool
for i := range list {
if list[i].Name == c.topic {
exist = true
break
}
}

if !exist {
c.logger.Errorf("Topic doesn't exist")
}
}
4 changes: 4 additions & 0 deletions consumer_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type base struct {
incomingMessageStream chan *IncomingMessage
singleConsumingStream chan *Message
batchConsumingStream chan []*Message
brokers []string
topic string
retryTopic string
subprocesses subprocesses
wg sync.WaitGroup
Expand Down Expand Up @@ -116,6 +118,8 @@ func newBase(cfg *ConsumerConfig, messageChSize int) (*base, error) {
consumerState: stateRunning,
skipMessageByHeaderFn: cfg.SkipMessageByHeaderFn,
metricPrefix: cfg.MetricPrefix,
brokers: cfg.Reader.Brokers,
topic: cfg.Reader.Topic,
}

if cfg.DistributedTracingEnabled {
Expand Down

0 comments on commit 72deca3

Please sign in to comment.