2018-05-11 07:46:06 +00:00
|
|
|
package kafka
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.loafle.net/commons/logging-go"
|
|
|
|
occe "git.loafle.net/overflow/commons-go/config/external"
|
|
|
|
"github.com/segmentio/kafka-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-05-11 10:51:52 +00:00
|
|
|
writers map[string]*kafka.Writer
|
|
|
|
readers map[string]*kafka.Reader
|
2018-05-11 07:46:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func InitPackage(config *occe.Kafka) {
|
|
|
|
if nil == config {
|
|
|
|
return
|
|
|
|
}
|
2018-05-11 08:53:55 +00:00
|
|
|
|
2018-05-11 09:56:33 +00:00
|
|
|
if nil != config.Producers && 0 < len(config.Producers) {
|
2018-05-11 10:51:52 +00:00
|
|
|
writers = make(map[string]*kafka.Writer)
|
2018-05-11 08:53:55 +00:00
|
|
|
for n, c := range config.Producers {
|
|
|
|
wc := kafka.WriterConfig{
|
|
|
|
Brokers: c.Brokers,
|
|
|
|
Topic: c.Topic,
|
|
|
|
Balancer: &kafka.LeastBytes{},
|
|
|
|
MaxAttempts: c.MaxAttempts,
|
|
|
|
QueueCapacity: c.QueueCapacity,
|
|
|
|
BatchSize: c.BatchSize,
|
|
|
|
BatchTimeout: c.BatchTimeout,
|
|
|
|
ReadTimeout: c.ReadTimeout,
|
|
|
|
WriteTimeout: c.WriteTimeout,
|
|
|
|
RebalanceInterval: c.RebalanceInterval,
|
|
|
|
RequiredAcks: c.RequiredAcks,
|
|
|
|
Async: c.Async,
|
|
|
|
}
|
2018-05-11 10:51:52 +00:00
|
|
|
writers[n] = kafka.NewWriter(wc)
|
2018-05-11 08:53:55 +00:00
|
|
|
}
|
2018-05-11 07:46:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 09:56:33 +00:00
|
|
|
if nil != config.Consumers && 0 < len(config.Consumers) {
|
2018-05-11 10:51:52 +00:00
|
|
|
readers = make(map[string]*kafka.Reader)
|
2018-05-11 08:53:55 +00:00
|
|
|
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,
|
|
|
|
}
|
2018-05-11 10:51:52 +00:00
|
|
|
readers[n] = kafka.NewReader(rc)
|
2018-05-11 07:46:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func StartPackage(config *occe.Kafka) {
|
|
|
|
if nil == config {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func StopPackage(config *occe.Kafka) {
|
|
|
|
if nil == config {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func DestroyPackage(config *occe.Kafka) {
|
|
|
|
if nil == config {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-11 10:51:52 +00:00
|
|
|
for _, w := range writers {
|
2018-05-11 07:46:06 +00:00
|
|
|
if err := w.Close(); nil != err {
|
|
|
|
logging.Logger().Errorf("%v", err)
|
|
|
|
}
|
|
|
|
}
|
2018-05-11 09:56:33 +00:00
|
|
|
|
2018-05-11 10:51:52 +00:00
|
|
|
for _, r := range readers {
|
2018-05-11 09:56:33 +00:00
|
|
|
if err := r.Close(); nil != err {
|
|
|
|
logging.Logger().Errorf("%v", err)
|
|
|
|
}
|
|
|
|
}
|
2018-05-11 07:46:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 08:53:55 +00:00
|
|
|
func Writer(name string) *kafka.Writer {
|
2018-05-11 10:51:52 +00:00
|
|
|
if nil == writers {
|
2018-05-11 08:53:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-05-11 10:51:52 +00:00
|
|
|
return writers[name]
|
2018-05-11 08:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Reader(name string) *kafka.Reader {
|
2018-05-11 10:51:52 +00:00
|
|
|
if nil == readers {
|
2018-05-11 08:53:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-05-11 10:51:52 +00:00
|
|
|
return readers[name]
|
2018-05-11 08:53:55 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 07:46:06 +00:00
|
|
|
func Write(name string, key []byte, value []byte) error {
|
2018-05-11 10:51:52 +00:00
|
|
|
if nil == writers {
|
2018-05-11 07:46:06 +00:00
|
|
|
return fmt.Errorf("Kafka client is not valid")
|
|
|
|
}
|
|
|
|
|
2018-05-11 10:51:52 +00:00
|
|
|
w, ok := writers[name]
|
2018-05-11 07:46:06 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Kafka client is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := w.WriteMessages(context.Background(),
|
|
|
|
kafka.Message{
|
|
|
|
Key: key,
|
|
|
|
Value: value,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|