Skip to content

Add replay policy option for stream sub #1286

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 12 additions & 2 deletions cli/sub_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type subCmd struct {
sseq uint64
deliverAll bool
deliverNew bool
replayPolicy string
reportSubjects bool
reportSubjectsCount int
reportSub bool
Expand Down Expand Up @@ -121,7 +122,8 @@ func configureSubCommand(app commandHost) {
act.Flag("last", "Delivers the most recent and all future messages (requires JetStream)").UnNegatableBoolVar(&c.deliverLast)
act.Flag("since", "Delivers messages received since a duration like 1d3h5m2s(requires JetStream)").PlaceHolder("DURATION").StringVar(&c.deliverSince)
act.Flag("last-per-subject", "Deliver the most recent messages for each subject in the Stream (requires JetStream)").UnNegatableBoolVar(&c.deliverLastPerSubject)
act.Flag("stream", "Subscribe to a specific stream (required JetStream)").PlaceHolder("STREAM").StringVar(&c.stream)
act.Flag("replay", "Replay Policy (instant, original) (requires Jetstream)").PlaceHolder("POLICY").EnumVar(&c.replayPolicy, "instant", "original")
act.Flag("stream", "Subscribe to a specific stream (requires JetStream)").PlaceHolder("STREAM").StringVar(&c.stream)
act.Flag("ignore-subject", "Subjects for which corresponding messages will be ignored and therefore not shown in the output").Short('I').PlaceHolder("SUBJECT").StringsVar(&c.ignoreSubjects)
act.Flag("wait", "Unsubscribe after this amount of time without any traffic").DurationVar(&c.wait)
act.Flag("report-subjects", "Subscribes to subject patterns and builds a de-duplicated report of active subjects receiving data").UnNegatableBoolVar(&c.reportSubjects)
Expand Down Expand Up @@ -277,7 +279,7 @@ func (c *subCmd) subscribe(p *fisk.ParseContext) error {
}
defer nc.Close()

c.jetStream = c.sseq > 0 || len(c.durable) > 0 || c.deliverAll || c.deliverNew || c.deliverLast || c.deliverSince != "" || c.deliverLastPerSubject || c.stream != ""
c.jetStream = c.sseq > 0 || len(c.durable) > 0 || c.deliverAll || c.deliverNew || c.deliverLast || c.deliverSince != "" || c.deliverLastPerSubject || c.replayPolicy != "" || c.stream != ""

switch {
case len(c.subjects) == 0 && c.inbox:
Expand Down Expand Up @@ -615,6 +617,14 @@ func (c *subCmd) subscribe(p *fisk.ParseContext) error {
opts = append(opts, nats.DeliverLastPerSubject())
}

if c.replayPolicy != "" {
if c.replayPolicy == "instant" {
opts = append(opts, nats.ReplayInstant())
} else if c.replayPolicy == "original" {
opts = append(opts, nats.ReplayOriginal())
}
}

if bindDurable {
sub, err := js.Subscribe(c.firstSubject(), handler, nats.Bind(c.stream, c.durable))
if err != nil {
Expand Down