Skip to content

Commit

Permalink
Retry request up to 3 times on curl error
Browse files Browse the repository at this point in the history
Maybe helps with random errors like in #26
  • Loading branch information
2bc4 committed Dec 29, 2023
1 parent 32b069c commit d41f3a1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ pub const TWITCH_HLS_BASE: &str = "https://usher.ttvnw.net/api/channel/hls/";

pub const DEFAULT_CLIENT_ID: &str = "kimne78kx3ncx6brgo4mv6wki5h1ko";
pub const DEFAULT_CONFIG_PATH: &str = "twitch-hls-client/config";

pub const HTTP_RETRIES: u8 = 3;
13 changes: 12 additions & 1 deletion src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,18 @@ fn init_curl<T: Write>(handle: &mut Easy2<RequestHandler<T>>, url: &Url) -> Resu
}

fn perform<T: Write>(handle: &mut Easy2<RequestHandler<T>>) -> Result<()> {
handle.perform()?;
let mut retries = 0;
loop {
match handle.perform() {
Ok(()) => break,
Err(_) if retries < constants::HTTP_RETRIES => {
retries += 1;
continue;
},
Err(e) => return Err(e.into()),
}
}

handle.get_ref().check_error()?;

let code = handle.response_code()?;
Expand Down

0 comments on commit d41f3a1

Please sign in to comment.