mirror of
https://github.com/nats-io/nats.go.git
synced 2025-10-30 19:36:29 +08:00
[IMPROVED] Fetch and FetchBatch for draining and closed subscriptions (#1582)
Signed-off-by: Piotr Piotrowski <piotr@synadia.com>
This commit is contained in:
16
js.go
16
js.go
@@ -2861,7 +2861,13 @@ func (sub *Subscription) Fetch(batch int, opts ...PullOpt) ([]*Msg, error) {
|
||||
}
|
||||
var hbTimer *time.Timer
|
||||
var hbErr error
|
||||
if err == nil && len(msgs) < batch {
|
||||
sub.mu.Lock()
|
||||
subClosed := sub.closed || sub.draining
|
||||
sub.mu.Unlock()
|
||||
if subClosed {
|
||||
err = errors.Join(ErrBadSubscription, ErrSubscriptionClosed)
|
||||
}
|
||||
if err == nil && len(msgs) < batch && !subClosed {
|
||||
// For batch real size of 1, it does not make sense to set no_wait in
|
||||
// the request.
|
||||
noWait := batch-len(msgs) > 1
|
||||
@@ -3129,8 +3135,14 @@ func (sub *Subscription) FetchBatch(batch int, opts ...PullOpt) (MessageBatch, e
|
||||
result.msgs <- msg
|
||||
}
|
||||
}
|
||||
if len(result.msgs) == batch || result.err != nil {
|
||||
sub.mu.Lock()
|
||||
subClosed := sub.closed || sub.draining
|
||||
sub.mu.Unlock()
|
||||
if len(result.msgs) == batch || result.err != nil || subClosed {
|
||||
close(result.msgs)
|
||||
if subClosed && len(result.msgs) == 0 {
|
||||
return nil, errors.Join(ErrBadSubscription, ErrSubscriptionClosed)
|
||||
}
|
||||
result.done <- struct{}{}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user