2017-03-06 23:18:59 +03:00
|
|
|
package endpoint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-11-15 13:59:08 +03:00
|
|
|
"github.com/tidwall/gjson"
|
2017-03-06 23:18:59 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Shopify/sarama"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
kafkaExpiresAfter = time.Second * 30
|
|
|
|
)
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
// KafkaConn is an endpoint connection
|
|
|
|
type KafkaConn struct {
|
2017-03-06 23:18:59 +03:00
|
|
|
mu sync.Mutex
|
|
|
|
ep Endpoint
|
|
|
|
conn sarama.SyncProducer
|
|
|
|
ex bool
|
|
|
|
t time.Time
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
// Expired returns true if the connection has expired
|
|
|
|
func (conn *KafkaConn) Expired() bool {
|
2017-03-06 23:18:59 +03:00
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
|
|
|
if !conn.ex {
|
|
|
|
if time.Now().Sub(conn.t) > kafkaExpiresAfter {
|
|
|
|
if conn.conn != nil {
|
|
|
|
conn.close()
|
|
|
|
}
|
|
|
|
conn.ex = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return conn.ex
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
func (conn *KafkaConn) close() {
|
2017-03-06 23:18:59 +03:00
|
|
|
if conn.conn != nil {
|
|
|
|
conn.conn.Close()
|
|
|
|
conn.conn = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
// Send sends a message
|
|
|
|
func (conn *KafkaConn) Send(msg string) error {
|
2017-03-06 23:18:59 +03:00
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
|
|
|
|
|
|
|
if conn.ex {
|
|
|
|
return errExpired
|
|
|
|
}
|
|
|
|
conn.t = time.Now()
|
|
|
|
|
|
|
|
uri := fmt.Sprintf("%s:%d", conn.ep.Kafka.Host, conn.ep.Kafka.Port)
|
|
|
|
if conn.conn == nil {
|
2018-04-17 18:30:49 +03:00
|
|
|
cfg := sarama.NewConfig()
|
|
|
|
cfg.Net.DialTimeout = time.Second
|
|
|
|
cfg.Net.ReadTimeout = time.Second * 5
|
|
|
|
cfg.Net.WriteTimeout = time.Second * 5
|
2018-07-03 15:27:01 +03:00
|
|
|
// Fix #333 : fix backward incompatibility introduced by sarama library
|
|
|
|
cfg.Producer.Return.Successes = true
|
|
|
|
|
2018-04-17 18:30:49 +03:00
|
|
|
c, err := sarama.NewSyncProducer([]string{uri}, cfg)
|
2017-03-06 23:18:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.conn = c
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:59:08 +03:00
|
|
|
// parse json again to get out info for our kafka key
|
|
|
|
key := gjson.Get(msg, "key")
|
|
|
|
id := gjson.Get(msg, "id")
|
|
|
|
keyValue := fmt.Sprintf("%s-%s", key.String(), id.String())
|
|
|
|
|
2017-03-06 23:18:59 +03:00
|
|
|
message := &sarama.ProducerMessage{
|
2018-11-15 13:59:08 +03:00
|
|
|
Topic: conn.ep.Kafka.TopicName,
|
|
|
|
Key: sarama.StringEncoder(keyValue),
|
2017-03-06 23:18:59 +03:00
|
|
|
Value: sarama.StringEncoder(msg),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, offset, err := conn.conn.SendMessage(message)
|
|
|
|
if err != nil {
|
|
|
|
conn.close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if offset < 0 {
|
|
|
|
conn.close()
|
|
|
|
return errors.New("invalid kafka reply")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:25:39 +03:00
|
|
|
func newKafkaConn(ep Endpoint) *KafkaConn {
|
|
|
|
return &KafkaConn{
|
2017-03-06 23:18:59 +03:00
|
|
|
ep: ep,
|
|
|
|
t: time.Now(),
|
|
|
|
}
|
|
|
|
}
|