From 58a38800d38dd838c1a55943645e076c214234fd Mon Sep 17 00:00:00 2001 From: stephen-totty-hpe Date: Tue, 9 Jul 2024 10:07:18 -0400 Subject: [PATCH] avoid creating nats stream if stream is passed as empty Signed-off-by: stephen-totty-hpe --- protocol/nats_jetstream/v2/sender.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/protocol/nats_jetstream/v2/sender.go b/protocol/nats_jetstream/v2/sender.go index 2ed7fa2d2..34f79ce54 100644 --- a/protocol/nats_jetstream/v2/sender.go +++ b/protocol/nats_jetstream/v2/sender.go @@ -44,21 +44,28 @@ func NewSender(url, stream, subject string, natsOpts []nats.Option, jsmOpts []na // NewSenderFromConn creates a new protocol.Sender which leaves responsibility for opening and closing the NATS // connection to the caller +// The stream parameter is only needed if auto-creation of the stream is needed when the stream does not exist. func NewSenderFromConn(conn *nats.Conn, stream, subject string, jsmOpts []nats.JSOpt, opts ...SenderOption) (*Sender, error) { jsm, err := conn.JetStream(jsmOpts...) if err != nil { return nil, err } - streamInfo, err := jsm.StreamInfo(stream, jsmOpts...) - - if streamInfo == nil || err != nil && err.Error() == "stream not found" { - _, err = jsm.AddStream(&nats.StreamConfig{ - Name: stream, - Subjects: []string{stream + ".*"}, - }) - if err != nil { - return nil, err + // A Stream parameter is not needed for a send operation. + // A subject is all that is needed. + // Below, a stream is created with default stream config which may not be desired. + // It may be that the intention is for the call to fail if a stream does not exist. + if stream != "" { + streamInfo, err := jsm.StreamInfo(stream, jsmOpts...) + + if streamInfo == nil || err != nil && err.Error() == "stream not found" { + _, err = jsm.AddStream(&nats.StreamConfig{ + Name: stream, + Subjects: []string{stream + ".*"}, + }) + if err != nil { + return nil, err + } } }