This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
forked from armon/relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry.go
344 lines (300 loc) · 8.67 KB
/
retry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package relay
import (
"errors"
"log"
"sync"
"time"
"github.com/WinnerSoftLab/relay/broker"
)
var (
// Error returned when an invalid number of attempts is specified. Protects
// overflows of these values.
errAttemptsRange = errors.New("Attempts must be between 1 and 32")
// Error returned if a zero value is given for min/max wait time.
errWaitRange = errors.New("Min/Max wait times must be positive")
)
// retryBroker implements a resilient AMQP broker which automatically replaces
// the underlying channel if an unexpected error is observed. Due to AMQP's
// persistent connections, this becomes necessary to survive even minor network
// hiccups which may temporarily sever the connection to the server.
//
// The retryBroker does not come without a price, however. Clients are still
// responsible for deduplicating messages or handling them idempotently, as
// duplications are a regular happening in the face of network errors due to
// message redelivery provided by the protocol. It is also important to note
// that acknowledgements - both ack and nack - cannot be retried across
// connection resets. This stems from the fact that a given consumer's channel
// cannot be revived and must be thrown away if the consumer is to be reused.
type retryBroker struct {
broker *relayBroker
attempts int
min time.Duration
max time.Duration
}
// RetryBroker returns a new retrying broker with the given settings.
func (r *Relay) RetryBroker(attempts int, min, max time.Duration) (*retryBroker, error) {
if attempts < 1 || attempts > 32 {
return nil, errAttemptsRange
}
if min == 0 || max == 0 {
return nil, errWaitRange
}
return &retryBroker{&relayBroker{r}, attempts, min, max}, nil
}
// Publisher returns a new retrying broker.Publisher.
func (rb *retryBroker) Publisher(queue string) (broker.Publisher, error) {
return rb.PublisherWithRoutingKey(queue, queue) //keep legacy behaviour
}
// Publisher returns a new retrying broker.Publisher.
func (rb *retryBroker) PublisherWithRoutingKey(queue string, routingKey string) (broker.Publisher, error) {
return &retryPublisher{
broker: rb.broker,
queue: queue,
routingKey: routingKey,
attempts: rb.attempts,
min: rb.min,
max: rb.max,
}, nil
}
// Consumer returns a new retrying broker.Consumer.
func (rb *retryBroker) Consumer(queue string) (broker.Consumer, error) {
return &retryConsumer{
broker: rb.broker,
queue: queue,
attempts: rb.attempts,
min: rb.min,
max: rb.max,
}, nil
}
// Close closes the broker.
func (rb *retryBroker) Close() error {
if rb.broker != nil {
return rb.broker.Close()
}
return nil
}
// retryConsumer tracks the relay broker and an associated consumer, allowing
// the consumer to be replaced transparently in the face of failure.
type retryConsumer struct {
broker broker.Broker
cons broker.Consumer
queue string
attempts int
min time.Duration
max time.Duration
l sync.RWMutex
}
// consumer is used to connect the consumer to the relay queue.
func (rc *retryConsumer) consumer(create bool) (broker.Consumer, error) {
// Check for an existing consumer
rc.l.RLock()
cons := rc.cons
rc.l.RUnlock()
if cons != nil || !create {
return cons, nil
}
// Make a new consumer
rc.l.Lock()
defer rc.l.Unlock()
cons, err := rc.broker.Consumer(rc.queue)
if err != nil {
return nil, err
}
rc.cons = cons
return cons, nil
}
// discard is used to remove a broken consumer.
func (rc *retryConsumer) discard(cons broker.Consumer) {
rc.l.Lock()
defer rc.l.Unlock()
if cons == nil {
return
}
cons.Close()
if cons == rc.cons {
rc.cons = nil
}
}
// Close closes the consumer.
func (rc *retryConsumer) Close() error {
cons, err := rc.consumer(false)
if err != nil || cons == nil {
return err
}
return cons.Close()
}
// Ack marks message(s) as delivered.
func (rc *retryConsumer) Ack() error {
cons, err := rc.consumer(false)
if err != nil || cons == nil {
return err
}
err = cons.Ack()
if err != nil {
log.Printf("[ERR] relay: consumer failed ack: %v", err)
rc.discard(cons)
}
return err
}
// Nack sends message(s) back to the queue.
func (rc *retryConsumer) Nack() error {
cons, err := rc.consumer(false)
if err != nil || cons == nil {
return err
}
err = cons.Nack()
if err != nil {
log.Printf("[ERR] relay: consumer failed nack: %v", err)
rc.discard(cons)
}
return err
}
// ConsumeAck is used to consume with automatic ack. The consume operation
// is able to be retried, but if a message is consumed and the acknowledgement
// fails, the broker will re-deliver the message. Because of this, it is
// critical that the returned error is checked, as it is possible that both
// the output is written and an error is encountered if we see connection
// loss between Consume() and Ack().
func (rc *retryConsumer) ConsumeAck(out interface{}) error {
if err := rc.Consume(out); err != nil {
return err
}
return rc.Ack()
}
// Consume consumes a single message from the queue.
func (rc *retryConsumer) Consume(out interface{}) error {
return rc.ConsumeTimeout(out, -1)
}
// ConsumeTimeout consumes a single message from the queue with an upper bound
// on the time spent waiting.
func (rc *retryConsumer) ConsumeTimeout(out interface{}, timeout time.Duration) error {
// Record the deadline so we can honor the timeout
var deadline time.Time
if timeout >= 0 {
deadline = time.Now().Add(timeout)
}
for i := 0; ; i++ {
cons, err := rc.consumer(true)
if err != nil {
goto RETRY
}
err = cons.ConsumeTimeout(out, timeout)
if err == nil {
break
}
// Respect the case where the consume legitimately times out.
if err == TimedOut {
return err
}
RETRY:
log.Printf("[WARN] relay: consumer got error: %v", err)
rc.discard(cons)
if i == rc.attempts {
log.Printf("[ERR] relay: consumer giving up after %d attempts", i)
return err
}
// Check if we are already passed the deadline
now := time.Now()
if !deadline.IsZero() && now.After(deadline) {
log.Printf("[DEBUG] relay: consumer reached deadline")
return TimedOut
}
// Calculate the next wait period
wait := rc.min * time.Duration(int64(1)<<uint(i))
if wait > rc.max {
wait = rc.max
}
// Ensure we don't wait past the deadline. Adjust the timeout
// so that the next call to consume will return earlier.
if !deadline.IsZero() && now.Add(wait).After(deadline) {
wait = deadline.Sub(now)
}
timeout -= wait
log.Printf("[DEBUG] relay: consumer retrying in %s", wait)
time.Sleep(wait)
}
return nil
}
// retryPublisher wraps a relay broker and an associated publisher, allowing
// the underlying publisher's channel to be automatically swapped out if any
// errors are seen during a publish.
type retryPublisher struct {
broker broker.Broker
pub broker.Publisher
queue string
routingKey string
attempts int
min time.Duration
max time.Duration
l sync.RWMutex
}
// publisher is used to connect the publisher to the relay queue.
func (rp *retryPublisher) publisher(create bool) (broker.Publisher, error) {
// Check for an existing publisher
rp.l.RLock()
pub := rp.pub
rp.l.RUnlock()
if pub != nil || !create {
return pub, nil
}
// Make a new publisher
rp.l.Lock()
defer rp.l.Unlock()
pub, err := rp.broker.PublisherWithRoutingKey(rp.queue, rp.routingKey)
if err != nil {
return nil, err
}
rp.pub = pub
return pub, nil
}
// discard is used to remove a broken publisher.
func (rp *retryPublisher) discard(pub broker.Publisher) {
rp.l.Lock()
defer rp.l.Unlock()
if pub == nil {
return
}
pub.Close()
if pub == rp.pub {
rp.pub = nil
}
}
// Close closes the publisher.
func (rp *retryPublisher) Close() error {
pub, err := rp.publisher(false)
if err != nil || pub == nil {
return err
}
return pub.Close()
}
// Publish publishes a single message to the queue. If an error is encountered,
// the broker automatically tries to replace the underlying channel and submits
// the message again. If publisher confirms are enabled, this may result in the
// same message being published multiple times. If this is not desirable, the
// config struct allows disabling publisher confirms, which may drop messages.
func (rp *retryPublisher) Publish(in interface{}) error {
for i := 0; ; i++ {
pub, err := rp.publisher(true)
if err != nil {
goto RETRY
}
err = pub.Publish(in)
if err == nil {
break
}
RETRY:
log.Printf("[WARN] relay: publisher got error: %v", err)
rp.discard(pub)
if i == rp.attempts {
log.Printf("[ERR] relay: publisher giving up after %d attempts", i)
return err
}
wait := rp.min * (1 << uint(i))
if wait > rp.max {
wait = rp.max
}
log.Printf("[DEBUG] relay: publisher retrying in %s", wait)
time.Sleep(wait)
}
return nil
}