mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-27 13:48:49 +08:00

* 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>
76 lines
1.6 KiB
Go
76 lines
1.6 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package nats
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/mainflux/mainflux/pkg/messaging"
|
|
broker "github.com/nats-io/nats.go"
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// A maximum number of reconnect attempts before NATS connection closes permanently.
|
|
// Value -1 represents an unlimited number of reconnect retries, i.e. the client
|
|
// will never give up on retrying to re-establish connection to NATS server.
|
|
const maxReconnects = -1
|
|
|
|
var _ messaging.Publisher = (*publisher)(nil)
|
|
|
|
type publisher struct {
|
|
js jetstream.JetStream
|
|
conn *broker.Conn
|
|
}
|
|
|
|
// Publisher wraps messaging Publisher exposing
|
|
// Close() method for NATS connection.
|
|
|
|
// NewPublisher returns NATS message Publisher.
|
|
func NewPublisher(ctx context.Context, url string) (messaging.Publisher, error) {
|
|
conn, err := broker.Connect(url, broker.MaxReconnects(maxReconnects))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
js, err := jetstream.New(conn)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err := js.CreateStream(ctx, jsStreamConfig); err != nil {
|
|
return nil, err
|
|
}
|
|
ret := &publisher{
|
|
js: js,
|
|
conn: conn,
|
|
}
|
|
|
|
return ret, nil
|
|
}
|
|
|
|
func (pub *publisher) Publish(ctx context.Context, topic string, msg *messaging.Message) error {
|
|
if topic == "" {
|
|
return ErrEmptyTopic
|
|
}
|
|
|
|
data, err := proto.Marshal(msg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
subject := fmt.Sprintf("%s.%s", chansPrefix, topic)
|
|
if msg.Subtopic != "" {
|
|
subject = fmt.Sprintf("%s.%s", subject, msg.Subtopic)
|
|
}
|
|
|
|
_, err = pub.js.Publish(ctx, subject, data)
|
|
|
|
return err
|
|
}
|
|
|
|
func (pub *publisher) Close() error {
|
|
pub.conn.Close()
|
|
return nil
|
|
}
|