MCPcopy Create free account
hub / github.com/google/go-cloud / getNextBatch

Method getNextBatch

pubsub/pubsub.go:652–686  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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
652func (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
688var errSubscriptionShutdown = gcerr.Newf(gcerr.FailedPrecondition, nil, "pubsub: Subscription has been Shutdown")
689

Callers 1

ReceiveMethod · 0.95

Calls 7

SplitFunction · 0.92
CallFunction · 0.92
StartMethod · 0.80
EndMethod · 0.80
WaitMethod · 0.80
wrapErrorFunction · 0.70
ReceiveBatchMethod · 0.65

Tested by

no test coverage detected