NOISSUE - MQTT To Publish Payload Only (#1903)

* Publish only message payload

Signed-off-by: Rodney Osodo <socials@rodneyosodo.com>

* Use concatenation instead of sprintf as documented

Signed-off-by: Rodney Osodo <socials@rodneyosodo.com>

* Fix tests

Signed-off-by: Rodney Osodo <socials@rodneyosodo.com>

---------

Signed-off-by: Rodney Osodo <socials@rodneyosodo.com>
This commit is contained in:
b1ackd0t 2023-09-05 16:01:24 +03:00 committed by GitHub
parent 7c3add6949
commit a008440dcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 15 deletions

View File

@ -43,15 +43,17 @@ func handle(ctx context.Context, pub messaging.Publisher, logger mflog.Logger) h
} }
// Use concatenation instead of fmt.Sprintf for the // Use concatenation instead of fmt.Sprintf for the
// sake of simplicity and performance. // sake of simplicity and performance.
topic := fmt.Sprintf("channels/%s/messages", msg.Channel) topic := "channels/" + msg.Channel + "/messages"
if msg.Subtopic != "" { if msg.Subtopic != "" {
topic = fmt.Sprintf("%s/%s", topic, strings.ReplaceAll(msg.Subtopic, ".", "/")) topic = topic + "/" + strings.ReplaceAll(msg.Subtopic, ".", "/")
} }
go func() { go func() {
if err := pub.Publish(ctx, topic, msg); err != nil { if err := pub.Publish(ctx, topic, msg); err != nil {
logger.Warn(fmt.Sprintf("Failed to forward message: %s", err)) logger.Warn(fmt.Sprintf("Failed to forward message: %s", err))
} }
}() }()
return nil return nil
} }
} }

View File

@ -10,7 +10,6 @@ import (
mqtt "github.com/eclipse/paho.mqtt.golang" mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/mainflux/mainflux/pkg/messaging" "github.com/mainflux/mainflux/pkg/messaging"
"google.golang.org/protobuf/proto"
) )
var errPublishTimeout = errors.New("failed to publish due to timeout reached") var errPublishTimeout = errors.New("failed to publish due to timeout reached")
@ -40,20 +39,18 @@ func (pub publisher) Publish(ctx context.Context, topic string, msg *messaging.M
if topic == "" { if topic == "" {
return ErrEmptyTopic return ErrEmptyTopic
} }
data, err := proto.Marshal(msg)
if err != nil { // Publish only the payload and not the whole message.
return err token := pub.client.Publish(topic, qos, false, msg.GetPayload())
}
token := pub.client.Publish(topic, qos, false, data)
if token.Error() != nil { if token.Error() != nil {
return token.Error() return token.Error()
} }
ok := token.WaitTimeout(pub.timeout)
if !ok { if ok := token.WaitTimeout(pub.timeout); !ok {
return errPublishTimeout return errPublishTimeout
} }
return token.Error() return nil
} }
func (pub publisher) Close() error { func (pub publisher) Close() error {

View File

@ -62,7 +62,7 @@ type pubsub struct {
} }
// NewPubSub returns MQTT message publisher/subscriber. // NewPubSub returns MQTT message publisher/subscriber.
func NewPubSub(url, queue string, timeout time.Duration, logger mflog.Logger) (messaging.PubSub, error) { func NewPubSub(url, _ string, timeout time.Duration, logger mflog.Logger) (messaging.PubSub, error) {
client, err := newClient(url, "mqtt-publisher", timeout) client, err := newClient(url, "mqtt-publisher", timeout)
if err != nil { if err != nil {
return nil, err return nil, err
@ -194,7 +194,7 @@ func newClient(address, id string, timeout time.Duration) (mqtt.Client, error) {
} }
func (ps *pubsub) mqttHandler(h messaging.MessageHandler) mqtt.MessageHandler { func (ps *pubsub) mqttHandler(h messaging.MessageHandler) mqtt.MessageHandler {
return func(c mqtt.Client, m mqtt.Message) { return func(_ mqtt.Client, m mqtt.Message) {
var msg messaging.Message var msg messaging.Message
if err := proto.Unmarshal(m.Payload(), &msg); err != nil { if err := proto.Unmarshal(m.Payload(), &msg); err != nil {
ps.logger.Warn(fmt.Sprintf("Failed to unmarshal received message: %s", err)) ps.logger.Warn(fmt.Sprintf("Failed to unmarshal received message: %s", err))

View File

@ -111,7 +111,9 @@ func TestPublisher(t *testing.T) {
assert.Nil(t, err, fmt.Sprintf("%s: failed to serialize protobuf error: %s\n", tc.desc, err)) assert.Nil(t, err, fmt.Sprintf("%s: failed to serialize protobuf error: %s\n", tc.desc, err))
receivedMsg := <-msgChan receivedMsg := <-msgChan
assert.Equal(t, data, receivedMsg, fmt.Sprintf("%s: expected %+v got %+v\n", tc.desc, data, receivedMsg)) if tc.payload != nil {
assert.Equal(t, expectedMsg.GetPayload(), receivedMsg, fmt.Sprintf("%s: expected %+v got %+v\n", tc.desc, data, receivedMsg))
}
} }
} }
@ -271,9 +273,14 @@ func TestPubSub(t *testing.T) {
Subtopic: subtopic, Subtopic: subtopic,
Payload: data, Payload: data,
} }
data, err := proto.Marshal(&expectedMsg)
assert.Nil(t, err, fmt.Sprintf("%s: failed to serialize protobuf error: %s\n", tc.desc, err))
msg := messaging.Message{
Payload: data,
}
// Publish message, and then receive it on message channel. // Publish message, and then receive it on message channel.
err := pubsub.Publish(context.TODO(), topic, &expectedMsg) err = pubsub.Publish(context.TODO(), topic, &msg)
assert.Nil(t, err, fmt.Sprintf("%s: got unexpected error: %s\n", tc.desc, err)) assert.Nil(t, err, fmt.Sprintf("%s: got unexpected error: %s\n", tc.desc, err))
receivedMsg := <-msgChan receivedMsg := <-msgChan

View File

@ -161,6 +161,7 @@ func (ps *pubsub) natsHandler(h messaging.MessageHandler) broker.MsgHandler {
ps.logger.Warn(fmt.Sprintf("Failed to unmarshal received message: %s", err)) ps.logger.Warn(fmt.Sprintf("Failed to unmarshal received message: %s", err))
return return
} }
if err := h.Handle(&msg); err != nil { if err := h.Handle(&msg); err != nil {
ps.logger.Warn(fmt.Sprintf("Failed to handle Mainflux message: %s", err)) ps.logger.Warn(fmt.Sprintf("Failed to handle Mainflux message: %s", err))
} }