diff --git a/example_test.go b/example_test.go index fc7a84e8a..08616a73d 100644 --- a/example_test.go +++ b/example_test.go @@ -620,6 +620,11 @@ func ExampleSubOpt() { js.Subscribe("foo", func(msg *nats.Msg) { fmt.Printf("Received a message: %s\n", string(msg.Data)) }, nats.StartTime(time.Now().Add(-2*time.Hour))) + + // Start delivering messages with delay based on BackOff array of time durations. + js.Subscribe("foo", func(msg *nats.Msg) { + fmt.Printf("Received a message: %s\n", string(msg.Data)) + }, nats.ManualAck(), nats.MaxDeliver(2), nats.BackOff([]time.Duration{50 * time.Millisecond, 250 * time.Millisecond})) } func ExampleMaxWait() { diff --git a/js.go b/js.go index d526978b2..af323b424 100644 --- a/js.go +++ b/js.go @@ -2237,6 +2237,14 @@ func RateLimit(n uint64) SubOpt { }) } +// BackOff is an array of time durations that represent the time to delay based on delivery count. +func BackOff(backOff []time.Duration) SubOpt { + return subOptFn(func(opts *subOpts) error { + opts.cfg.BackOff = backOff + return nil + }) +} + // BindStream binds a consumer to a stream explicitly based on a name. // When a stream name is not specified, the library uses the subscribe // subject as a way to find the stream name. It is done by making a request