/
kafka_consumer.go
executable file
·49 lines (38 loc) · 1.32 KB
/
kafka_consumer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright [2021] - [2022], AssetMantle Pte. Ltd. and the code contributors
// SPDX-License-Identifier: Apache-2.0
package queuing
import (
"github.com/Shopify/sarama"
"github.com/cosmos/cosmos-sdk/codec"
)
// newConsumer : is a consumer which is needed to create child consumers to consume topics
func newConsumer(kafkaNodes []string) sarama.Consumer {
config := sarama.NewConfig()
consumer, err := sarama.NewConsumer(kafkaNodes, config)
if err != nil {
panic(err)
}
return consumer
}
// partitionConsumers : is a child consumer
func partitionConsumers(consumer sarama.Consumer, topic string) sarama.PartitionConsumer {
// partition and offset defined in configurations.go
partitionConsumer, err := consumer.ConsumePartition(topic, partition, offset)
if err != nil {
panic(err)
}
return partitionConsumer
}
// kafkaTopicConsumer : Takes a consumer and makes it consume a topic message at a time
func kafkaTopicConsumer(topic string, consumers map[string]sarama.PartitionConsumer, legacyAmino *codec.LegacyAmino) kafkaMsg {
partitionConsumer := consumers[topic]
if len(partitionConsumer.Messages()) == 0 {
return kafkaMsg{Msg: nil}
}
var consumedKafkaMsg kafkaMsg
err := legacyAmino.UnmarshalJSON((<-partitionConsumer.Messages()).Value, &consumedKafkaMsg)
if err != nil {
panic(err)
}
return consumedKafkaMsg
}