github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/kafka/producer.go (about)

     1  /*
     2  Copyright 2021 The Dapr Authors
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6      http://www.apache.org/licenses/LICENSE-2.0
     7  Unless required by applicable law or agreed to in writing, software
     8  distributed under the License is distributed on an "AS IS" BASIS,
     9  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  See the License for the specific language governing permissions and
    11  limitations under the License.
    12  */
    13  
    14  package kafka
    15  
    16  import (
    17  	"context"
    18  	"errors"
    19  	"fmt"
    20  
    21  	"github.com/Shopify/sarama"
    22  )
    23  
    24  func getSyncProducer(config sarama.Config, brokers []string, maxMessageBytes int) (sarama.SyncProducer, error) {
    25  	// Add SyncProducer specific properties to copy of base config
    26  	config.Producer.RequiredAcks = sarama.WaitForAll
    27  	config.Producer.Retry.Max = 5
    28  	config.Producer.Return.Successes = true
    29  
    30  	if maxMessageBytes > 0 {
    31  		config.Producer.MaxMessageBytes = maxMessageBytes
    32  	}
    33  
    34  	producer, err := sarama.NewSyncProducer(brokers, &config)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	return producer, nil
    40  }
    41  
    42  // Publish message to Kafka cluster.
    43  func (k *Kafka) Publish(_ context.Context, topic string, data []byte, metadata map[string]string) error {
    44  	if k.Producer == nil {
    45  		return errors.New("component is closed")
    46  	}
    47  	// k.logger.Debugf("Publishing topic %v with data: %v", topic, string(data))
    48  	k.logger.Info(fmt.Sprintf("Publishing on topic %v", topic))
    49  
    50  	msg := &sarama.ProducerMessage{
    51  		Topic: topic,
    52  		Value: sarama.ByteEncoder(data),
    53  	}
    54  
    55  	for name, value := range metadata {
    56  		if name == key {
    57  			msg.Key = sarama.StringEncoder(value)
    58  		} else {
    59  			if msg.Headers == nil {
    60  				msg.Headers = make([]sarama.RecordHeader, 0, len(metadata))
    61  			}
    62  			msg.Headers = append(msg.Headers, sarama.RecordHeader{
    63  				Key:   []byte(name),
    64  				Value: []byte(value),
    65  			})
    66  		}
    67  	}
    68  
    69  	partition, offset, err := k.Producer.SendMessage(msg)
    70  
    71  	k.logger.Info(fmt.Sprintf("Partition: %v, offset: %v", partition, offset))
    72  
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	return nil
    78  }