Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 gcps | |
| 6 | |
| 7 import ( | |
| 8 "github.com/luci/luci-go/common/retry" | |
| 9 "golang.org/x/net/context" | |
| 10 "google.golang.org/cloud/pubsub" | |
| 11 ) | |
| 12 | |
| 13 // Retry wraps a PubSub implementation and retries on transient errors. | |
|
dnj (Google)
2016/01/21 04:36:24
This is super useful. Now we don't have to have on
| |
| 14 type Retry struct { | |
| 15 // PS is the PubSub to retry. | |
| 16 PS PubSub | |
| 17 // G is the retry generator to use. If nil, retry.Default will be used. | |
| 18 G retry.Generator | |
| 19 | |
| 20 // C, if not nil, will be called when an error is encountered. | |
| 21 C retry.Callback | |
| 22 } | |
| 23 | |
| 24 // TopicExists implements the PubSub interface. | |
| 25 func (r *Retry) TopicExists(c context.Context, t Topic) (exists bool, err error) { | |
| 26 err = retry.Retry(c, r.retryIter(), func() (err error) { | |
| 27 exists, err = r.TopicExists(c, t) | |
| 28 return | |
| 29 }, r.C) | |
| 30 return | |
| 31 } | |
| 32 | |
| 33 // SubExists implements the PubSub interface. | |
| 34 func (r *Retry) SubExists(c context.Context, s Subscription) (exists bool, err e rror) { | |
| 35 err = retry.Retry(c, r.retryIter(), func() (err error) { | |
| 36 exists, err = r.SubExists(c, s) | |
| 37 return | |
| 38 }, r.C) | |
| 39 return | |
| 40 } | |
| 41 | |
| 42 // Publish implements the PubSub interface. | |
| 43 func (r *Retry) Publish(c context.Context, t Topic, msgs ...*pubsub.Message) (id s []string, err error) { | |
| 44 err = retry.Retry(c, r.retryIter(), func() (err error) { | |
| 45 ids, err = r.Publish(c, t, msgs...) | |
| 46 return | |
| 47 }, r.C) | |
| 48 return | |
| 49 } | |
| 50 | |
| 51 // Pull implements the PubSub interface. | |
| 52 func (r *Retry) Pull(c context.Context, s Subscription, batch int) (msgs []*pubs ub.Message, err error) { | |
| 53 err = retry.Retry(c, r.retryIter(), func() (err error) { | |
| 54 msgs, err = r.Pull(c, s, batch) | |
| 55 return | |
| 56 }, r.C) | |
| 57 return | |
| 58 } | |
| 59 | |
| 60 // Ack implements the PubSub interface. | |
| 61 func (r *Retry) Ack(c context.Context, s Subscription, ackIDs ...string) (err er ror) { | |
| 62 return retry.Retry(c, r.retryIter(), func() error { | |
| 63 return r.Ack(c, s, ackIDs...) | |
| 64 }, r.C) | |
| 65 } | |
| 66 | |
| 67 func (r *Retry) retryIter() retry.Iterator { | |
| 68 var it retry.Iterator | |
| 69 if r.G != nil { | |
| 70 it = r.G() | |
| 71 } else { | |
| 72 it = retry.Default() | |
| 73 } | |
| 74 return retry.TransientOnly(it) | |
| 75 } | |
| OLD | NEW |