| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package subscriber | |
| 6 | |
| 7 import ( | |
| 8 "github.com/luci/luci-go/common/gcloud/pubsub" | |
| 9 "golang.org/x/net/context" | |
| 10 ) | |
| 11 | |
| 12 // Source is used to pull Pub/Sub messages in batches. | |
| 13 type Source interface { | |
| 14 // Pull retrieves a new batch of Pub/Sub messages to process. | |
| 15 Pull(context.Context) ([]*pubsub.Message, error) | |
| 16 } | |
| 17 | |
| 18 // PubSubSource is a Source implementation built on top of a pubsub.PubSub. | |
| 19 type pubSubSource struct { | |
| 20 ps pubsub.Connection | |
| 21 sub pubsub.Subscription | |
| 22 batch int | |
| 23 } | |
| 24 | |
| 25 // NewSource generates a new Source by wrapping a pubsub.Connection | |
| 26 // implementation. This Source is bound to a single subscription. | |
| 27 // | |
| 28 // If the supplied batch size is <= 0, the maximum allowed Pub/Sub batch size | |
| 29 // will be used. | |
| 30 func NewSource(ps pubsub.Connection, s pubsub.Subscription, batch int) Source { | |
| 31 if batch <= 0 { | |
| 32 batch = pubsub.MaxSubscriptionPullSize | |
| 33 } | |
| 34 return &pubSubSource{ | |
| 35 ps: ps, | |
| 36 sub: s, | |
| 37 batch: batch, | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 func (s *pubSubSource) Pull(c context.Context) (msgs []*pubsub.Message, err erro
r) { | |
| 42 return s.ps.Pull(c, s.sub, s.batch) | |
| 43 } | |
| OLD | NEW |