update kafka plugins

This commit is contained in:
joy.zhou committed 2019-09-18 14:00:19 +08:00
1 parent 80b64b147e
commit 55576c1eb3
2 files changed
+46 -18

No files matched your search

+42 -13
View File
@@ -4,7 +4,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil" "io/ioutil"
"regexp" "strings"
"github.com/Shopify/sarama" "github.com/Shopify/sarama"
"go.uber.org/zap" "go.uber.org/zap"
@@ -17,7 +17,7 @@ type kafakConfig struct {
PublishTopic string `json:"onPublish"` PublishTopic string `json:"onPublish"`
UnsubscribeTopic string `json:"onUnsubscribe"` UnsubscribeTopic string `json:"onUnsubscribe"`
DisconnectTopic string `json:"onDisconnect"` DisconnectTopic string `json:"onDisconnect"`
RegexpMap map[string]string `json:"regexpMap"` DeliverMap map[string]string `json:"deliverMap"`
} }
type kafka struct { type kafka struct {
@@ -65,34 +65,34 @@ func (k *kafka) connect() {
func (k *kafka) Publish(e *Elements) error { func (k *kafka) Publish(e *Elements) error {
config := k.kafakConfig config := k.kafakConfig
key := e.ClientID key := e.ClientID
var topics []string var topics map[string]bool
switch e.Action { switch e.Action {
case Connect: case Connect:
if config.ConnectTopic != "" { if config.ConnectTopic != "" {
topics = append(topics, config.ConnectTopic) topics[config.ConnectTopic] = true
} }
case Publish: case Publish:
if config.PublishTopic != "" { if config.PublishTopic != "" {
topics = append(topics, config.PublishTopic) topics[config.PublishTopic] = true
} }
// foreach regexp map config // foreach regexp map config
for reg, topic := range config.RegexpMap { for reg, topic := range config.DeliverMap {
match, _ := regexp.MatchString(reg, e.Topic) match := matchTopic(reg, e.Topic)
if match { if match {
topics = append(topics, topic) topics[topic] = true
} }
} }
case Subscribe: case Subscribe:
if config.SubscribeTopic != "" { if config.SubscribeTopic != "" {
topics = append(topics, config.SubscribeTopic) topics[config.SubscribeTopic] = true
} }
case Unsubscribe: case Unsubscribe:
if config.UnsubscribeTopic != "" { if config.UnsubscribeTopic != "" {
topics = append(topics, config.UnsubscribeTopic) topics[config.UnsubscribeTopic] = true
} }
case Disconnect: case Disconnect:
if config.DisconnectTopic != "" { if config.DisconnectTopic != "" {
topics = append(topics, config.DisconnectTopic) topics[config.DisconnectTopic] = true
} }
default: default:
return errors.New("error action: " + e.Action) return errors.New("error action: " + e.Action)
@@ -102,13 +102,13 @@ func (k *kafka) Publish(e *Elements) error {
} }
func (k *kafka) publish(topics []string, key string, msg *Elements) error { func (k *kafka) publish(topics map[string]bool, key string, msg *Elements) error {
payload, err := json.Marshal(msg) payload, err := json.Marshal(msg)
if err != nil { if err != nil {
return err return err
} }
for _, topic := range topics { for topic, _ := range topics {
k.kafkaClient.Input() <- &sarama.ProducerMessage{ k.kafkaClient.Input() <- &sarama.ProducerMessage{
Topic: topic, Topic: topic,
Key: sarama.ByteEncoder(key), Key: sarama.ByteEncoder(key),
@@ -118,3 +118,32 @@ func (k *kafka) publish(topics []string, key string, msg *Elements) error {
return nil return nil
} }
func match(subTopic []string, topic []string) bool {
if len(subTopic) == 0 {
if len(topic) == 0 {
return true
}
return false
}
if len(topic) == 0 {
if subTopic[0] == "#" {
return true
}
return false
}
if subTopic[0] == "#" {
return true
}
if (subTopic[0] == "+") || (subTopic[0] == topic[0]) {
return match(subTopic[1:], topic[1:])
}
return false
}
func matchTopic(subTopic string, topic string) bool {
return match(strings.Split(subTopic, "/"), strings.Split(topic, "/"))
}
+4 -5
View File
@@ -7,9 +7,8 @@
"onSubscribe": "onSubscribe", "onSubscribe": "onSubscribe",
"onDisconnect": "onDisconnect", "onDisconnect": "onDisconnect",
"onUnsubscribe": "onUnsubscribe", "onUnsubscribe": "onUnsubscribe",
"regexpMap": [ "deliverMap": {
{ "#": "publish",
"^/(.+)/(.+)/upload/(.*)$": "upload" "/upload/+/#": "upload"
} }
]
} }