1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-27 13:48:49 +08:00
b1ackd0t 824156fbf0
NOISSUE - Use Nats JetStream As Internal Message Bus (#1893)
* Replace Nats with Nats Jestream For PubSub

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

* Add Stream Description

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

* Subscribe using wildcard

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

* Add consumers description

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

* Remove unused queue variable

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

* Add extra configs to stream

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

* Use inline error handling

Signed-off-by: Rodney Osodo <socials@rodneyosodo.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>

* feat(messaging): Add support for durable consumers

This commit adds support for durable consumers to NATS JS in the messaging package.

To support this functionality, the `strings` package has been imported in the `pubsub.go` file to check the topic.

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

* feat : remove internal logic to keep subscribers

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

* feat(messaging): add function to format consumer name

This commit adds a new function to the `pubsub` package in the `messaging` module. The function is called `formatConsumerName` and is used to generate a consumer name for NATS messaging. The function takes two parameters, `topic` and `id`, and returns a formatted consumer name. The consumer name is generated by concatenating the `topic` and `id` parameters, with some restrictions on the characters that can be used. This function will be useful for creating durable subscriptions in NATS messaging.

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.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-18 17:00:38 +02:00

130 lines
3.2 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package rabbitmq_test
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"testing"
mflog "github.com/mainflux/mainflux/logger"
"github.com/mainflux/mainflux/pkg/messaging"
"github.com/mainflux/mainflux/pkg/messaging/rabbitmq"
"github.com/ory/dockertest/v3"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
)
const (
port = "5672/tcp"
brokerName = "rabbitmq"
brokerVersion = "3.9.20"
)
var (
publisher messaging.Publisher
pubsub messaging.PubSub
logger mflog.Logger
address string
)
func TestMain(m *testing.M) {
pool, err := dockertest.NewPool("")
if err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}
container, err := pool.Run(brokerName, brokerVersion, []string{})
if err != nil {
log.Fatalf("Could not start container: %s", err)
}
handleInterrupt(pool, container)
address = fmt.Sprintf("amqp://%s:%s", "localhost", container.GetPort(port))
if err := pool.Retry(func() error {
publisher, err = rabbitmq.NewPublisher(address)
return err
}); err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}
logger, err = mflog.New(os.Stdout, mflog.Debug.String())
if err != nil {
log.Fatalf(err.Error())
}
if err := pool.Retry(func() error {
pubsub, err = rabbitmq.NewPubSub(address, logger)
return err
}); err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}
code := m.Run()
if err := pool.Purge(container); err != nil {
log.Fatalf("Could not purge container: %s", err)
}
os.Exit(code)
}
func newConn() (*amqp.Connection, *amqp.Channel, error) {
conn, err := amqp.Dial(address)
if err != nil {
return nil, nil, err
}
ch, err := conn.Channel()
if err != nil {
return nil, nil, err
}
if err := ch.ExchangeDeclare(exchangeName, amqp.ExchangeTopic, true, false, false, false, nil); err != nil {
return nil, nil, err
}
return conn, ch, nil
}
func rabbitHandler(deliveries <-chan amqp.Delivery, h messaging.MessageHandler) {
for d := range deliveries {
var msg messaging.Message
if err := proto.Unmarshal(d.Body, &msg); err != nil {
logger.Warn(fmt.Sprintf("Failed to unmarshal received message: %s", err))
return
}
if err := h.Handle(&msg); err != nil {
logger.Warn(fmt.Sprintf("Failed to handle Mainflux message: %s", err))
return
}
}
}
func subscribe(t *testing.T, ch *amqp.Channel, topic string) <-chan amqp.Delivery {
_, err := ch.QueueDeclare(topic, true, true, true, false, nil)
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
err = ch.QueueBind(topic, topic, exchangeName, false, nil)
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
clientID := fmt.Sprintf("%s-%s", topic, clientID)
msgs, err := ch.Consume(topic, clientID, true, false, false, false, nil)
assert.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
return msgs
}
func handleInterrupt(pool *dockertest.Pool, container *dockertest.Resource) {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
if err := pool.Purge(container); err != nil {
log.Fatalf("Could not purge container: %s", err)
}
os.Exit(0)
}()
}