github.com/teng231/kafclient@v1.2.9/type.go (about)

     1  package kafclient
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  	"sync"
     7  
     8  	"github.com/Shopify/sarama"
     9  )
    10  
    11  func init() {
    12  	log.SetFlags(log.Lshortfile)
    13  	if kafkaVersion == "" {
    14  		kafkaVersion = "2.5.0"
    15  	}
    16  }
    17  
    18  var (
    19  	NUM_PARTITION      = 3
    20  	REPLICATION_FACTOR = 1
    21  	kafkaVersion       = os.Getenv("KAFKA_VERSION")
    22  )
    23  
    24  // SenderConfig addion config when publish message
    25  type SenderConfig struct {
    26  	Metadata interface{}
    27  	Headers  map[string]string
    28  }
    29  
    30  type Topic struct {
    31  	Name                    string
    32  	AutoCommit              bool
    33  	Partition               *int32
    34  	IsNeedManualCreateTopic bool
    35  }
    36  
    37  // type KafClientConfig struct {
    38  // 	KafkaVersion           string // default : 2.5.0
    39  // 	BrokerURLs             []string
    40  // 	KafkaNumerberPartition int // default: using 3 partitions
    41  // 	KafkaReplicationFactor int // default: -1
    42  // }
    43  
    44  type Client struct {
    45  	brokerURLs    []string
    46  	mProducer     sync.Map
    47  	group         sarama.ConsumerGroup
    48  	consumer      sarama.Consumer // for using consumer mode
    49  	kafkaVersion  sarama.KafkaVersion
    50  	reconnect     chan bool
    51  	consumerGroup string
    52  }
    53  
    54  type IClient interface {
    55  	InitConsumerGroup(consumerGroup string, brokerURLs ...string) error
    56  	// InitConsumer depredicated
    57  	InitConsumer(brokerURLs ...string) error
    58  	InitPublisher(brokerURLs ...string) // backward compatible
    59  	// Publish send multiple messages to topic
    60  	Publish(topic string, messages ...interface{}) error
    61  	// OnScanMessages depredicated
    62  	OnScanMessages(topics []string, bufMessage chan Message) error
    63  	// ListTopics for ping
    64  	ListTopics(brokers ...string) ([]string, error)
    65  	// OnAsyncSubscribe subscribe message from list topics,
    66  	// numberPuller is number worker goroutines for pull message from kafka server to message chan
    67  	OnAsyncSubscribe(topics []*Topic, numberPuller int, buf chan Message) error
    68  	// PublishWithConfig help we can publish message to 1 partition.
    69  	// help application process task synchronized
    70  	PublishWithConfig(topic *Topic, config *SenderConfig, messages ...interface{}) error
    71  	Close() error
    72  }
    73  
    74  // Message define message encode/decode sarama message
    75  type Message struct {
    76  	Offset        int64  `json:"offset,omitempty"`
    77  	Partition     int    `json:"partition,omitempty"`
    78  	Topic         string `json:"topic,omitempty"`
    79  	Body          []byte `json:"body,omitempty"`
    80  	Timestamp     int64  `json:"timestamp,omitempty"`
    81  	ConsumerGroup string `json:"consumer_group,omitempty"`
    82  	Commit        func()
    83  	Headers       map[string]string
    84  }
    85  
    86  // ConsumerGroupHandle represents a Sarama consumer group consumer
    87  type ConsumerGroupHandle struct {
    88  	wg         *sync.WaitGroup
    89  	lock       chan bool
    90  	bufMessage chan Message
    91  	autoCommit map[string]bool
    92  }
    93  
    94  func ToInt32(in *int32) int32 {
    95  	return *in
    96  }
    97  
    98  func ToPInt32(in int32) *int32 {
    99  	return &in
   100  }