From 3f4717e07ed78620289e07ce43853b8c6c9b8b29 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 | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/protocol/nats_jetstream/v2/sender.go b/protocol/nats_jetstream/v2/sender.go index 2ed7fa2d2..c69f3cb95 100644 --- a/protocol/nats_jetstream/v2/sender.go +++ b/protocol/nats_jetstream/v2/sender.go @@ -50,15 +50,21 @@ func NewSenderFromConn(conn *nats.Conn, stream, subject string, jsmOpts []nats.J 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 + } } }