github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/kafka/client.go (about)

     1  // Copyright (c) 2016 Arista Networks, Inc.
     2  // Use of this source code is governed by the Apache License 2.0
     3  // that can be found in the COPYING file.
     4  
     5  package kafka
     6  
     7  import (
     8  	"os"
     9  	"time"
    10  
    11  	"github.com/IBM/sarama"
    12  	"github.com/aristanetworks/glog"
    13  )
    14  
    15  const (
    16  	outOfBrokersBackoff = 30 * time.Second
    17  	outOfBrokersRetries = 5
    18  )
    19  
    20  // NewClient returns a Kafka client
    21  func NewClient(addresses []string) (sarama.Client, error) {
    22  	config := sarama.NewConfig()
    23  	hostname, err := os.Hostname()
    24  	if err != nil {
    25  		hostname = ""
    26  	}
    27  	config.ClientID = hostname
    28  	config.Producer.Compression = sarama.CompressionSnappy
    29  	config.Producer.Return.Successes = true
    30  
    31  	var client sarama.Client
    32  	retries := outOfBrokersRetries + 1
    33  	for retries > 0 {
    34  		client, err = sarama.NewClient(addresses, config)
    35  		retries--
    36  		if err == sarama.ErrOutOfBrokers {
    37  			glog.Errorf("Can't connect to the Kafka cluster at %s (%d retries left): %s",
    38  				addresses, retries, err)
    39  			time.Sleep(outOfBrokersBackoff)
    40  		} else {
    41  			break
    42  		}
    43  	}
    44  	return client, err
    45  }