1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-24 13:48:49 +08:00
b1ackd0t 4401e79a0d
NOISSUE - Add Subscriber Config (#1896)
* Replace Nats with Nats Jestream For PubSub

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Add Stream Description

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Fix connection leak in NATS publisher

The publisher struct in pkg/messaging/nats/publisher.go was modified to include a new `conn` field of type `*broker.Conn`. This change was made to fix a connection leak issue in the NATS publisher.

The `NewPublisher` function was updated to assign the `conn` parameter to the new `conn` field in the publisher struct.

Additionally, the `Close` method in the publisher struct was modified to close the `conn` connection.

This commit fixes the connection leak issue in the NATS publisher and ensures that connections are properly closed.

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

* Setup subscriber config to contain handler topic and ID

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Add delivery policy

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Avoid duplicate messages

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Rename to DeliveryPolicy

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Fix tests

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Not check for data result set when we are returning subset of messages

Signed-off-by: Rodney Osodo <socials@rodneyosodo.com>
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>

* For unsubscribe remove config

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>

* Fix comment

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

---------

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: Rodney Osodo <socials@rodneyosodo.com>
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
2023-10-23 15:27:15 +02:00

85 lines
1.9 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package mongodb
import (
"context"
"github.com/mainflux/mainflux/consumers"
"github.com/mainflux/mainflux/pkg/errors"
"github.com/mainflux/mainflux/pkg/transformers/json"
"github.com/mainflux/mainflux/pkg/transformers/senml"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
const senmlCollection string = "messages"
var errSaveMessage = errors.New("failed to save message to mongodb database")
var _ consumers.BlockingConsumer = (*mongoRepo)(nil)
type mongoRepo struct {
db *mongo.Database
}
// New returns new MongoDB writer.
func New(db *mongo.Database) consumers.BlockingConsumer {
return &mongoRepo{db}
}
func (repo *mongoRepo) ConsumeBlocking(ctx context.Context, message interface{}) error {
switch m := message.(type) {
case json.Messages:
return repo.saveJSON(ctx, m)
default:
return repo.saveSenml(ctx, m)
}
}
func (repo *mongoRepo) saveSenml(ctx context.Context, messages interface{}) error {
msgs, ok := messages.([]senml.Message)
if !ok {
return errSaveMessage
}
coll := repo.db.Collection(senmlCollection)
var dbMsgs []interface{}
for _, msg := range msgs {
// Check if message is already in database.
filter := bson.M{"time": msg.Time, "publisher": msg.Publisher, "subtopic": msg.Subtopic, "name": msg.Name}
count, err := coll.CountDocuments(ctx, filter)
if err != nil {
return errors.Wrap(errSaveMessage, err)
}
if count == 0 {
dbMsgs = append(dbMsgs, msg)
}
}
_, err := coll.InsertMany(ctx, dbMsgs)
if err != nil {
return errors.Wrap(errSaveMessage, err)
}
return nil
}
func (repo *mongoRepo) saveJSON(ctx context.Context, msgs json.Messages) error {
m := []interface{}{}
for _, msg := range msgs.Data {
m = append(m, msg)
}
coll := repo.db.Collection(msgs.Format)
_, err := coll.InsertMany(ctx, m)
if err != nil {
return errors.Wrap(errSaveMessage, err)
}
return nil
}