You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/kafka/producer.go

99 lines
3.4 KiB

package kafka
import (
2 years ago
"context"
2 years ago
"errors"
2 years ago
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
log "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2 years ago
"github.com/Shopify/sarama"
2 years ago
"google.golang.org/protobuf/proto"
2 years ago
2 years ago
prome "github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome"
)
2 years ago
var errEmptyMsg = errors.New("binary msg is empty")
2 years ago
type Producer struct {
topic string
addr []string
config *sarama.Config
producer sarama.SyncProducer
}
func NewKafkaProducer(addr []string, topic string) *Producer {
p := Producer{}
p.config = sarama.NewConfig() //Instantiate a sarama Config
p.config.Producer.Return.Successes = true //Whether to enable the successes channel to be notified after the message is sent successfully
p.config.Producer.Return.Errors = true
p.config.Producer.RequiredAcks = sarama.WaitForAll //Set producer Message Reply level 0 1 all
p.config.Producer.Partitioner = sarama.NewHashPartitioner //Set the hash-key automatic hash partition. When sending a message, you must specify the key value of the message. If there is no key, the partition will be selected randomly
2 years ago
if config.Config.Kafka.SASLUserName != "" && config.Config.Kafka.SASLPassword != "" {
2 years ago
p.config.Net.SASL.Enable = true
2 years ago
p.config.Net.SASL.User = config.Config.Kafka.SASLUserName
p.config.Net.SASL.Password = config.Config.Kafka.SASLPassword
2 years ago
}
p.addr = addr
p.topic = topic
producer, err := sarama.NewSyncProducer(p.addr, p.config) //Initialize the client
if err != nil {
panic(err.Error())
}
p.producer = producer
return &p
}
2 years ago
2 years ago
func GetMQHeaderWithContext(ctx context.Context) ([]sarama.RecordHeader, error) {
2 years ago
operationID, opUserID, platform, connID, err := mcontext.GetCtxInfos(ctx)
2 years ago
if err != nil {
return nil, err
}
return []sarama.RecordHeader{
{Key: []byte(constant.OperationID), Value: []byte(operationID)},
{Key: []byte(constant.OpUserID), Value: []byte(opUserID)},
{Key: []byte(constant.OpUserPlatform), Value: []byte(platform)},
{Key: []byte(constant.ConnID), Value: []byte(connID)}}, err
}
2 years ago
2 years ago
func GetContextWithMQHeader(header []*sarama.RecordHeader) context.Context {
var values []string
for _, recordHeader := range header {
values = append(values, string(recordHeader.Value))
}
2 years ago
return mcontext.WithMustInfoCtx(values) // TODO
2 years ago
}
2 years ago
func (p *Producer) SendMessage(ctx context.Context, key string, msg proto.Message) (int32, int64, error) {
log.ZDebug(ctx, "SendMessage", "msg", msg, "topic", p.topic, "key", key)
kMsg := &sarama.ProducerMessage{}
kMsg.Topic = p.topic
2 years ago
kMsg.Key = sarama.StringEncoder(key)
2 years ago
bMsg, err := proto.Marshal(msg)
if err != nil {
2 years ago
return 0, 0, utils.Wrap(err, "kafka proto Marshal err")
}
2 years ago
if len(bMsg) == 0 {
2 years ago
return 0, 0, utils.Wrap(errEmptyMsg, "")
2 years ago
}
kMsg.Value = sarama.ByteEncoder(bMsg)
if kMsg.Key.Length() == 0 || kMsg.Value.Length() == 0 {
2 years ago
return 0, 0, utils.Wrap(errEmptyMsg, "")
}
2 years ago
kMsg.Metadata = ctx
2 years ago
header, err := GetMQHeaderWithContext(ctx)
2 years ago
if err != nil {
return 0, 0, utils.Wrap(err, "")
}
2 years ago
kMsg.Headers = header
2 years ago
partition, offset, err := p.producer.SendMessage(kMsg)
2 years ago
log.ZDebug(ctx, "ByteEncoder SendMessage end", "key ", kMsg.Key, "key length", kMsg.Value.Length())
2 years ago
if err == nil {
2 years ago
prome.Inc(prome.SendMsgCounter)
2 years ago
}
2 years ago
return partition, offset, utils.Wrap(err, "")
}