-
Notifications
You must be signed in to change notification settings - Fork 27
/
consumers.go
987 lines (821 loc) · 28.5 KB
/
consumers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
// Copyright 2020 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package jsm
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"sync"
"time"
"github.com/nats-io/jsm.go/api"
"github.com/nats-io/nats.go"
"github.com/nats-io/nuid"
)
// DefaultConsumer is the configuration that will be used to create new Consumers in NewConsumer
var DefaultConsumer = api.ConsumerConfig{
DeliverPolicy: api.DeliverAll,
AckPolicy: api.AckExplicit,
AckWait: 30 * time.Second,
ReplayPolicy: api.ReplayInstant,
}
// SampledDefaultConsumer is the configuration that will be used to create new Consumers in NewConsumer
var SampledDefaultConsumer = api.ConsumerConfig{
DeliverPolicy: api.DeliverAll,
AckPolicy: api.AckExplicit,
AckWait: 30 * time.Second,
ReplayPolicy: api.ReplayInstant,
SampleFrequency: "100%",
}
// ConsumerOption configures consumers
type ConsumerOption func(o *api.ConsumerConfig) error
// Consumer represents a JetStream consumer
type Consumer struct {
name string
stream string
cfg *api.ConsumerConfig
mgr *Manager
lastInfo *api.ConsumerInfo
sync.Mutex
}
// NewConsumerFromDefault creates a new consumer based on a template config that gets modified by opts
func (m *Manager) NewConsumerFromDefault(stream string, dflt api.ConsumerConfig, opts ...ConsumerOption) (consumer *Consumer, err error) {
if !IsValidName(stream) {
return nil, fmt.Errorf("%q is not a valid stream name", stream)
}
cfg, err := NewConsumerConfiguration(dflt, opts...)
if err != nil {
return nil, err
}
valid, errs := cfg.Validate()
if !valid {
return nil, fmt.Errorf("configuration validation failed: %s", strings.Join(errs, ", "))
}
req := api.JSApiConsumerCreateRequest{
Stream: stream,
Config: *cfg,
Pedantic: m.pedantic,
}
createdInfo, err := m.createConsumer(req)
if err != nil {
return nil, err
}
if createdInfo == nil {
return nil, fmt.Errorf("expected a consumer name but none were generated")
}
c := m.consumerFromCfg(stream, createdInfo.Name, &createdInfo.Config)
c.lastInfo = createdInfo
return c, nil
}
func (m *Manager) createConsumer(req api.JSApiConsumerCreateRequest) (info *api.ConsumerInfo, err error) {
var resp api.JSApiConsumerCreateResponse
if req.Config.Name == "" {
return nil, fmt.Errorf("consumer conmfiguration requires a name")
}
var subj string
if req.Config.FilterSubject == "" {
subj = fmt.Sprintf(api.JSApiConsumerCreateWithNameT, req.Stream, req.Config.Name)
} else {
subj = fmt.Sprintf(api.JSApiConsumerCreateExT, req.Stream, req.Config.Name, req.Config.FilterSubject)
}
err = m.jsonRequest(subj, req, &resp)
if err != nil {
return nil, err
}
return resp.ConsumerInfo, nil
}
// NewConsumer creates a consumer based on DefaultConsumer modified by opts
func (m *Manager) NewConsumer(stream string, opts ...ConsumerOption) (consumer *Consumer, err error) {
if !IsValidName(stream) {
return nil, fmt.Errorf("%q is not a valid stream name", stream)
}
return m.NewConsumerFromDefault(stream, DefaultConsumer, opts...)
}
// LoadOrNewConsumer loads a consumer by name if known else creates a new one with these properties
func (m *Manager) LoadOrNewConsumer(stream string, name string, opts ...ConsumerOption) (consumer *Consumer, err error) {
return m.LoadOrNewConsumerFromDefault(stream, name, DefaultConsumer, opts...)
}
// LoadOrNewConsumerFromDefault loads a consumer by name if known else creates a new one with these properties based on template
func (m *Manager) LoadOrNewConsumerFromDefault(stream string, name string, template api.ConsumerConfig, opts ...ConsumerOption) (consumer *Consumer, err error) {
if !IsValidName(stream) {
return nil, fmt.Errorf("%q is not a valid stream name", stream)
}
if !IsValidName(name) {
return nil, fmt.Errorf("%q is not a valid consumer name", name)
}
c, err := m.LoadConsumer(stream, name)
if IsNatsError(err, 10014) {
return m.NewConsumerFromDefault(stream, template, opts...)
}
return c, err
}
// LoadConsumer loads a consumer by name
func (m *Manager) LoadConsumer(stream string, name string) (consumer *Consumer, err error) {
if !IsValidName(stream) {
return nil, fmt.Errorf("%q is not a valid stream name", stream)
}
if !IsValidName(name) {
return nil, fmt.Errorf("%q is not a valid consumer name", name)
}
consumer = m.consumerFromCfg(stream, name, &api.ConsumerConfig{})
err = m.loadConfigForConsumer(consumer)
if err != nil {
return nil, err
}
return consumer, nil
}
func (m *Manager) consumerFromCfg(stream string, name string, cfg *api.ConsumerConfig) *Consumer {
if name == "" && cfg.Name != "" {
name = cfg.Name
}
return &Consumer{
name: name,
stream: stream,
cfg: cfg,
mgr: m,
}
}
// NewConsumerConfiguration generates a new configuration based on template modified by opts
func NewConsumerConfiguration(dflt api.ConsumerConfig, opts ...ConsumerOption) (*api.ConsumerConfig, error) {
cfg := dflt
for _, o := range opts {
err := o(&cfg)
if err != nil {
return nil, err
}
}
if cfg.Durable != "" {
cfg.Name = cfg.Durable
}
if cfg.Name == "" {
cfg.Name = generateConsName()
}
return &cfg, nil
}
const rdigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
const base = 62
func generateConsName() string {
name := nuid.Next()
sha := sha256.New()
sha.Write([]byte(name))
b := sha.Sum(nil)
for i := 0; i < 8; i++ {
b[i] = rdigits[int(b[i]%base)]
}
return string(b[:8])
}
func (m *Manager) loadConfigForConsumer(consumer *Consumer) (err error) {
info, err := m.loadConsumerInfo(consumer.stream, consumer.name)
if err != nil {
return err
}
consumer.Lock()
consumer.cfg = &info.Config
consumer.lastInfo = &info
consumer.Unlock()
return nil
}
func (m *Manager) loadConsumerInfo(s string, c string) (info api.ConsumerInfo, err error) {
var resp api.JSApiConsumerInfoResponse
err = m.jsonRequest(fmt.Sprintf(api.JSApiConsumerInfoT, s, c), nil, &resp)
if err != nil {
return info, err
}
return *resp.ConsumerInfo, nil
}
// ConsumerDescription is a textual description of this consumer to provide additional context
func ConsumerDescription(d string) ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.Description = d
return nil
}
}
// DeliverySubject is the subject where a Push consumer will deliver its messages
func DeliverySubject(s string) ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.DeliverSubject = s
return nil
}
}
// ConsumerName sets a name for the consumer, when creating a durable consumer use DurableName, using ConsumerName allows
// for creating named ephemeral consumers, else a random name will be generated
func ConsumerName(s string) ConsumerOption {
return func(o *api.ConsumerConfig) error {
if !IsValidName(s) {
return fmt.Errorf("%q is not a valid consumer name", s)
}
o.Name = s
return nil
}
}
// DurableName is the name given to the consumer, when not set an ephemeral consumer is created
func DurableName(s string) ConsumerOption {
return func(o *api.ConsumerConfig) error {
if !IsValidName(s) {
return fmt.Errorf("%q is not a valid consumer name", s)
}
o.Durable = s
return nil
}
}
// StartAtSequence starts consuming messages at a specific sequence in the stream
func StartAtSequence(s uint64) ConsumerOption {
return func(o *api.ConsumerConfig) error {
resetDeliverPolicy(o)
o.DeliverPolicy = api.DeliverByStartSequence
o.OptStartSeq = s
return nil
}
}
// StartAtTime starts consuming messages at a specific point in time in the stream
func StartAtTime(t time.Time) ConsumerOption {
return func(o *api.ConsumerConfig) error {
resetDeliverPolicy(o)
o.DeliverPolicy = api.DeliverByStartTime
ut := t.UTC()
o.OptStartTime = &ut
return nil
}
}
// DeliverAllAvailable delivers messages starting with the first available in the stream
func DeliverAllAvailable() ConsumerOption {
return func(o *api.ConsumerConfig) error {
resetDeliverPolicy(o)
o.DeliverPolicy = api.DeliverAll
return nil
}
}
// DeliverLastPerSubject delivers the last message for each subject in a wildcard stream based on the filter subjects of the consumer
func DeliverLastPerSubject() ConsumerOption {
return func(o *api.ConsumerConfig) error {
resetDeliverPolicy(o)
o.DeliverPolicy = api.DeliverLastPerSubject
return nil
}
}
// StartWithLastReceived starts delivery at the last messages received in the stream
func StartWithLastReceived() ConsumerOption {
return func(o *api.ConsumerConfig) error {
resetDeliverPolicy(o)
o.DeliverPolicy = api.DeliverLast
return nil
}
}
// StartWithNextReceived starts delivery at the next messages received in the stream
func StartWithNextReceived() ConsumerOption {
return func(o *api.ConsumerConfig) error {
resetDeliverPolicy(o)
o.DeliverPolicy = api.DeliverNew
return nil
}
}
// StartAtTimeDelta starts delivering messages at a past point in time
func StartAtTimeDelta(d time.Duration) ConsumerOption {
return func(o *api.ConsumerConfig) error {
resetDeliverPolicy(o)
t := time.Now().UTC().Add(-1 * d)
o.DeliverPolicy = api.DeliverByStartTime
o.OptStartTime = &t
return nil
}
}
// DeliverHeadersOnly configures the consumer to only deliver existing header and the `Nats-Msg-Size` header, no bodies.
// To deliver also the bodies use DeliverBodies.
func DeliverHeadersOnly() ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.HeadersOnly = true
return nil
}
}
// DeliverBodies configures the consumer to deliver the headers and the bodies for each message.
// To only deliver headers only use DeliverHeadersOnly.
func DeliverBodies() ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.HeadersOnly = false
return nil
}
}
func resetDeliverPolicy(o *api.ConsumerConfig) {
o.DeliverPolicy = api.DeliverAll
o.OptStartSeq = 0
o.OptStartTime = nil
}
// AcknowledgeNone disables message acknowledgement
func AcknowledgeNone() ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.AckPolicy = api.AckNone
return nil
}
}
// AcknowledgeAll enables an acknowledgement mode where acknowledging message 100 will also ack the preceding messages
func AcknowledgeAll() ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.AckPolicy = api.AckAll
return nil
}
}
// AcknowledgeExplicit requires that every message received be acknowledged
func AcknowledgeExplicit() ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.AckPolicy = api.AckExplicit
return nil
}
}
// AckWait sets the time a delivered message might remain unacknowledged before redelivery is attempted
func AckWait(t time.Duration) ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.AckWait = t
return nil
}
}
// MaxDeliveryAttempts is the number of times a message will be attempted to be delivered
func MaxDeliveryAttempts(n int) ConsumerOption {
return func(o *api.ConsumerConfig) error {
if n == 0 {
return fmt.Errorf("configuration would prevent all deliveries")
}
o.MaxDeliver = n
return nil
}
}
// FilterStreamBySubject filters the messages in a wildcard stream to those matching a specific subject
func FilterStreamBySubject(s ...string) ConsumerOption {
return func(o *api.ConsumerConfig) error {
if len(s) == 1 {
// If there is only one filter, reset the multiple subjects filter.
o.FilterSubjects = nil
o.FilterSubject = s[0]
} else {
// If there are more subjects, reset the single subject filter.
o.FilterSubject = ""
o.FilterSubjects = s
}
return nil
}
}
// ReplayInstantly delivers messages to the consumer as fast as possible
func ReplayInstantly() ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.ReplayPolicy = api.ReplayInstant
return nil
}
}
// ReplayAsReceived delivers messages at the rate they were received at
func ReplayAsReceived() ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.ReplayPolicy = api.ReplayOriginal
return nil
}
}
// SamplePercent configures sampling of a subset of messages expressed as a percentage
func SamplePercent(i int) ConsumerOption {
return func(o *api.ConsumerConfig) error {
if i < 0 || i > 100 {
return fmt.Errorf("sample percent must be 0-100")
}
if i == 0 {
o.SampleFrequency = ""
return nil
}
o.SampleFrequency = fmt.Sprintf("%d%%", i)
return nil
}
}
// RateLimitBitsPerSecond limits message delivery to a rate in bits per second
func RateLimitBitsPerSecond(bps uint64) ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.RateLimit = bps
return nil
}
}
// MaxWaiting is the number of outstanding pulls that are allowed on any one consumer. Pulls made that exceeds this limit are discarded.
func MaxWaiting(pulls uint) ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.MaxWaiting = int(pulls)
return nil
}
}
// MaxAckPending maximum number of messages without acknowledgement that can be outstanding, once this limit is reached message delivery will be suspended
func MaxAckPending(pending uint) ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.MaxAckPending = int(pending)
return nil
}
}
// IdleHeartbeat sets the time before an idle consumer will send a empty message with Status header 100 indicating the consumer is still alive
func IdleHeartbeat(hb time.Duration) ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.Heartbeat = hb
return nil
}
}
// PushFlowControl enables flow control for push based consumers
func PushFlowControl() ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.FlowControl = true
return nil
}
}
// DeliverGroup when set will only deliver messages to subscriptions matching that group
func DeliverGroup(g string) ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.DeliverGroup = g
return nil
}
}
// MaxRequestMaxBytes sets the limit of max bytes a consumer my request
func MaxRequestMaxBytes(max int) ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.MaxRequestMaxBytes = max
return nil
}
}
// MaxRequestBatch is the largest batch that can be specified when doing pulls against the consumer
func MaxRequestBatch(max uint) ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.MaxRequestBatch = int(max)
return nil
}
}
// MaxRequestExpires is the longest pull request expire the server will allow
func MaxRequestExpires(max time.Duration) ConsumerOption {
return func(o *api.ConsumerConfig) error {
if max != 0 && max < time.Millisecond {
return fmt.Errorf("must be larger than 1ms")
}
o.MaxRequestExpires = max
return nil
}
}
// InactiveThreshold is the idle time an ephemeral consumer allows before it is removed
func InactiveThreshold(t time.Duration) ConsumerOption {
return func(o *api.ConsumerConfig) error {
if t < 0 {
return fmt.Errorf("inactive threshold must be positive")
}
o.InactiveThreshold = t
return nil
}
}
// BackoffIntervals sets a series of intervals by which retries will be attempted for this consumr
func BackoffIntervals(i ...time.Duration) ConsumerOption {
return func(o *api.ConsumerConfig) error {
if len(i) == 0 {
return fmt.Errorf("at least one interval is required")
}
o.BackOff = i
return nil
}
}
// BackoffPolicy sets a consumer policy
func BackoffPolicy(policy []time.Duration) ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.BackOff = policy
return nil
}
}
// ConsumerOverrideReplicas override the replica count inherited from the Stream with this value
func ConsumerOverrideReplicas(r int) ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.Replicas = r
return nil
}
}
func ConsumerOverrideMemoryStorage() ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.MemoryStorage = true
return nil
}
}
// LinearBackoffPolicy creates a backoff policy with linearly increasing steps between min and max
func LinearBackoffPolicy(steps uint, min time.Duration, max time.Duration) ConsumerOption {
return func(o *api.ConsumerConfig) error {
p, err := LinearBackoffPeriods(steps, min, max)
if err != nil {
return err
}
o.BackOff = p
return nil
}
}
func ConsumerMetadata(meta map[string]string) ConsumerOption {
return func(o *api.ConsumerConfig) error {
for k := range meta {
if len(k) == 0 {
return fmt.Errorf("invalid empty string key in metadata")
}
}
o.Metadata = meta
return nil
}
}
func PauseUntil(deadline time.Time) ConsumerOption {
return func(o *api.ConsumerConfig) error {
o.PauseUntil = deadline
return nil
}
}
// UpdateConfiguration updates the consumer configuration
// At present the description, ack wait, max deliver, sample frequency, max ack pending, max waiting and header only settings can be changed
func (c *Consumer) UpdateConfiguration(opts ...ConsumerOption) error {
if !c.IsDurable() {
return fmt.Errorf("only durable consumers can be updated")
}
ncfg, err := NewConsumerConfiguration(*c.cfg, opts...)
if err != nil {
return err
}
_, err = c.mgr.NewConsumerFromDefault(c.stream, *ncfg)
if err != nil {
return err
}
return c.Reset()
}
// Reset reloads the Consumer configuration from the JetStream server
func (c *Consumer) Reset() error {
return c.mgr.loadConfigForConsumer(c)
}
// NextSubject returns the subject used to retrieve the next message for pull-based Consumers, empty when not a pull-base consumer
func (m *Manager) NextSubject(stream string, consumer string) (string, error) {
s, err := NextSubject(stream, consumer)
if err != nil {
return "", err
}
return m.apiSubject(s), err
}
// NextSubject returns the subject used to retrieve the next message for pull-based Consumers, empty when not a pull-base consumer
func (c *Consumer) NextSubject() string {
if !c.IsPullMode() {
return ""
}
s, _ := c.mgr.NextSubject(c.stream, c.name)
return s
}
func DirectSubject(stream string) (string, error) {
if !IsValidName(stream) {
return "", fmt.Errorf("%q is not a valid stream name", stream)
}
return fmt.Sprintf(api.JSDirectMsgGetT, stream), nil
}
// NextSubject returns the subject used to retrieve the next message for pull-based Consumers, empty when not a pull-base consumer
func NextSubject(stream string, consumer string) (string, error) {
if !IsValidName(stream) {
return "", fmt.Errorf("%q is not a valid stream name", stream)
}
if !IsValidName(consumer) {
return "", fmt.Errorf("%q is not a valid consumer name", consumer)
}
return fmt.Sprintf(api.JSApiRequestNextT, stream, consumer), nil
}
// AckSampleSubject is the subject used to publish ack samples to
func (c *Consumer) AckSampleSubject() string {
if c.SampleFrequency() == "" {
return ""
}
return api.JSMetricConsumerAckPre + "." + c.StreamName() + "." + c.name
}
// AdvisorySubject is a wildcard subscription subject that subscribes to all advisories for this consumer
func (c *Consumer) AdvisorySubject() string {
return api.JSAdvisoryPrefix + ".CONSUMER.*." + c.StreamName() + "." + c.name
}
// MetricSubject is a wildcard subscription subject that subscribes to all metrics for this consumer
func (c *Consumer) MetricSubject() string {
return api.JSMetricPrefix + ".CONSUMER.*." + c.StreamName() + "." + c.name
}
// NextMsg requests the next message from the server with the manager timeout
func (m *Manager) NextMsg(stream string, consumer string) (*nats.Msg, error) {
if !m.nc.Opts.UseOldRequestStyle {
return nil, fmt.Errorf("pull mode requires the use of UseOldRequestStyle() option")
}
s, err := m.NextSubject(stream, consumer)
if err != nil {
return nil, err
}
rj, err := json.Marshal(&api.JSApiConsumerGetNextRequest{
Expires: m.timeout,
Batch: 1,
})
if err != nil {
return nil, err
}
return m.request(s, rj)
}
// NextMsgRequest creates a request for a batch of messages on a consumer, data or control flow messages will be sent to inbox
func (m *Manager) NextMsgRequest(stream string, consumer string, inbox string, req *api.JSApiConsumerGetNextRequest) error {
s, err := m.NextSubject(stream, consumer)
if err != nil {
return err
}
jreq, err := json.Marshal(req)
if err != nil {
return err
}
if m.trace {
log.Printf(">>> %s:\n%s\n\n", s, string(jreq))
}
return m.nc.PublishMsg(&nats.Msg{Subject: s, Reply: inbox, Data: jreq})
}
// NextMsgContext requests the next message from the server. This request will wait for as long as the context is
// active. If repeated pulls will be made it's better to use NextMsgRequest()
func (m *Manager) NextMsgContext(ctx context.Context, stream string, consumer string) (*nats.Msg, error) {
if !m.nc.Opts.UseOldRequestStyle {
return nil, fmt.Errorf("pull mode requires the use of UseOldRequestStyle() option")
}
s, err := m.NextSubject(stream, consumer)
if err != nil {
return nil, err
}
return m.requestWithContext(ctx, s, []byte(strconv.Itoa(1)))
}
// NextMsgRequest creates a request for a batch of messages, data or control flow messages will be sent to inbox
func (c *Consumer) NextMsgRequest(inbox string, req *api.JSApiConsumerGetNextRequest) error {
return c.mgr.NextMsgRequest(c.stream, c.name, inbox, req)
}
// NextMsg retrieves the next message, waiting up to manager timeout for a response
func (c *Consumer) NextMsg() (*nats.Msg, error) {
return c.mgr.NextMsg(c.stream, c.name)
}
// NextMsgContext retrieves the next message, interrupted by the cancel context ctx
func (c *Consumer) NextMsgContext(ctx context.Context) (*nats.Msg, error) {
return c.mgr.NextMsgContext(ctx, c.stream, c.name)
}
// DeliveredState reports the messages sequences that were successfully delivered
func (c *Consumer) DeliveredState() (api.SequenceInfo, error) {
info, err := c.State()
if err != nil {
return api.SequenceInfo{}, err
}
return info.Delivered, nil
}
// AcknowledgedFloor reports the highest contiguous message sequences that were acknowledged
func (c *Consumer) AcknowledgedFloor() (api.SequenceInfo, error) {
info, err := c.State()
if err != nil {
return api.SequenceInfo{}, err
}
return info.AckFloor, nil
}
// PendingAcknowledgement reports the number of messages sent but not yet acknowledged
func (c *Consumer) PendingAcknowledgement() (int, error) {
info, err := c.State()
if err != nil {
return 0, err
}
return info.NumAckPending, nil
}
// PendingMessages is the number of unprocessed messages for this consumer
func (c *Consumer) PendingMessages() (uint64, error) {
info, err := c.State()
if err != nil {
return 0, err
}
return info.NumPending, nil
}
// WaitingClientPulls is the number of clients that have outstanding pull requests against this consumer
func (c *Consumer) WaitingClientPulls() (int, error) {
info, err := c.State()
if err != nil {
return 0, err
}
return info.NumWaiting, nil
}
// RedeliveryCount reports the number of redelivers that were done
func (c *Consumer) RedeliveryCount() (int, error) {
info, err := c.State()
if err != nil {
return 0, err
}
return info.NumRedelivered, nil
}
// LatestState returns the most recently loaded state
func (c *Consumer) LatestState() (api.ConsumerInfo, error) {
c.Lock()
s := c.lastInfo
c.Unlock()
if s != nil {
return *s, nil
}
return c.State()
}
// State loads a snapshot of consumer state including delivery counts, retries and more
func (c *Consumer) State() (api.ConsumerInfo, error) {
s, err := c.mgr.loadConsumerInfo(c.stream, c.name)
if err != nil {
return api.ConsumerInfo{}, err
}
c.Lock()
c.lastInfo = &s
c.Unlock()
return s, nil
}
// Configuration is the Consumer configuration
func (c *Consumer) Configuration() (config api.ConsumerConfig) {
return *c.cfg
}
// Delete deletes the Consumer, after this the Consumer object should be disposed
func (c *Consumer) Delete() (err error) {
var resp api.JSApiConsumerDeleteResponse
err = c.mgr.jsonRequest(fmt.Sprintf(api.JSApiConsumerDeleteT, c.StreamName(), c.Name()), nil, &resp)
if err != nil {
return err
}
if resp.Success {
return nil
}
return fmt.Errorf("unknown response while removing consumer %s", c.Name())
}
// LeaderStepDown requests the current RAFT group leader in a clustered JetStream to stand down forcing a new election
func (c *Consumer) LeaderStepDown() error {
var resp api.JSApiConsumerLeaderStepDownResponse
err := c.mgr.jsonRequest(fmt.Sprintf(api.JSApiConsumerLeaderStepDownT, c.StreamName(), c.Name()), nil, &resp)
if err != nil {
return err
}
if !resp.Success {
return fmt.Errorf("unknown error while requesting leader step down")
}
return nil
}
// Pause requests a consumer be paused until the deadline, if it fails to pause an error is returned.
//
// A common reason for failures is when a time is supplied that is in the past from the perspective of the server
func (c *Consumer) Pause(deadline time.Time) (*api.JSApiConsumerPauseResponse, error) {
var resp *api.JSApiConsumerPauseResponse
req := api.JSApiConsumerPauseRequest{
PauseUntil: deadline,
}
err := c.mgr.jsonRequest(fmt.Sprintf(api.JSApiConsumerPauseT, c.StreamName(), c.Name()), &req, &resp)
if err != nil {
return nil, err
}
if !resp.Paused {
return nil, fmt.Errorf("pause request failed, perhaps due to a time in the past")
}
return resp, nil
}
// Resume requests the server resumes a paused consumer
func (c *Consumer) Resume() error {
var resp *api.JSApiConsumerPauseResponse
err := c.mgr.jsonRequest(fmt.Sprintf(api.JSApiConsumerPauseT, c.StreamName(), c.Name()), nil, &resp)
if err != nil {
return err
}
if resp.Paused {
return fmt.Errorf("pause request failed for an unknown reason")
}
return nil
}
func (c *Consumer) Name() string { return c.name }
func (c *Consumer) IsSampled() bool { return c.SampleFrequency() != "" }
func (c *Consumer) IsPullMode() bool { return c.cfg.DeliverSubject == "" }
func (c *Consumer) IsPushMode() bool { return !c.IsPullMode() }
func (c *Consumer) IsDurable() bool { return c.cfg.Durable != "" }
func (c *Consumer) IsEphemeral() bool { return !c.IsDurable() }
func (c *Consumer) IsHeadersOnly() bool { return c.cfg.HeadersOnly }
func (c *Consumer) StreamName() string { return c.stream }
func (c *Consumer) DeliverySubject() string { return c.cfg.DeliverSubject }
func (c *Consumer) DurableName() string { return c.cfg.Durable }
func (c *Consumer) Description() string { return c.cfg.Description }
func (c *Consumer) StartSequence() uint64 { return c.cfg.OptStartSeq }
func (c *Consumer) DeliverPolicy() api.DeliverPolicy { return c.cfg.DeliverPolicy }
func (c *Consumer) AckPolicy() api.AckPolicy { return c.cfg.AckPolicy }
func (c *Consumer) AckWait() time.Duration { return c.cfg.AckWait }
func (c *Consumer) MaxDeliver() int { return c.cfg.MaxDeliver }
func (c *Consumer) Backoff() []time.Duration { return c.cfg.BackOff }
func (c *Consumer) FilterSubject() string { return c.cfg.FilterSubject }
func (c *Consumer) FilterSubjects() []string { return c.cfg.FilterSubjects }
func (c *Consumer) ReplayPolicy() api.ReplayPolicy { return c.cfg.ReplayPolicy }
func (c *Consumer) SampleFrequency() string { return c.cfg.SampleFrequency }
func (c *Consumer) RateLimit() uint64 { return c.cfg.RateLimit }
func (c *Consumer) MaxAckPending() int { return c.cfg.MaxAckPending }
func (c *Consumer) FlowControl() bool { return c.cfg.FlowControl }
func (c *Consumer) Heartbeat() time.Duration { return c.cfg.Heartbeat }
func (c *Consumer) DeliverGroup() string { return c.cfg.DeliverGroup }
func (c *Consumer) MaxWaiting() int { return c.cfg.MaxWaiting }
func (c *Consumer) MaxRequestBatch() int { return c.cfg.MaxRequestBatch }
func (c *Consumer) MaxRequestExpires() time.Duration { return c.cfg.MaxRequestExpires }
func (c *Consumer) MaxRequestMaxBytes() int { return c.cfg.MaxRequestMaxBytes }
func (c *Consumer) InactiveThreshold() time.Duration { return c.cfg.InactiveThreshold }
func (c *Consumer) Replicas() int { return c.cfg.Replicas }
func (c *Consumer) Metadata() map[string]string { return c.cfg.Metadata }
func (c *Consumer) MemoryStorage() bool { return c.cfg.MemoryStorage }
func (c *Consumer) PauseUntil() time.Time { return c.cfg.PauseUntil }
func (c *Consumer) StartTime() time.Time {
if c.cfg.OptStartTime == nil {
return time.Time{}
}
return *c.cfg.OptStartTime
}