Skip to content

Commit

Permalink
Make max retries configurable (#33)
Browse files Browse the repository at this point in the history
* Implement customisable number of retries

* Better name for constant

* Fix rustdoc link

* Better name for option

* Fix rustdoc field name
  • Loading branch information
nick42d authored Aug 4, 2024
1 parent fc3e9d6 commit dd8beae
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1234,3 +1234,6 @@ pub static FORMATS: Lazy<HashMap<&str, StaticFormat>> = Lazy::new(|| {
),
])
});

/// Default max number of retries for a web reqwest.
pub const DEFAULT_MAX_RETRIES: u32 = 3;
8 changes: 5 additions & 3 deletions src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::stream::{LiveStream, LiveStreamOptions};
use crate::structs::FFmpegArgs;

use crate::{
constants::BASE_URL,
constants::{BASE_URL, DEFAULT_MAX_RETRIES},
info_extras::{get_media, get_related_videos},
stream::{NonLiveStream, NonLiveStreamOptions, Stream},
structs::{
Expand Down Expand Up @@ -52,7 +52,7 @@ impl Video {

let retry_policy = ExponentialBackoff::builder()
.retry_bounds(Duration::from_millis(1000), Duration::from_millis(30000))
.build_with_max_retries(3);
.build_with_max_retries(DEFAULT_MAX_RETRIES);
let client = ClientBuilder::new(client)
.with(RetryTransientMiddleware::new_with_policy_and_strategy(
retry_policy,
Expand Down Expand Up @@ -102,9 +102,11 @@ impl Video {
}
};

let max_retries = options.request_options.max_retries.unwrap_or(DEFAULT_MAX_RETRIES);

let retry_policy = ExponentialBackoff::builder()
.retry_bounds(Duration::from_millis(1000), Duration::from_millis(30000))
.build_with_max_retries(3);
.build_with_max_retries(max_retries);
let client = ClientBuilder::new(client)
.with(RetryTransientMiddleware::new_with_policy_and_strategy(
retry_policy,
Expand Down
4 changes: 2 additions & 2 deletions src/stream/streams/live.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::constants::DEFAULT_HEADERS;
use crate::constants::{DEFAULT_HEADERS, DEFAULT_MAX_RETRIES};
use crate::stream::{
encryption::Encryption, media_format::MediaFormat, remote_data::RemoteData, segment::Segment,
streams::Stream,
Expand Down Expand Up @@ -41,7 +41,7 @@ impl LiveStream {
std::time::Duration::from_millis(1000),
std::time::Duration::from_millis(30000),
)
.build_with_max_retries(3);
.build_with_max_retries(DEFAULT_MAX_RETRIES);
reqwest_middleware::ClientBuilder::new(client)
.with(
reqwest_retry::RetryTransientMiddleware::new_with_policy_and_strategy(
Expand Down
4 changes: 2 additions & 2 deletions src/stream/streams/non_live.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::sync::RwLock;

use crate::constants::DEFAULT_HEADERS;
use crate::constants::{DEFAULT_HEADERS, DEFAULT_MAX_RETRIES};
use crate::stream::streams::Stream;
use crate::structs::{CustomRetryableStrategy, VideoError};

Expand Down Expand Up @@ -62,7 +62,7 @@ impl NonLiveStream {
std::time::Duration::from_millis(1000),
std::time::Duration::from_millis(30000),
)
.build_with_max_retries(3);
.build_with_max_retries(DEFAULT_MAX_RETRIES);
reqwest_middleware::ClientBuilder::new(client)
.with(
reqwest_retry::RetryTransientMiddleware::new_with_policy_and_strategy(
Expand Down
15 changes: 15 additions & 0 deletions src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,21 @@ pub struct RequestOptions {
/// };
/// ```
pub ipv6_block: Option<String>,
/// Override the default number of retries to allow per web request (ie, per chunk downloaded)
/// Default is [`crate::constants::DEFAULT_MAX_RETRIES`].
///
/// # Example
/// ```ignore
/// // Allow 5 retries per chunk.
/// let video_options = VideoOptions {
/// request_options: RequestOptions {
/// max_retries: Some(5),
/// ..Default::default()
/// },
/// ..Default::default()
/// };
/// ```
pub max_retries: Option<u32>,
}

#[derive(thiserror::Error, Debug)]
Expand Down

0 comments on commit dd8beae

Please sign in to comment.