getNextBatch gets the next batch of messages from the server. It will return a channel that will itself return the messages as they come from each independent batch, or an operation error
(nMessages int)
| 650 | // getNextBatch gets the next batch of messages from the server. It will return a channel that will itself return the |
| 651 | // messages as they come from each independent batch, or an operation error |
| 652 | func (s *Subscription) getNextBatch(nMessages int) chan msgsOrError { |
| 653 | // Split nMessages into batches based on recvBatchOpts; we'll make a |
| 654 | // separate ReceiveBatch call for each batch, and aggregate the results in |
| 655 | // msgs. |
| 656 | batches := batcher.Split(nMessages, s.recvBatchOpts) |
| 657 | result := make(chan msgsOrError, len(batches)) |
| 658 | g, ctx := errgroup.WithContext(s.backgroundCtx) |
| 659 | for _, maxMessagesInBatch := range batches { |
| 660 | // Make a copy of the loop variable since it will be used by a goroutine. |
| 661 | curMaxMessagesInBatch := maxMessagesInBatch |
| 662 | g.Go(func() error { |
| 663 | var msgs []*driver.Message |
| 664 | err := retry.Call(ctx, gax.Backoff{}, s.driver.IsRetryable, func() error { |
| 665 | var err error |
| 666 | spanCtx, span := s.tracer.Start(ctx, "driver.Subscription.ReceiveBatch") |
| 667 | defer func() { s.tracer.End(spanCtx, span, err) }() |
| 668 | msgs, err = s.driver.ReceiveBatch(spanCtx, curMaxMessagesInBatch) |
| 669 | return err |
| 670 | }) |
| 671 | if err != nil { |
| 672 | return wrapError(s.driver, err) |
| 673 | } |
| 674 | result <- msgsOrError{msgs: msgs} |
| 675 | return nil |
| 676 | }) |
| 677 | } |
| 678 | go func() { |
| 679 | // wait on group completion on the background and proper channel closing |
| 680 | if err := g.Wait(); err != nil { |
| 681 | result <- msgsOrError{err: err} |
| 682 | } |
| 683 | close(result) |
| 684 | }() |
| 685 | return result |
| 686 | } |
| 687 | |
| 688 | var errSubscriptionShutdown = gcerr.Newf(gcerr.FailedPrecondition, nil, "pubsub: Subscription has been Shutdown") |
| 689 |