feat: code cleanup
This commit is contained in:
28
producer.go
28
producer.go
@@ -16,10 +16,6 @@ import (
|
||||
)
|
||||
|
||||
// InitProducer 初始化生产者
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 3:52 下午 2021/9/21
|
||||
func InitProducer(hostList []string, topic string, cfg *sarama.Config) (*Producer, error) {
|
||||
if cfg == nil {
|
||||
cfg = sarama.NewConfig()
|
||||
@@ -43,10 +39,6 @@ func InitProducer(hostList []string, topic string, cfg *sarama.Config) (*Produce
|
||||
}
|
||||
|
||||
// Producer 生产者
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 3:57 下午 2021/9/21
|
||||
type Producer struct {
|
||||
cfg *sarama.Config
|
||||
hostList []string
|
||||
@@ -57,10 +49,6 @@ type Producer struct {
|
||||
}
|
||||
|
||||
// init kafka初始化
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 3:58 下午 2021/9/21
|
||||
func (p *Producer) init() error {
|
||||
var (
|
||||
err error
|
||||
@@ -85,20 +73,12 @@ func (p *Producer) init() error {
|
||||
}
|
||||
|
||||
// Destroy 断开生产者连接时,触发的逻辑
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 3:58 下午 2021/9/21
|
||||
func (p *Producer) Destroy() {
|
||||
// 断开客户端链接
|
||||
_ = p.client.Close()
|
||||
}
|
||||
|
||||
// Sync 同步生产数据
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 4:08 下午 2021/9/21
|
||||
func (p *Producer) Sync(data ProducerData) ProducerResult {
|
||||
mes := p.buildMessage(data)
|
||||
result := ProducerResult{}
|
||||
@@ -107,10 +87,6 @@ func (p *Producer) Sync(data ProducerData) ProducerResult {
|
||||
}
|
||||
|
||||
// Async 异步生产数据
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 4:09 下午 2021/9/21
|
||||
func (p *Producer) Async(data ProducerData) ProducerResult {
|
||||
result := ProducerResult{}
|
||||
p.asyncProducer.Input() <- p.buildMessage(data)
|
||||
@@ -129,10 +105,6 @@ func (p *Producer) Async(data ProducerData) ProducerResult {
|
||||
}
|
||||
|
||||
// buildMessage 构建消息
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 7:32 下午 2021/9/21
|
||||
func (p *Producer) buildMessage(data ProducerData) *sarama.ProducerMessage {
|
||||
mes := &sarama.ProducerMessage{
|
||||
Topic: p.topic,
|
||||
|
||||
Reference in New Issue
Block a user