-
Notifications
You must be signed in to change notification settings - Fork 7
/
retry.rs
872 lines (773 loc) · 28.1 KB
/
retry.rs
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
// Copyright 2020 Palantir Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::errors::{RemoteError, ThrottledError, UnavailableError};
use crate::raw::Service;
use crate::raw::{BodyError, RawBody};
use crate::rng::ConjureRng;
use crate::service::map_error::RawClientError;
use crate::service::Layer;
use crate::util::spans::{self, HttpSpanFuture};
use crate::{builder, BodyWriter, Builder, Idempotency};
use conjure_error::{Error, ErrorKind};
use conjure_http::client::{AsyncRequestBody, AsyncWriteBody, BoxAsyncWriteBody, Endpoint};
use futures::future;
use http::request::Parts;
use http::{Request, Response, StatusCode};
use rand::Rng;
use std::error;
use std::future::Future;
use std::pin::Pin;
use tokio::time::{self, Duration};
use witchcraft_log::info;
/// A layer which retries failed requests in certain cases.
///
/// All requests that fail with a cause of `ThrottledError` or `UnavailableError` will be retried. Requests failing with
/// a cause of `RawClientError` or `RemoteError` and a status code of `INTERNAL_SERVER_ERROR` will be retried if the
/// request is considered idempotent. This is configured by the `Idempotency` enum.
///
/// The layer retries up to a specified maximum number of times, applying exponential backoff with full jitter in
/// between each attempt.
///
/// Due to https://github.com/rust-lang/rust/issues/71462, this layer also converts the Conjure `Body` into an
/// `http_body::Body`, but this should be factored out to a separate layer when the compiler bug has been fixed.
pub struct RetryLayer {
idempotency: Idempotency,
max_num_retries: u32,
backoff_slot_size: Duration,
rng: ConjureRng,
}
impl RetryLayer {
pub fn new<T>(builder: &Builder<builder::Complete<T>>) -> RetryLayer {
RetryLayer {
idempotency: builder.get_idempotency(),
max_num_retries: if builder.mesh_mode() {
0
} else {
builder.get_max_num_retries()
},
backoff_slot_size: builder.get_backoff_slot_size(),
rng: ConjureRng::new(builder),
}
}
}
impl<S> Layer<S> for RetryLayer {
type Service = RetryService<S>;
fn layer(self, inner: S) -> Self::Service {
RetryService {
inner,
idempotency: self.idempotency,
max_num_retries: self.max_num_retries,
backoff_slot_size: self.backoff_slot_size,
rng: self.rng,
}
}
}
pub struct RetryService<S> {
inner: S,
idempotency: Idempotency,
max_num_retries: u32,
backoff_slot_size: Duration,
rng: ConjureRng,
}
impl<'a, S, B> Service<Request<AsyncRequestBody<'a, BodyWriter>>> for RetryService<S>
where
S: Service<Request<RawBody>, Response = Response<B>, Error = Error> + 'a + Sync + Send,
S::Response: Send,
B: 'static,
{
type Response = S::Response;
type Error = S::Error;
fn call(
&self,
req: Request<AsyncRequestBody<'a, BodyWriter>>,
) -> impl Future<Output = Result<Self::Response, Self::Error>> {
let idempotent = match self.idempotency {
Idempotency::Always => true,
Idempotency::ByMethod => req.method().is_idempotent(),
Idempotency::Never => false,
};
let state = State {
service: self,
idempotent,
attempt: 0,
};
state.call(req)
}
}
struct State<'a, S> {
service: &'a RetryService<S>,
idempotent: bool,
attempt: u32,
}
impl<S, B> State<'_, S>
where
S: Service<Request<RawBody>, Response = Response<B>, Error = Error>,
{
async fn call(
mut self,
req: Request<AsyncRequestBody<'_, BodyWriter>>,
) -> Result<S::Response, Error> {
let (parts, mut body) = req.into_parts();
loop {
let mut tracked = None;
let body: AsyncRequestBody<'_, BodyWriter> = match &mut body {
AsyncRequestBody::Empty => AsyncRequestBody::Empty,
AsyncRequestBody::Fixed(bytes) => AsyncRequestBody::Fixed(bytes.clone()),
AsyncRequestBody::Streaming(writer) => {
let body =
BoxAsyncWriteBody::new(tracked.insert(ResetTracker::new(writer)).writer());
AsyncRequestBody::Streaming(body)
}
};
let attempt_req = self.clone_request(&parts, body);
let (error, retry_after) = match self.send_attempt(attempt_req).await? {
AttemptOutcome::Ok(response) => return Ok(response),
AttemptOutcome::Retry { error, retry_after } => (error, retry_after),
};
self.prepare_for_retry(tracked.as_mut(), error, retry_after)
.await?;
}
}
// Request.extensions isn't clone, so we need to handle this manually
fn clone_request<'a>(
&self,
parts: &Parts,
body: AsyncRequestBody<'a, BodyWriter>,
) -> Request<AsyncRequestBody<'a, BodyWriter>> {
let mut new_req = Request::new(());
*new_req.method_mut() = parts.method.clone();
*new_req.uri_mut() = parts.uri.clone();
*new_req.headers_mut() = parts.headers.clone();
if let Some(endpoint) = parts.extensions.get::<Endpoint>() {
new_req.extensions_mut().insert(endpoint.clone());
}
let parts = new_req.into_parts().0;
Request::from_parts(parts, body)
}
async fn send_attempt(
&mut self,
req: Request<AsyncRequestBody<'_, BodyWriter>>,
) -> Result<AttemptOutcome<S::Response>, Error> {
let mut span = zipkin::next_span()
.with_name("conjure-runtime: attempt")
.with_tag("failures", &self.attempt.to_string())
.detach();
spans::add_request_tags(&mut span, &req);
match HttpSpanFuture::new(self.send_raw(req), span).await {
Ok(response) => Ok(AttemptOutcome::Ok(response)),
Err(error) => {
// we don't want to retry after propagated QoS errors
match error.kind() {
ErrorKind::Service(_) => {}
_ => return Err(error),
}
#[allow(clippy::if_same_then_else)] // conditionals get too crazy if combined
if let Some(throttled) = error.cause().downcast_ref::<ThrottledError>() {
Ok(AttemptOutcome::Retry {
retry_after: throttled.retry_after,
error,
})
} else if error.cause().is::<UnavailableError>() {
Ok(AttemptOutcome::Retry {
error,
retry_after: None,
})
} else if self.idempotent && error.cause().is::<RawClientError>() {
Ok(AttemptOutcome::Retry {
error,
retry_after: None,
})
} else if self.idempotent
&& error
.cause()
.downcast_ref::<RemoteError>()
.map_or(false, |e| *e.status() == StatusCode::INTERNAL_SERVER_ERROR)
{
Ok(AttemptOutcome::Retry {
error,
retry_after: None,
})
} else {
Err(error)
}
}
}
}
// As noted above, this should be a separate layer!
async fn send_raw(
&mut self,
req: Request<AsyncRequestBody<'_, BodyWriter>>,
) -> Result<S::Response, Error> {
let (parts, body) = req.into_parts();
let (body, writer) = RawBody::new(body);
let req = Request::from_parts(parts, body);
let (body_result, response_result) =
future::join(writer.write(), self.service.inner.call(req)).await;
match (body_result, response_result) {
(Ok(()), Ok(response)) => Ok(response),
(Ok(()), Err(e)) => Err(e),
(Err(e), Ok(response)) => {
info!(
"body write reported an error on a successful request",
error: e,
);
Ok(response)
}
(Err(body), Err(hyper)) => Err(self.deconflict_errors(body, hyper)),
}
}
// An error in the body write will cause an error on the hyper side, and vice versa. To pick the right one, we see
// if the hyper error was due to the body write aborting or not.
fn deconflict_errors(&self, body_error: Error, hyper_error: Error) -> Error {
let mut cause: &(dyn error::Error + 'static) = hyper_error.cause();
loop {
if cause.is::<BodyError>() {
return body_error;
}
cause = match cause.source() {
Some(cause) => cause,
None => return hyper_error,
};
}
}
async fn prepare_for_retry(
&mut self,
body: Option<&mut ResetTracker<'_, '_>>,
error: Error,
retry_after: Option<Duration>,
) -> Result<(), Error> {
self.attempt += 1;
if self.attempt > self.service.max_num_retries {
info!("exceeded retry limits");
return Err(error);
}
if let Some(body) = body {
let needs_reset = body.needs_reset;
if needs_reset && !Pin::new(&mut *body.body_writer).reset().await {
info!("unable to reset body when retrying request");
return Err(error);
}
}
let backoff = match retry_after {
Some(backoff) => backoff,
None => {
let scale = 1 << (self.attempt - 1);
let max = self.service.backoff_slot_size * scale;
// gen_range panics when min == max
if max == Duration::from_secs(0) {
Duration::from_secs(0)
} else {
self.service
.rng
.with(|rng| rng.gen_range(Duration::from_secs(0)..max))
}
}
};
let _span = zipkin::next_span()
.with_name("conjure-runtime: backoff-with-jitter")
.detach();
time::sleep(backoff).await;
Ok(())
}
}
struct ResetTracker<'a, 'b> {
needs_reset: bool,
body_writer: &'a mut BoxAsyncWriteBody<'b, BodyWriter>,
}
impl<'a, 'b> ResetTracker<'a, 'b> {
fn new(body_writer: &'a mut BoxAsyncWriteBody<'b, BodyWriter>) -> Self {
ResetTracker {
needs_reset: false,
body_writer,
}
}
fn writer<'c>(&'c mut self) -> ResetTrackingBodyWriter<'c, 'a, 'b> {
ResetTrackingBodyWriter { tracker: self }
}
}
struct ResetTrackingBodyWriter<'a, 'b, 'c> {
tracker: &'a mut ResetTracker<'b, 'c>,
}
impl AsyncWriteBody<BodyWriter> for ResetTrackingBodyWriter<'_, '_, '_> {
async fn write_body(mut self: Pin<&mut Self>, w: Pin<&mut BodyWriter>) -> Result<(), Error> {
self.tracker.needs_reset = true;
Pin::new(&mut *self.tracker.body_writer).write_body(w).await
}
async fn reset(mut self: Pin<&mut Self>) -> bool {
let ok = Pin::new(&mut *self.tracker.body_writer).reset().await;
if ok {
self.tracker.needs_reset = false;
}
ok
}
}
enum AttemptOutcome<R> {
Ok(R),
Retry {
error: Error,
retry_after: Option<Duration>,
},
}
#[cfg(test)]
mod test {
use super::*;
use crate::raw::RawBodyInner;
use crate::service;
use crate::BodyWriter;
use bytes::Bytes;
use http::Method;
use http_body_util::BodyExt;
use std::pin::pin;
use std::sync::atomic::{AtomicUsize, Ordering};
use tokio::io::AsyncWriteExt;
fn endpoint() -> Endpoint {
Endpoint::new("service", None, "name", "path")
}
#[tokio::test]
async fn no_body() {
let service = RetryLayer::new(&Builder::for_test()).layer(service::service_fn(
|req: Request<RawBody>| async move {
match req.body().inner {
RawBodyInner::Empty => {}
_ => panic!("expected empty body"),
}
Ok(Response::new(()))
},
));
let request = Request::builder()
.extension(endpoint())
.body(AsyncRequestBody::Empty)
.unwrap();
service.call(request).await.unwrap();
}
#[tokio::test]
async fn fixed_size_body() {
let body = "hello world";
let service = RetryLayer::new(&Builder::for_test()).layer(service::service_fn(
|req: Request<RawBody>| async move {
match &req.body().inner {
RawBodyInner::Single(chunk) => assert_eq!(chunk.data_ref().unwrap(), body),
_ => panic!("expected single chunk body"),
}
Ok(Response::new(()))
},
));
let request = Request::builder()
.extension(endpoint())
.body(AsyncRequestBody::Fixed(Bytes::from(body)))
.unwrap();
service.call(request).await.unwrap();
}
struct StreamedBody;
impl AsyncWriteBody<BodyWriter> for StreamedBody {
async fn write_body(
self: Pin<&mut Self>,
mut w: Pin<&mut BodyWriter>,
) -> Result<(), Error> {
w.write_all(b"hello ").await.unwrap();
w.flush().await.unwrap();
w.write_all(b"world").await.unwrap();
Ok(())
}
async fn reset(self: Pin<&mut Self>) -> bool {
false
}
}
#[tokio::test]
async fn streamed_body() {
let service = RetryLayer::new(&Builder::for_test()).layer(service::service_fn(
|req: Request<RawBody>| async move {
match req.body().inner {
RawBodyInner::Stream { .. } => {}
_ => panic!("expected streaming body"),
}
let body = req.into_body().collect().await.unwrap();
assert_eq!(body.to_bytes(), "hello world");
Ok(Response::new(()))
},
));
let request = Request::builder()
.extension(endpoint())
.body(AsyncRequestBody::Streaming(BoxAsyncWriteBody::new(
StreamedBody,
)))
.unwrap();
service.call(request).await.unwrap();
}
struct StreamedInfiniteBody;
impl AsyncWriteBody<BodyWriter> for StreamedInfiniteBody {
async fn write_body(
self: Pin<&mut Self>,
mut w: Pin<&mut BodyWriter>,
) -> Result<(), Error> {
loop {
w.write_all(b"hello").await.map_err(Error::internal_safe)?;
w.flush().await.map_err(Error::internal_safe)?;
}
}
async fn reset(self: Pin<&mut Self>) -> bool {
false
}
}
#[tokio::test]
async fn streamed_body_hangup() {
let service = RetryLayer::new(&Builder::for_test()).layer(service::service_fn(
|req: Request<RawBody>| async move {
let mut body = pin!(req.into_body());
body.frame().await.unwrap().unwrap();
Err::<Response<()>, _>(Error::internal_safe("blammo"))
},
));
let request = Request::builder()
.extension(endpoint())
.body(AsyncRequestBody::Streaming(BoxAsyncWriteBody::new(
StreamedInfiniteBody,
)))
.unwrap();
let err = service.call(request).await.err().unwrap();
assert_eq!(err.cause().to_string(), "blammo");
}
struct StreamedErrorBody;
impl AsyncWriteBody<BodyWriter> for StreamedErrorBody {
async fn write_body(
self: Pin<&mut Self>,
mut w: Pin<&mut BodyWriter>,
) -> Result<(), Error> {
w.write_all(b"hello ").await.unwrap();
w.flush().await.unwrap();
Err(Error::internal_safe("uh oh"))
}
async fn reset(self: Pin<&mut Self>) -> bool {
false
}
}
#[tokio::test]
async fn streamed_body_error() {
let service = RetryLayer::new(&Builder::for_test()).layer(service::service_fn(
|req: Request<RawBody>| async move {
req.into_body()
.collect()
.await
.map_err(Error::internal_safe)?;
Ok(Response::new(()))
},
));
let request = Request::builder()
.extension(endpoint())
.body(AsyncRequestBody::Streaming(BoxAsyncWriteBody::new(
StreamedErrorBody,
)))
.unwrap();
let err = service.call(request).await.err().unwrap();
assert_eq!(err.cause().to_string(), "uh oh");
}
struct RetryingBody {
retries: u32,
needs_reset: bool,
}
impl RetryingBody {
fn new(retries: u32) -> RetryingBody {
RetryingBody {
retries,
needs_reset: false,
}
}
}
impl AsyncWriteBody<BodyWriter> for RetryingBody {
async fn write_body(
mut self: Pin<&mut Self>,
mut w: Pin<&mut BodyWriter>,
) -> Result<(), Error> {
assert!(!self.needs_reset);
self.needs_reset = true;
w.write_all(b"hello ").await.unwrap();
w.flush().await.unwrap();
w.write_all(b"world").await.unwrap();
Ok(())
}
async fn reset(mut self: Pin<&mut Self>) -> bool {
assert!(self.needs_reset);
assert!(self.retries > 0);
self.needs_reset = false;
self.retries -= 1;
true
}
}
#[tokio::test]
async fn retry_after_raw_client_error() {
let service = RetryLayer::new(
&Builder::for_test()
.max_num_retries(2)
.backoff_slot_size(Duration::from_secs(0)),
)
.layer(service::service_fn({
let attempt = AtomicUsize::new(0);
move |req: Request<RawBody>| {
let attempt = attempt.fetch_add(1, Ordering::SeqCst);
async move {
let body = req.into_body().collect().await.unwrap();
assert_eq!(body.to_bytes(), "hello world");
match attempt {
0 => Err(Error::internal_safe(RawClientError("blammo".into()))),
1 => Ok(Response::new(())),
_ => panic!(),
}
}
}
}));
let request = Request::builder()
.extension(endpoint())
.body(AsyncRequestBody::Streaming(BoxAsyncWriteBody::new(
RetryingBody::new(1),
)))
.unwrap();
service.call(request).await.unwrap();
}
#[tokio::test]
async fn retry_after_unavailable() {
let service = RetryLayer::new(
&Builder::for_test()
.max_num_retries(2)
.backoff_slot_size(Duration::from_secs(0)),
)
.layer(service::service_fn({
let attempt = AtomicUsize::new(0);
move |req: Request<RawBody>| {
let attempt = attempt.fetch_add(1, Ordering::SeqCst);
async move {
let body = req.into_body().collect().await.unwrap();
assert_eq!(body.to_bytes(), "hello world");
match attempt {
0 => Err(Error::internal_safe(UnavailableError(()))),
1 => Ok(Response::new(())),
_ => panic!(),
}
}
}
}));
let request = Request::builder()
.extension(endpoint())
.body(AsyncRequestBody::Streaming(BoxAsyncWriteBody::new(
RetryingBody::new(1),
)))
.unwrap();
service.call(request).await.unwrap();
}
#[tokio::test]
async fn retry_after_throttled() {
let service = RetryLayer::new(
&Builder::for_test()
.max_num_retries(2)
.backoff_slot_size(Duration::from_secs(0)),
)
.layer(service::service_fn({
let attempt = AtomicUsize::new(0);
move |req: Request<RawBody>| {
let attempt = attempt.fetch_add(1, Ordering::SeqCst);
async move {
let body = req.into_body().collect().await.unwrap();
assert_eq!(body.to_bytes(), "hello world");
match attempt {
0 => Err(Error::internal_safe(ThrottledError { retry_after: None })),
1 => Ok(Response::new(())),
_ => panic!(),
}
}
}
}));
let request = Request::builder()
.extension(endpoint())
.body(AsyncRequestBody::Streaming(BoxAsyncWriteBody::new(
RetryingBody::new(1),
)))
.unwrap();
service.call(request).await.unwrap();
}
#[tokio::test]
async fn no_retry_after_propagated_unavailable() {
let service = RetryLayer::new(
&Builder::for_test()
.max_num_retries(2)
.backoff_slot_size(Duration::from_secs(0)),
)
.layer(service::service_fn({
let attempt = AtomicUsize::new(0);
move |req: Request<RawBody>| {
let attempt = attempt.fetch_add(1, Ordering::SeqCst);
async move {
let body = req.into_body().collect().await.unwrap();
assert_eq!(body.to_bytes(), "hello world");
match attempt {
0 => Err(Error::throttle_safe(UnavailableError(()))),
1 => Ok(Response::new(())),
_ => panic!(),
}
}
}
}));
let request = Request::builder()
.extension(endpoint())
.body(AsyncRequestBody::Streaming(BoxAsyncWriteBody::new(
RetryingBody::new(0),
)))
.unwrap();
service.call(request).await.err().unwrap();
}
#[tokio::test]
async fn no_retry_after_propagated_throttled() {
let service = RetryLayer::new(
&Builder::for_test()
.max_num_retries(2)
.backoff_slot_size(Duration::from_secs(0)),
)
.layer(service::service_fn({
let attempt = AtomicUsize::new(0);
move |req: Request<RawBody>| {
let attempt = attempt.fetch_add(1, Ordering::SeqCst);
async move {
let body = req.into_body().collect().await.unwrap();
assert_eq!(body.to_bytes(), "hello world");
match attempt {
0 => Err(Error::throttle_safe(ThrottledError { retry_after: None })),
1 => Ok(Response::new(())),
_ => panic!(),
}
}
}
}));
let request = Request::builder()
.extension(endpoint())
.body(AsyncRequestBody::Streaming(BoxAsyncWriteBody::new(
RetryingBody::new(0),
)))
.unwrap();
service.call(request).await.err().unwrap();
}
#[tokio::test]
async fn no_retry_non_idempotent() {
let service = RetryLayer::new(
&Builder::for_test()
.max_num_retries(2)
.backoff_slot_size(Duration::from_secs(0)),
)
.layer(service::service_fn({
let attempt = AtomicUsize::new(0);
move |_| {
let attempt = attempt.fetch_add(1, Ordering::SeqCst);
async move {
match attempt {
0 => Err(Error::internal_safe("blammo")),
1 => Ok(Response::new(())),
_ => panic!(),
}
}
}
}));
let request = Request::builder()
.method(Method::POST)
.extension(endpoint())
.body(AsyncRequestBody::Empty)
.unwrap();
let err = service.call(request).await.err().unwrap();
assert_eq!(err.cause().to_string(), "blammo");
}
#[tokio::test]
async fn retry_non_idempotent_for_qos_errors() {
let service = RetryLayer::new(
&Builder::for_test()
.max_num_retries(2)
.backoff_slot_size(Duration::from_secs(0)),
)
.layer(service::service_fn({
let attempt = AtomicUsize::new(0);
move |_| {
let attempt = attempt.fetch_add(1, Ordering::SeqCst);
async move {
match attempt {
0 => Err(Error::internal_safe(UnavailableError(()))),
1 => Ok(Response::new(())),
_ => panic!(),
}
}
}
}));
let request = Request::builder()
.method(Method::POST)
.extension(endpoint())
.body(AsyncRequestBody::Empty)
.unwrap();
service.call(request).await.unwrap();
}
#[tokio::test]
async fn no_reset_unread_body() {
let service = RetryLayer::new(
&Builder::for_test()
.max_num_retries(2)
.backoff_slot_size(Duration::from_secs(0)),
)
.layer(service::service_fn({
let attempt = AtomicUsize::new(0);
move |req: Request<RawBody>| {
let attempt = attempt.fetch_add(1, Ordering::SeqCst);
async move {
match attempt {
0 => Err(Error::internal_safe(UnavailableError(()))),
1 => {
let body = req.into_body().collect().await.unwrap();
assert_eq!(body.to_bytes(), "hello world");
Ok(Response::new(()))
}
_ => panic!(),
}
}
}
}));
let request = Request::builder()
.extension(endpoint())
.body(AsyncRequestBody::Streaming(BoxAsyncWriteBody::new(
RetryingBody::new(0),
)))
.unwrap();
service.call(request).await.unwrap();
}
#[tokio::test]
async fn give_up_after_limit() {
let service = RetryLayer::new(
&Builder::for_test()
.max_num_retries(1)
.backoff_slot_size(Duration::from_secs(0)),
)
.layer(service::service_fn({
let attempt = AtomicUsize::new(0);
move |req: Request<RawBody>| {
let attempt = attempt.fetch_add(1, Ordering::SeqCst);
async move {
let body = req.into_body().collect().await.unwrap();
assert_eq!(body.to_bytes(), "hello world");
match attempt {
0 | 1 => Err::<Response<()>, _>(Error::internal_safe(UnavailableError(()))),
_ => panic!(),
}
}
}
}));
let request = Request::builder()
.extension(endpoint())
.body(AsyncRequestBody::Streaming(BoxAsyncWriteBody::new(
RetryingBody::new(1),
)))
.unwrap();
service.call(request).await.err().unwrap();
}
}