puller.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2016 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package pubsub
  15. import (
  16. "sync"
  17. "golang.org/x/net/context"
  18. )
  19. // puller fetches messages from the server in a batch.
  20. type puller struct {
  21. Client *Client
  22. Sub string
  23. BatchSize int64
  24. // A function to call when a new message is fetched from the server, but not yet returned from Next.
  25. Notify func(ackID string)
  26. mu sync.Mutex
  27. buf []*Message
  28. }
  29. // Next returns the next message from the server, fetching a new batch if necessary.
  30. // Notify is called with the ackIDs of newly fetched messages.
  31. func (p *puller) Next(ctx context.Context) (*Message, error) {
  32. p.mu.Lock()
  33. defer p.mu.Unlock()
  34. // TODO: prefetch.
  35. for len(p.buf) == 0 {
  36. var err error
  37. p.buf, err = p.Client.s.fetchMessages(ctx, p.Sub, p.BatchSize)
  38. if err != nil {
  39. // TODO: retry before giving up.
  40. return nil, err
  41. }
  42. for _, m := range p.buf {
  43. p.Notify(m.AckID)
  44. }
  45. }
  46. m := p.buf[0]
  47. p.buf = p.buf[1:]
  48. return m, nil
  49. }
  50. // Pending returns the list of messages that have been fetched from the server
  51. // but not yet returned via Next.
  52. func (p *puller) Pending() []*Message {
  53. p.mu.Lock()
  54. defer p.mu.Unlock()
  55. return p.buf
  56. }