This commit is contained in:
crusader 2018-05-11 17:53:55 +09:00
parent d871241375
commit 1742b7fab6

View File

@ -11,18 +11,16 @@ import (
var ( var (
kafkaWriters map[string]*kafka.Writer kafkaWriters map[string]*kafka.Writer
kafkaReaders map[string]*kafka.Reader
) )
func InitPackage(config *occe.Kafka) { func InitPackage(config *occe.Kafka) {
if nil == config { if nil == config {
return return
} }
if nil == config.Producers || 0 == len(config.Producers) {
return
}
if nil != config.Producers || 0 < len(config.Producers) {
kafkaWriters = make(map[string]*kafka.Writer) kafkaWriters = make(map[string]*kafka.Writer)
for n, c := range config.Producers { for n, c := range config.Producers {
wc := kafka.WriterConfig{ wc := kafka.WriterConfig{
Brokers: c.Brokers, Brokers: c.Brokers,
@ -40,6 +38,29 @@ func InitPackage(config *occe.Kafka) {
} }
kafkaWriters[n] = kafka.NewWriter(wc) kafkaWriters[n] = kafka.NewWriter(wc)
} }
}
if nil != config.Consumers || 0 < len(config.Consumers) {
kafkaReaders = make(map[string]*kafka.Reader)
for n, c := range config.Consumers {
rc := kafka.ReaderConfig{
Brokers: c.Brokers,
GroupID: c.GroupID,
Topic: c.Topic,
QueueCapacity: c.QueueCapacity,
MinBytes: c.MaxBytes,
MaxBytes: c.MaxBytes,
MaxWait: c.MaxWait,
ReadLagInterval: c.ReadLagInterval,
HeartbeatInterval: c.HeartbeatInterval,
CommitInterval: c.CommitInterval,
SessionTimeout: c.SessionTimeout,
RebalanceTimeout: c.RebalanceTimeout,
RetentionTime: c.RetentionTime,
}
kafkaReaders[n] = kafka.NewReader(rc)
}
}
} }
func StartPackage(config *occe.Kafka) { func StartPackage(config *occe.Kafka) {
@ -68,6 +89,20 @@ func DestroyPackage(config *occe.Kafka) {
} }
} }
func Writer(name string) *kafka.Writer {
if nil == kafkaWriters {
return nil
}
return kafkaWriters[name]
}
func Reader(name string) *kafka.Reader {
if nil == kafkaReaders {
return nil
}
return kafkaReaders[name]
}
func Write(name string, key []byte, value []byte) error { func Write(name string, key []byte, value []byte) error {
if nil == kafkaWriters { if nil == kafkaWriters {
return fmt.Errorf("Kafka client is not valid") return fmt.Errorf("Kafka client is not valid")