reqwest-partial-retry 0.1.1

Wrapper around reqwest to allow for easy partial retries
Documentation
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
//! Wrapper around reqwest to allow for easy partial retries
//!
//! # Example
//!
//! ```
//! use futures_util::StreamExt;
//! use reqwest_partial_retry::ClientExt;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let client = reqwest::Client::new().resumable();
//! let request = client.get("http://httpbin.org/ip").build().unwrap();
//! let mut stream = client
//!     .execute_resumable(request)
//!     .await?
//!     .bytes_stream_resumable();
//!
//! while let Some(item) = stream.next().await {
//!     println!("Bytes: {:?}", item?);
//! }
//! # Ok(())
//! # }
//! ```

#![warn(
    missing_docs,
    clippy::missing_errors_doc,
    clippy::missing_panics_doc,
    clippy::missing_const_for_fn,
    clippy::future_not_send,
    clippy::large_futures
)]

use std::fmt::Display;
use std::future::Future;
use std::ops::Deref;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;

use bytes::Bytes;
use chrono::Utc;
use futures_core::{ready, Stream};
use reqwest::Request;
use reqwest_retry::{DefaultRetryableStrategy, RetryPolicy, Retryable, RetryableStrategy};
use retry_policies::RetryDecision;

/// Config for the resumable [`Client`]
pub struct Config {
    inner: Arc<ConfigInner>,
}

struct ConfigInner {
    stream_timeout: Option<Duration>,
    retry_policy: Box<dyn RetryPolicy + Send + Sync>,
    retryable_strategy: Box<dyn RetryableStrategy + Send + Sync>,
}

impl Clone for Config {
    #[inline]
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

impl Default for Config {
    /// The default config has no stream timeout,
    /// uses an exponential backoff with 10 retries,
    /// and uses the [`DefaultRetryableStrategy`]
    #[inline]
    fn default() -> Self {
        Self {
            inner: Arc::new(ConfigInner {
                stream_timeout: None,
                retry_policy: Box::new(
                    retry_policies::policies::ExponentialBackoffBuilder::default().build_with_max_retries(10),
                ),
                retryable_strategy: Box::new(DefaultRetryableStrategy),
            }),
        }
    }
}

impl Config {
    /// Create a `ConfigBuilder`
    #[inline]
    pub fn builder() -> ConfigBuilder {
        ConfigBuilder::default()
    }

    /// Get the stream timeout
    #[inline]
    pub fn stream_timeout(&self) -> &Option<Duration> {
        &self.inner.stream_timeout
    }

    /// Get the retry policy
    #[inline]
    pub fn retry_policy(&self) -> &(dyn RetryPolicy + Send + Sync) {
        self.inner.retry_policy.as_ref()
    }

    /// Get the retryable strategy
    #[inline]
    pub fn retryable_strategy(&self) -> &(dyn RetryableStrategy + Send + Sync) {
        self.inner.retryable_strategy.as_ref()
    }
}

/// ConfigBuilder for the [`Config`] of the resumable [`Client`]
pub struct ConfigBuilder {
    stream_timeout: Option<Duration>,
    retry_policy: Box<dyn RetryPolicy + Send + Sync>,
    retryable_strategy: Box<dyn RetryableStrategy + Send + Sync>,
}

impl Default for ConfigBuilder {
    /// The default config builder has no stream timeout,
    /// uses an exponential backoff with 10 retries,
    /// and uses the [`DefaultRetryableStrategy`]
    #[inline]
    fn default() -> Self {
        Self {
            stream_timeout: None,
            retry_policy: Box::new(
                retry_policies::policies::ExponentialBackoffBuilder::default().build_with_max_retries(10),
            ),
            retryable_strategy: Box::new(DefaultRetryableStrategy),
        }
    }
}

impl ConfigBuilder {
    /// Set the timeout for the
    /// [`bytes_stream_resumable`](ResumableResponse::bytes_stream_resumable).
    ///
    /// If no new data has been received for the specified duration, it times out,
    /// and depending on the `RetryPolicy` and `RetryableStrategy` a retry is tried.
    ///
    /// If the value is `None`, it will never time out.
    #[inline]
    pub const fn stream_timeout(mut self, stream_timeout: Option<Duration>) -> Self {
        self.stream_timeout = stream_timeout;
        self
    }

    /// Set the retry policy.
    ///
    /// Retries are counted in [`execute_resumable`](Client::execute_resumable)
    /// and [`bytes_stream_resumable`](ResumableResponse::bytes_stream_resumable),
    /// and will be reset whenever new data has been received.
    #[inline]
    pub fn retry_policy<P: RetryPolicy + Send + Sync + 'static>(mut self, retry_policy: P) -> Self {
        self.retry_policy = Box::new(retry_policy);
        self
    }

    /// Set the retryable strategy.
    #[inline]
    pub fn retryable_strategy<S: RetryableStrategy + Send + Sync + 'static>(mut self, retryable_strategy: S) -> Self {
        self.retryable_strategy = Box::new(retryable_strategy);
        self
    }

    /// Build a `Config`
    #[inline]
    pub fn build(self) -> Config {
        let Self {
            stream_timeout,
            retry_policy,
            retryable_strategy,
        } = self;

        Config {
            inner: Arc::new(ConfigInner {
                stream_timeout,
                retry_policy,
                retryable_strategy,
            }),
        }
    }
}

/// Extension to [`reqwest::Client`] that provides methods to convert it into a resumable [`Client`]
pub trait ClientExt {
    /// Convert a [`reqwest::Client`] into a
    /// [`reqwest_partial_retry::Client`](Client)
    fn resumable(self) -> Client;

    /// Convert a [`reqwest::Client`] into a
    /// [`reqwest_partial_retry::Client`](Client) with a config
    fn resumable_with_config(self, config: Config) -> Client;
}

impl ClientExt for reqwest::Client {
    #[inline]
    fn resumable(self) -> Client {
        Client {
            client: self,
            config: Config::default(),
        }
    }

    #[inline]
    fn resumable_with_config(self, config: Config) -> Client {
        Client { client: self, config }
    }
}

/// A wrapper for [`reqwest::Client`] to allow for resumable byte streams
pub struct Client {
    client: reqwest::Client,
    config: Config,
}

impl Clone for Client {
    #[inline]
    fn clone(&self) -> Self {
        Self {
            client: self.client.clone(),
            config: self.config.clone(),
        }
    }
}

impl Deref for Client {
    type Target = reqwest::Client;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.client
    }
}

impl Client {
    /// Executes a `Request`.
    ///
    /// A `Request` can be built manually with `Request::new()` or obtained
    /// from a RequestBuilder with `RequestBuilder::build()`.
    ///
    /// # Errors
    ///
    /// This method fails if the `Request` is not cloneable (i.e. if the body is
    /// a stream).
    ///
    /// It also fails if the [`RetryableStrategy`] decides that the error is fatal,
    /// or it is transient and the retry limit has been reached.
    pub async fn execute_resumable(&self, request: Request) -> Result<ResumableResponse, Error> {
        let mut current_retries = 0;

        'outer_loop: loop {
            let Some(cloned_request) = request.try_clone() else {
                return Err(Error::RequestNotCloneable);
            };

            let response = self
                .client
                .execute(cloned_request)
                .await
                .map_err(reqwest_middleware::Error::Reqwest);

            match (self.config.inner.retryable_strategy.handle(&response), response) {
                (Some(Retryable::Fatal), response) => {
                    let reqwest_error = if let Err(reqwest_middleware::Error::Reqwest(err)) = response {
                        Some(err)
                    } else {
                        None
                    };

                    return Err(Error::UnresolvableError(reqwest_error));
                }
                (None, Ok(response)) => {
                    let headers = hyperx::Headers::from(response.headers());
                    let accept_byte_ranges = if let Some(hyperx::header::AcceptRanges(ranges)) = headers.get() {
                        ranges.iter().any(|u| *u == hyperx::header::RangeUnit::Bytes)
                    } else {
                        false
                    };

                    return Ok(ResumableResponse {
                        client: self.clone(),
                        request,
                        response,
                        accept_byte_ranges,
                    });
                }
                (Some(Retryable::Transient), response) | (None, response @ Err(_)) => {
                    let retry_decision = self.config.inner.retry_policy.should_retry(current_retries);
                    current_retries += 1;

                    match retry_decision {
                        RetryDecision::Retry { execute_after } => {
                            if let Ok(duration) = (execute_after - Utc::now()).to_std() {
                                tokio::time::sleep(duration).await;
                            }

                            continue 'outer_loop;
                        }
                        RetryDecision::DoNotRetry => {
                            let reqwest_error = if let Err(reqwest_middleware::Error::Reqwest(err)) = response {
                                Some(RetryError::RequestError(err))
                            } else {
                                None
                            };

                            return Err(Error::MaxRetries(reqwest_error));
                        }
                    }
                }
            }
        }
    }
}

/// A wrapper for [`reqwest::Response`] to allow for resumable byte streams
pub struct ResumableResponse {
    client: Client,
    request: reqwest::Request,
    response: reqwest::Response,
    accept_byte_ranges: bool,
}

impl Deref for ResumableResponse {
    type Target = reqwest::Response;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.response
    }
}

impl ResumableResponse {
    /// Get the underlying [`reqwest::Response`]
    #[inline]
    pub fn response(self) -> reqwest::Response {
        self.response
    }

    /// Convert the response into a `Stream` of `Bytes` from the body.
    ///
    /// # Example
    ///
    /// ```
    /// use futures_util::StreamExt;
    /// use reqwest_partial_retry::ClientExt;
    ///
    /// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = reqwest::Client::new().resumable();
    /// let request = client.get("http://httpbin.org/ip").build().unwrap();
    /// let mut stream = client
    ///     .execute_resumable(request)
    ///     .await?
    ///     .bytes_stream_resumable();
    ///
    /// while let Some(item) = stream.next().await {
    ///     println!("Bytes: {:?}", item?);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    #[inline]
    pub fn bytes_stream_resumable(self) -> impl Stream<Item = Result<Bytes, Error>> + Send + Unpin {
        ResumableBytesStream {
            client: self.client,
            request: self.request,
            accept_byte_ranges: self.accept_byte_ranges,
            pos: 0,
            read_bytes: 0,
            current_retries: 0,
            stream: Box::pin(self.response.bytes_stream()),
            next_request: None,
            sleep: Box::pin(tokio::time::sleep(Duration::ZERO)),
            run_sleep: false,
            stream_sleep: Box::pin(tokio::time::sleep(Duration::ZERO)),
            tried_stream_poll: false,
        }
    }
}

struct ResumableBytesStream {
    client: Client,
    request: reqwest::Request,
    accept_byte_ranges: bool,
    pos: u64,
    read_bytes: u64,
    current_retries: u32,
    stream: Pin<Box<dyn Stream<Item = reqwest::Result<Bytes>> + Send>>,
    next_request: Option<Pin<Box<dyn Future<Output = Result<reqwest::Response, reqwest::Error>> + Send>>>,
    sleep: Pin<Box<tokio::time::Sleep>>,
    run_sleep: bool,
    stream_sleep: Pin<Box<tokio::time::Sleep>>,
    tried_stream_poll: bool,
}

/// The errors that may occur
#[derive(thiserror::Error, Debug)]
pub enum Error {
    /// The error was unresolvable
    UnresolvableError(Option<reqwest::Error>),
    /// The maximum retry limit has been reached
    MaxRetries(Option<RetryError>),
    /// The request is not cloneable
    RequestNotCloneable,
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::UnresolvableError(err) => {
                if let Some(err) = err {
                    write!(f, "encountered fatal error: {err}")
                } else {
                    write!(f, "encountered fatal error")
                }
            }
            Error::MaxRetries(err) => {
                if let Some(err) = err {
                    write!(f, "maximum amount of retries reached: {err}")
                } else {
                    write!(f, "maximum amount of retries reached")
                }
            }
            Error::RequestNotCloneable => write!(f, "request is not cloneable"),
        }
    }
}

/// The error that possibly caused the retry
#[derive(thiserror::Error, Debug)]
pub enum RetryError {
    /// A request error
    #[error("request error: {0}")]
    RequestError(reqwest::Error),
    /// Requested range is not satisfiable
    #[error("requested range is not satisfiable")]
    RangeNotSatisfiable,
    /// A stream error
    #[error("stream error: {0}")]
    StreamError(reqwest::Error),
    /// The stream timed out
    #[error("stream timed out")]
    StreamTimeout,
}

impl Stream for ResumableBytesStream {
    type Item = Result<Bytes, Error>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
        macro_rules! prepare_retry {
            ($retry_after:expr $(,)?) => {
                if let Ok(duration) = ($retry_after - Utc::now()).to_std() {
                    self.sleep.as_mut().reset(tokio::time::Instant::now() + duration);
                    self.run_sleep = true;
                }

                let Some(mut cloned_request) = self.request.try_clone() else {
                    return Poll::Ready(Some(Err(Error::RequestNotCloneable)));
                };

                if self.accept_byte_ranges {
                    let range_value = reqwest::header::HeaderValue::from_str(
                        &hyperx::header::Range::Bytes(vec![hyperx::header::ByteRangeSpec::AllFrom(self.pos)])
                            .to_string(),
                    )
                    .unwrap();
                    let _ = cloned_request
                        .headers_mut()
                        .insert(reqwest::header::RANGE, range_value);
                }

                self.next_request = Some(Box::pin(self.client.client.execute(cloned_request)));
            };
        }

        'outer_loop: loop {
            if self.run_sleep {
                ready!(self.sleep.as_mut().poll(cx));
                self.run_sleep = false;
            }

            if let Some(next_request) = &mut self.next_request {
                let response = ready!(next_request.as_mut().poll(cx)).map_err(reqwest_middleware::Error::Reqwest);
                self.next_request = None;

                if let Ok(response) = &response {
                    if matches!(
                        response.status(),
                        reqwest::StatusCode::OK | reqwest::StatusCode::RANGE_NOT_SATISFIABLE
                    ) {
                        self.accept_byte_ranges = false;
                    }
                }

                match (self.client.config.inner.retryable_strategy.handle(&response), response) {
                    (Some(Retryable::Fatal), response) => {
                        let reqwest_error = if let Err(reqwest_middleware::Error::Reqwest(err)) = response {
                            Some(err)
                        } else {
                            None
                        };

                        return Poll::Ready(Some(Err(Error::UnresolvableError(reqwest_error))));
                    }
                    (None, Ok(response)) => {
                        if !self.accept_byte_ranges {
                            self.pos = 0;
                        }

                        if response.status() == reqwest::StatusCode::RANGE_NOT_SATISFIABLE {
                            let retry_decision =
                                self.client.config.inner.retry_policy.should_retry(self.current_retries);
                            self.current_retries += 1;

                            match retry_decision {
                                RetryDecision::Retry { execute_after } => {
                                    prepare_retry!(execute_after);
                                    continue 'outer_loop;
                                }
                                RetryDecision::DoNotRetry => {
                                    return Poll::Ready(Some(Err(Error::MaxRetries(Some(
                                        RetryError::RangeNotSatisfiable,
                                    )))));
                                }
                            }
                        }

                        self.stream = Box::pin(response.bytes_stream());
                        self.current_retries = 0;

                        if let Some(timeout_duration) = self.client.config.inner.stream_timeout {
                            let stream_sleep_deadline = tokio::time::Instant::now() + timeout_duration;
                            self.stream_sleep.as_mut().reset(stream_sleep_deadline);
                        }
                    }
                    (Some(Retryable::Transient), response) | (None, response @ Err(_)) => {
                        let retry_decision = self.client.config.inner.retry_policy.should_retry(self.current_retries);
                        self.current_retries += 1;

                        match retry_decision {
                            RetryDecision::Retry { execute_after } => {
                                prepare_retry!(execute_after);
                                continue 'outer_loop;
                            }
                            RetryDecision::DoNotRetry => {
                                let reqwest_error = if let Err(reqwest_middleware::Error::Reqwest(err)) = response {
                                    Some(RetryError::RequestError(err))
                                } else {
                                    None
                                };

                                return Poll::Ready(Some(Err(Error::MaxRetries(reqwest_error))));
                            }
                        }
                    }
                }
            }

            if !self.tried_stream_poll {
                self.tried_stream_poll = true;

                if let Some(timeout_duration) = self.client.config.inner.stream_timeout {
                    let stream_sleep_deadline = tokio::time::Instant::now() + timeout_duration;
                    self.stream_sleep.as_mut().reset(stream_sleep_deadline);
                }
            }

            'stream_loop: loop {
                match self.stream.as_mut().poll_next(cx) {
                    Poll::Ready(Some(Err(err))) => {
                        let retry_decision = self.client.config.inner.retry_policy.should_retry(self.current_retries);
                        self.current_retries += 1;

                        match retry_decision {
                            RetryDecision::Retry { execute_after } => {
                                prepare_retry!(execute_after);
                                continue 'outer_loop;
                            }
                            RetryDecision::DoNotRetry => {
                                return Poll::Ready(Some(Err(Error::MaxRetries(Some(RetryError::StreamError(err))))));
                            }
                        }
                    }
                    Poll::Ready(Some(Ok(bytes))) => {
                        if !bytes.is_empty() {
                            self.current_retries = 0;

                            if let Some(timeout_duration) = self.client.config.inner.stream_timeout {
                                let stream_sleep_deadline = tokio::time::Instant::now() + timeout_duration;
                                self.stream_sleep.as_mut().reset(stream_sleep_deadline);
                            }

                            let start_pos = self.pos;
                            self.pos += bytes.len() as u64;

                            if self.pos > self.read_bytes {
                                let diff_read = (self.read_bytes - start_pos) as usize;
                                self.read_bytes = self.pos;
                                return Poll::Ready(Some(Ok(bytes.slice(diff_read..))));
                            }
                        }

                        continue 'stream_loop;
                    }
                    Poll::Ready(None) => return Poll::Ready(None),
                    Poll::Pending => {
                        if self.client.config.inner.stream_timeout.is_some() {
                            ready!(self.stream_sleep.as_mut().poll(cx));

                            let retry_decision =
                                self.client.config.inner.retry_policy.should_retry(self.current_retries);
                            self.current_retries += 1;

                            match retry_decision {
                                RetryDecision::Retry { execute_after } => {
                                    prepare_retry!(execute_after);
                                    continue 'outer_loop;
                                }
                                RetryDecision::DoNotRetry => {
                                    return Poll::Ready(Some(Err(Error::MaxRetries(Some(RetryError::StreamTimeout)))));
                                }
                            }
                        }

                        return Poll::Pending;
                    }
                }
            }
        }
    }
}