Skip to content
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

fix(pubsub): prevent draining error return for Receive #4733

Merged
merged 3 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 0 additions & 7 deletions pubsub/internal/scheduler/receive_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package scheduler

import (
"errors"
"sync"
)

Expand Down Expand Up @@ -68,12 +67,6 @@ func NewReceiveScheduler(workers int) *ReceiveScheduler {
// call causes pushback to the pubsub service (less Receive calls on the
// long-lived stream), which keeps memory footprint stable.
func (s *ReceiveScheduler) Add(key string, item interface{}, handle func(item interface{})) error {
select {
case <-s.done:
return errors.New("draining")
default:
}

if key == "" {
// Spawn a worker.
s.workers <- struct{}{}
Expand Down
16 changes: 16 additions & 0 deletions pubsub/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -925,13 +925,29 @@ func (s *Subscription) Receive(ctx context.Context, f func(context.Context, *Mes
}
}
}
// If the context is done, don't pull more messages.
select {
case <-ctx.Done():
return nil
codyoss marked this conversation as resolved.
Show resolved Hide resolved
default:
}
msgs, err := iter.receive(maxToPull)
if err == io.EOF {
return nil
}
if err != nil {
return err
}
// If context is done and messages have been pulled,
// nack them.
select {
case <-ctx.Done():
for _, m := range msgs {
m.Nack()
}
return nil
default:
}
for i, msg := range msgs {
msg := msg
// TODO(jba): call acquire closer to when the message is allocated.
Expand Down