Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flexibility in subject matching for nats_jetstream #1084

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion protocol/nats_jetstream/v2/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package nats_jetstream
import (
"context"
"io"
"strings"
"sync"

"github.com/nats-io/nats.go"
Expand Down Expand Up @@ -89,10 +90,21 @@ func NewConsumerFromConn(conn *nats.Conn, stream, subject string, jsmOpts []nats

streamInfo, err := jsm.StreamInfo(stream, jsmOpts...)

subjectMatch := stream + ".*"
if strings.Count(strings.TrimPrefix(subject, stream), ".") > 1 {
// More than one "." in the remainder of subject, use ".>" to match
subjectMatch = stream + ".>"
}
if !strings.HasPrefix(subject, stream) {
// Use an empty subject parameter in conjunction with
// nats.ConsumerFilterSubjects
subjectMatch = ""
}

if streamInfo == nil || err != nil && err.Error() == "stream not found" {
_, err = jsm.AddStream(&nats.StreamConfig{
Name: stream,
Subjects: []string{stream + ".*"},
Subjects: []string{subjectMatch},
})
if err != nil {
return nil, err
Expand Down