github.com/argoproj/argo-events@v1.9.1/eventbus/kafka/eventsource/source_conn.go (about) 1 package eventsource 2 3 import ( 4 "context" 5 6 "github.com/IBM/sarama" 7 "github.com/argoproj/argo-events/eventbus/common" 8 "github.com/argoproj/argo-events/eventbus/kafka/base" 9 "go.uber.org/zap" 10 ) 11 12 type KafkaSourceConnection struct { 13 *base.KafkaConnection 14 Topic string 15 Client sarama.Client 16 Producer sarama.SyncProducer 17 } 18 19 func (c *KafkaSourceConnection) Publish(ctx context.Context, msg common.Message) error { 20 key := base.EventKey(msg.EventSourceName, msg.EventName) 21 partition, offset, err := c.Producer.SendMessage(&sarama.ProducerMessage{ 22 Topic: c.Topic, 23 Key: sarama.StringEncoder(key), 24 Value: sarama.ByteEncoder(msg.Body), 25 }) 26 27 if err != nil { 28 // fail fast if topic does not exist 29 if err == sarama.ErrUnknownTopicOrPartition { 30 c.Logger.Fatalf( 31 "Topic does not exist. Please ensure the topic '%s' has been created, or the kafka setting '%s' is set to true.", 32 c.Topic, 33 "auto.create.topics.enable", 34 ) 35 } 36 37 return err 38 } 39 40 c.Logger.Infow("Published message to kafka", zap.String("topic", c.Topic), zap.String("key", key), zap.Int32("partition", partition), zap.Int64("offset", offset)) 41 42 return nil 43 } 44 45 func (c *KafkaSourceConnection) Close() error { 46 if err := c.Producer.Close(); err != nil { 47 return err 48 } 49 50 return c.Client.Close() 51 } 52 53 func (c *KafkaSourceConnection) IsClosed() bool { 54 return c.Client.Closed() 55 }