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

97 lines
2.6 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package tracing
import (
"context"
"github.com/mainflux/mainflux/internal/server"
"github.com/mainflux/mainflux/pkg/messaging"
"github.com/mainflux/mainflux/pkg/messaging/tracing"
"go.opentelemetry.io/otel/trace"
)
// Constants to define different operations to be traced.
const (
subscribeOP = "receive"
unsubscribeOp = "unsubscribe" // This is not specified in the open telemetry spec.
processOp = "process"
)
var _ messaging.PubSub = (*pubsubMiddleware)(nil)
type pubsubMiddleware struct {
publisherMiddleware
pubsub messaging.PubSub
host server.Config
}
// NewPubSub creates a new pubsub middleware that traces pubsub operations.
func NewPubSub(config server.Config, tracer trace.Tracer, pubsub messaging.PubSub) messaging.PubSub {
pb := &pubsubMiddleware{
publisherMiddleware: publisherMiddleware{
publisher: pubsub,
tracer: tracer,
host: config,
},
pubsub: pubsub,
host: config,
}
return pb
}
// Subscribe creates a new subscription and traces the operation.
func (pm *pubsubMiddleware) Subscribe(ctx context.Context, cfg messaging.SubscriberConfig) error {
ctx, span := tracing.CreateSpan(ctx, subscribeOP, cfg.ID, cfg.Topic, "", 0, pm.host, trace.SpanKindClient, pm.tracer)
defer span.End()
span.SetAttributes(defaultAttributes...)
cfg.Handler = &traceHandler{
ctx: ctx,
handler: cfg.Handler,
tracer: pm.tracer,
host: pm.host,
topic: cfg.Topic,
clientID: cfg.ID,
}
return pm.pubsub.Subscribe(ctx, cfg)
}
// Unsubscribe removes an existing subscription and traces the operation.
func (pm *pubsubMiddleware) Unsubscribe(ctx context.Context, id, topic string) error {
ctx, span := tracing.CreateSpan(ctx, unsubscribeOp, id, topic, "", 0, pm.host, trace.SpanKindInternal, pm.tracer)
defer span.End()
span.SetAttributes(defaultAttributes...)
return pm.pubsub.Unsubscribe(ctx, id, topic)
}
// TraceHandler is used to trace the message handling operation.
type traceHandler struct {
ctx context.Context
handler messaging.MessageHandler
tracer trace.Tracer
host server.Config
topic string
clientID string
}
// Handle instruments the message handling operation.
func (h *traceHandler) Handle(msg *messaging.Message) error {
_, span := tracing.CreateSpan(h.ctx, processOp, h.clientID, h.topic, msg.Subtopic, len(msg.Payload), h.host, trace.SpanKindConsumer, h.tracer)
defer span.End()
span.SetAttributes(defaultAttributes...)
return h.handler.Handle(msg)
}
// Cancel cancels the message handling operation.
func (h *traceHandler) Cancel() error {
return h.handler.Cancel()
}