confluentinc / confluent-kafka-go

Confluent's Apache Kafka Golang client
Apache License 2.0
4.65k stars 659 forks source link

How to specify private channel in Channel-Based producer? #627

Closed iAziz786 closed 3 years ago

iAziz786 commented 3 years ago

Description

In the README it's mentioned that we can use private channel with producer.ProducerChannel() I'm not able to find how to do so since the example which is given is using the producer.Events() function.

My use case is that I wanted to inform the caller of the ProducerChannel by a call back function if any error occurs therefore I need the private channel for that. I know I can do it with producer.Produce() but wanted to know how it is possible with producer channel.

image

iAziz786 commented 3 years ago

Here is what I'm doing right now with the Produce() method.

type KafkaProducer struct {
    producer *kafka.Producer
}

func (kp *KafkaProducer) Produce(msg *kafka.Message, fn func(error)) error {
    deliveryChan := make(chan kafka.Event)
    go func() {
        e := <-deliveryChan
        if m, ok := e.(*kafka.Message); ok {
            fn(m.TopicPartition.Error)
        }
    }()

    return kp.producer.Produce(msg, deliveryChan)
}
edenhill commented 3 years ago

Yep, your deliveryChan is the private (or rather user-specified) delivery channel, as opposed to the default p.Events() channel.

iAziz786 commented 3 years ago

@edenhill Yes, but how can I pass the private channel to producer.ProducerChannel()?