IBM / sarama

Sarama is a Go library for Apache Kafka.
MIT License
11.57k stars 1.76k forks source link

Health check with sarama.AsyncProducer? #1436

Closed Zaazik closed 5 years ago

Zaazik commented 5 years ago
Versions

Sarama Version: 1.22.1 Kafka Version: 1.1.1 Go Version:1.12

Problem Description

Hello, how we can make health check with sarama.AsyncProducer?

Zaazik commented 5 years ago

Resolve that using sarama.NewAsyncProducerFromClient(saramaClient):

func NewProducer(saramaCfg *sarama.Config, brokers []string) (*Producer, error) {
    saramaClient, err := sarama.NewClient(brokers, saramaCfg)
    if err!=nil{
        return nil, err
    }

    saramaProducer, err := sarama.NewAsyncProducerFromClient(saramaClient)
    if err != nil {
        return nil, errors.WithStack(err)
    }

    producer := &Producer{client:saramaClient, saramaProducer: saramaProducer}

    return producer, nil
}

Attach client to internal struct:

type Producer struct {
    client sarama.Client
    saramaProducer   sarama.AsyncProducer
}

And create method IsHealthy

func (p *Producer) IsHealthy() bool {
    return len(p.client.Brokers()) > 0
}

Its look like tricky, but its works for me)

d1egoaz commented 5 years ago

Hey @Zaazik it seems you found an answer for this, can this issue be closed?

d1egoaz commented 5 years ago

seems it's already resolved, feel free to re-open the issue if need more help.

lusson-luo commented 1 year ago

@Zaazik Can this feature be integrated into the standard version? @d1egoaz