2024-12-18 16:25:49 +08:00
|
|
|
// Package realtimedata define real time data operation functions
|
|
|
|
|
package realtimedata
|
2024-11-28 11:46:40 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"time"
|
|
|
|
|
|
2024-12-25 16:34:57 +08:00
|
|
|
"modelRT/logger"
|
2024-11-28 15:29:34 +08:00
|
|
|
|
2024-11-28 11:46:40 +08:00
|
|
|
"github.com/confluentinc/confluent-kafka-go/kafka"
|
2024-11-28 15:29:34 +08:00
|
|
|
"go.uber.org/zap"
|
2024-11-28 11:46:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// RealTimeDataComputer continuously processing real-time data from Kafka specified topics
|
2024-11-28 15:29:34 +08:00
|
|
|
func RealTimeDataComputer(ctx context.Context, consumerConfig kafka.ConfigMap, topics []string, duration string) {
|
|
|
|
|
// context for graceful shutdown
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
// get a logger
|
2024-12-25 16:34:57 +08:00
|
|
|
logger := logger.GetLoggerInstance()
|
2024-11-28 11:46:40 +08:00
|
|
|
|
2024-11-28 15:29:34 +08:00
|
|
|
// setup a channel to listen for interrupt signals
|
2024-12-16 15:37:44 +08:00
|
|
|
// TODO 将中断信号放到入参中
|
2024-11-28 15:29:34 +08:00
|
|
|
interrupt := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
|
|
// read message (-1 means wait indefinitely)
|
|
|
|
|
timeoutDuration, err := time.ParseDuration(duration)
|
|
|
|
|
|
|
|
|
|
// create a new consumer
|
2024-11-28 11:46:40 +08:00
|
|
|
consumer, err := kafka.NewConsumer(&consumerConfig)
|
|
|
|
|
if err != nil {
|
2024-11-28 15:29:34 +08:00
|
|
|
logger.Error("init kafka consume by config failed", zap.Any("config", consumerConfig), zap.Error(err))
|
2024-11-28 11:46:40 +08:00
|
|
|
}
|
|
|
|
|
|
2024-11-28 15:29:34 +08:00
|
|
|
// subscribe to the topic
|
2024-11-28 11:46:40 +08:00
|
|
|
err = consumer.SubscribeTopics(topics, nil)
|
|
|
|
|
if err != nil {
|
2024-11-28 15:29:34 +08:00
|
|
|
logger.Error("subscribe to the topic failed", zap.Strings("topic", topics), zap.Error(err))
|
2024-11-28 11:46:40 +08:00
|
|
|
}
|
|
|
|
|
|
2024-11-28 15:29:34 +08:00
|
|
|
// start a goroutine to handle shutdown
|
2024-11-28 11:46:40 +08:00
|
|
|
go func() {
|
|
|
|
|
<-interrupt
|
|
|
|
|
cancel()
|
|
|
|
|
consumer.Close()
|
|
|
|
|
}()
|
|
|
|
|
|
2024-11-28 15:29:34 +08:00
|
|
|
// continuously read messages from Kafka
|
2024-11-28 11:46:40 +08:00
|
|
|
for {
|
2024-11-28 15:29:34 +08:00
|
|
|
msg, err := consumer.ReadMessage(timeoutDuration)
|
2024-11-28 11:46:40 +08:00
|
|
|
if err != nil {
|
|
|
|
|
if ctx.Err() == context.Canceled {
|
2024-11-28 15:29:34 +08:00
|
|
|
logger.Info("context canceled, stopping read loop")
|
2024-11-28 11:46:40 +08:00
|
|
|
break
|
|
|
|
|
}
|
2024-11-28 15:29:34 +08:00
|
|
|
logger.Error("consumer read message failed", zap.Error(err))
|
2024-11-28 11:46:40 +08:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 15:29:34 +08:00
|
|
|
// TODO 使用 ants.pool处理 kafka 的订阅数据
|
|
|
|
|
_, err = consumer.CommitMessage(msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error("manual submission information failed", zap.Any("message", msg), zap.Error(err))
|
|
|
|
|
}
|
2024-11-28 11:46:40 +08:00
|
|
|
}
|
|
|
|
|
}
|