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 15, 2024
1 parent dc3d9b1 commit 58a3880
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions protocol/nats_jetstream/v2/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down

0 comments on commit 58a3880

Please sign in to comment.