@@ -729,17 +729,26 @@ func (s *pullSubscription) Drain() {
729
729
// It will wait up to provided expiry time if not all messages are available.
730
730
func (p * pullConsumer ) Fetch (batch int , opts ... FetchOpt ) (MessageBatch , error ) {
731
731
req := & pullRequest {
732
- Batch : batch ,
733
- Expires : DefaultExpires ,
732
+ Batch : batch ,
733
+ Expires : DefaultExpires ,
734
+ Heartbeat : unset ,
734
735
}
735
736
for _ , opt := range opts {
736
737
if err := opt (req ); err != nil {
737
738
return nil , err
738
739
}
739
740
}
740
- // for longer pulls, set heartbeat value
741
- if req .Expires >= 10 * time .Second {
742
- req .Heartbeat = 5 * time .Second
741
+ // if heartbeat was not explicitly set, set it to 5 seconds for longer pulls
742
+ // and disable it for shorter pulls
743
+ if req .Heartbeat == unset {
744
+ if req .Expires >= 10 * time .Second {
745
+ req .Heartbeat = 5 * time .Second
746
+ } else {
747
+ req .Heartbeat = 0
748
+ }
749
+ }
750
+ if req .Expires < 2 * req .Heartbeat {
751
+ return nil , fmt .Errorf ("%w: expiry time should be at least 2 times the heartbeat" , ErrInvalidOption )
743
752
}
744
753
745
754
return p .fetch (req )
@@ -748,26 +757,35 @@ func (p *pullConsumer) Fetch(batch int, opts ...FetchOpt) (MessageBatch, error)
748
757
// FetchBytes is used to retrieve up to a provided bytes from the stream.
749
758
func (p * pullConsumer ) FetchBytes (maxBytes int , opts ... FetchOpt ) (MessageBatch , error ) {
750
759
req := & pullRequest {
751
- Batch : 1000000 ,
752
- MaxBytes : maxBytes ,
753
- Expires : DefaultExpires ,
760
+ Batch : 1000000 ,
761
+ MaxBytes : maxBytes ,
762
+ Expires : DefaultExpires ,
763
+ Heartbeat : unset ,
754
764
}
755
765
for _ , opt := range opts {
756
766
if err := opt (req ); err != nil {
757
767
return nil , err
758
768
}
759
769
}
760
- // for longer pulls, set heartbeat value
761
- if req .Expires >= 10 * time .Second {
762
- req .Heartbeat = 5 * time .Second
770
+ // if heartbeat was not explicitly set, set it to 5 seconds for longer pulls
771
+ // and disable it for shorter pulls
772
+ if req .Heartbeat == unset {
773
+ if req .Expires >= 10 * time .Second {
774
+ req .Heartbeat = 5 * time .Second
775
+ } else {
776
+ req .Heartbeat = 0
777
+ }
778
+ }
779
+ if req .Expires < 2 * req .Heartbeat {
780
+ return nil , fmt .Errorf ("%w: expiry time should be at least 2 times the heartbeat" , ErrInvalidOption )
763
781
}
764
782
765
783
return p .fetch (req )
766
784
}
767
785
768
786
// FetchNoWait sends a single request to retrieve given number of messages.
769
- // If there are any messages available at the time of sending request,
770
- // FetchNoWait will return immediately .
787
+ // FetchNoWait will only return messages that are available at the time of the
788
+ // request. It will not wait for more messages to arrive .
771
789
func (p * pullConsumer ) FetchNoWait (batch int ) (MessageBatch , error ) {
772
790
req := & pullRequest {
773
791
Batch : batch ,
@@ -842,6 +860,10 @@ func (p *pullConsumer) fetch(req *pullRequest) (MessageBatch, error) {
842
860
return
843
861
}
844
862
p .Unlock ()
863
+ case err := <- sub .errs :
864
+ res .err = err
865
+ res .done = true
866
+ return
845
867
case <- time .After (req .Expires + 1 * time .Second ):
846
868
res .done = true
847
869
return
0 commit comments