mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-24 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> * 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>
86 lines
1.8 KiB
Go
86 lines
1.8 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package mocks
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"github.com/gorilla/websocket"
|
|
"github.com/mainflux/mainflux/pkg/messaging"
|
|
"github.com/mainflux/mainflux/ws"
|
|
)
|
|
|
|
var _ messaging.PubSub = (*mockPubSub)(nil)
|
|
|
|
type MockPubSub interface {
|
|
// Publish publishes message to the channel.
|
|
Publish(context.Context, string, *messaging.Message) error
|
|
|
|
// Subscribe subscribes messages from the channel.
|
|
Subscribe(context.Context, messaging.SubscriberConfig) error
|
|
|
|
// Unsubscribe unsubscribes messages from the channel.
|
|
Unsubscribe(context.Context, string, string) error
|
|
|
|
// SetFail sets the fail flag.
|
|
SetFail(bool)
|
|
|
|
// SetConn sets the connection.
|
|
SetConn(*websocket.Conn)
|
|
|
|
// Close closes the connection.
|
|
Close() error
|
|
}
|
|
|
|
type mockPubSub struct {
|
|
fail bool
|
|
conn *websocket.Conn
|
|
}
|
|
|
|
// NewPubSub returns mock message publisher-subscriber.
|
|
func NewPubSub() MockPubSub {
|
|
return &mockPubSub{false, nil}
|
|
}
|
|
|
|
func (pubsub *mockPubSub) Publish(ctx context.Context, s string, msg *messaging.Message) error {
|
|
if pubsub.conn != nil {
|
|
data, err := json.Marshal(msg)
|
|
if err != nil {
|
|
return ws.ErrFailedMessagePublish
|
|
}
|
|
return pubsub.conn.WriteMessage(websocket.BinaryMessage, data)
|
|
}
|
|
if pubsub.fail {
|
|
return ws.ErrFailedMessagePublish
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (pubsub *mockPubSub) Subscribe(context.Context, messaging.SubscriberConfig) error {
|
|
if pubsub.fail {
|
|
return ws.ErrFailedSubscription
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (pubsub *mockPubSub) Unsubscribe(context.Context, string, string) error {
|
|
if pubsub.fail {
|
|
return ws.ErrFailedUnsubscribe
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (pubsub *mockPubSub) SetFail(fail bool) {
|
|
pubsub.fail = fail
|
|
}
|
|
|
|
func (pubsub *mockPubSub) SetConn(c *websocket.Conn) {
|
|
pubsub.conn = c
|
|
}
|
|
|
|
func (pubsub *mockPubSub) Close() error {
|
|
return nil
|
|
}
|