Chromium Code Reviews| 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/gcps" | |
| 9 "golang.org/x/net/context" | |
| 10 "google.golang.org/cloud/pubsub" | |
| 11 ) | |
| 12 | |
| 13 // Source is used to pull Pub/Sub messages in batches. | |
| 14 type Source interface { | |
|
dnj (Google)
2016/01/21 04:36:24
Generalized Source allows for greater interoperabi
| |
| 15 // Pull retrieves a new batch of Pub/Sub messages to process. | |
| 16 Pull(context.Context) ([]*pubsub.Message, error) | |
| 17 } | |
| 18 | |
| 19 // PubSubSource is a Source implementation built on top of a gcps.PubSub. | |
| 20 type pubSubSource struct { | |
| 21 ps gcps.PubSub | |
| 22 sub gcps.Subscription | |
| 23 batch int | |
| 24 } | |
| 25 | |
| 26 // NewSource generates a new Source by wrapping a gcps.PubSub method. This | |
| 27 // Source is bound to a single subscription. | |
| 28 // | |
| 29 // If the supplied batch size is <= 0, the maximum allowed Pub/Sub batch size | |
| 30 // will be used. | |
| 31 func NewSource(ps gcps.PubSub, s gcps.Subscription, batch int) Source { | |
| 32 if batch <= 0 { | |
| 33 batch = gcps.MaxSubscriptionPullSize | |
| 34 } | |
| 35 return &pubSubSource{ | |
| 36 ps: ps, | |
| 37 sub: s, | |
| 38 batch: batch, | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 func (s *pubSubSource) Pull(c context.Context) (msgs []*pubsub.Message, err erro r) { | |
| 43 return s.ps.Pull(c, s.sub, s.batch) | |
| 44 } | |
| OLD | NEW |