Skip to content

Commit

Permalink
avoid creating nats stream if stream is passed as empty
Browse files Browse the repository at this point in the history
Signed-off-by: stephen-totty-hpe <[email protected]>
  • Loading branch information
stephen-totty-hpe committed Jul 9, 2024
1 parent 8efefb0 commit 3f4717e
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions protocol/nats_jetstream/v2/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down

0 comments on commit 3f4717e

Please sign in to comment.