Skip to content

Commit

Permalink
Make http retries and http connect timeout configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
2bc4 committed Dec 30, 2023
1 parent 3b61297 commit 142bd4c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Options:
-q, --quiet
Silences the player's output
--max-retries <COUNT>
Attempt to fetch the media playlist <COUNT> times before exiting (default: 50)
Attempt to fetch the media playlist <COUNT> times before exiting [default: 50]
--passthrough
Print the playlist URL to stdout and exit
--client-id <ID>
Expand All @@ -39,6 +39,10 @@ Options:
--never-proxy <CHANNEL>
Prevent specified channels from using a playlist proxy.
Can be multiple comma separated channels.
--http-retries <COUNT>
Attempt HTTP requests <COUNT> times before giving up [default: 3]
--http-connect-timeout <SECONDS>
HTTP connect timeout in seconds [default: 5]
-h, --help
Print help
-V, --version
Expand Down
26 changes: 17 additions & 9 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ pub struct Args {
pub player: String,
pub player_args: String,
pub debug: bool,
pub quiet: bool,
pub max_retries: u32,
pub passthrough: bool,
pub client_id: Option<String>,
pub auth_token: Option<String>,
pub never_proxy: Option<Vec<String>>,
pub http_retries: u32,
pub http_connect_timeout: u64,
pub channel: String,
pub quality: String,
pub quiet: bool,
}

impl Default for Args {
Expand All @@ -29,14 +31,16 @@ impl Default for Args {
player: String::default(),
player_args: String::from("-"),
debug: bool::default(),
quiet: bool::default(),
max_retries: 50,
passthrough: bool::default(),
client_id: Option::default(),
auth_token: Option::default(),
never_proxy: Option::default(),
http_retries: 3,
http_connect_timeout: 5,
channel: String::default(),
quality: String::default(),
quiet: bool::default(),
}
}
}
Expand Down Expand Up @@ -92,13 +96,15 @@ impl Args {
"player" => self.player = split.1.into(),
"player-args" => self.player_args = split.1.into(),
"debug" => self.debug = split.1.parse()?,
"quiet" => self.quiet = split.1.parse()?,
"max-retries" => self.max_retries = split.1.parse()?,
"passthrough" => self.passthrough = split.1.parse()?,
"client-id" => self.client_id = Some(split.1.into()),
"auth-token" => self.auth_token = Some(split.1.into()),
"never-proxy" => self.never_proxy = Some(split_comma(split.1)?),
"http-retries" => self.http_retries = split.1.parse()?,
"http-connect-timeout" => self.http_connect_timeout = split.1.parse()?,
"quality" => self.quality = split.1.into(),
"quiet" => self.quiet = split.1.parse()?,
_ => bail!("Unknown key in config: {}", split.0),
}
} else {
Expand All @@ -113,6 +119,8 @@ impl Args {
merge_opt::<String>(&mut self.player, parser.opt_value_from_str("-p")?);
merge_opt::<String>(&mut self.player_args, parser.opt_value_from_str("-a")?);
merge_opt::<u32>(&mut self.max_retries, parser.opt_value_from_str("--max-retries")?);
merge_opt::<u32>(&mut self.http_retries, parser.opt_value_from_str("--http-retries")?);
merge_opt::<u64>(&mut self.http_connect_timeout, parser.opt_value_from_str("--http-connect-timeout")?);

merge_opt_opt::<String>(&mut self.client_id, parser.opt_value_from_str("--client-id")?);
merge_opt_opt::<String>(&mut self.auth_token, parser.opt_value_from_str("--auth-token")?);
Expand Down Expand Up @@ -158,18 +166,18 @@ fn merge_opt<T>(dst: &mut T, val: Option<T>) {
}
}

fn merge_switch(dst: &mut bool, val: bool) {
if val {
*dst = true;
}
}

fn merge_opt_opt<T>(dst: &mut Option<T>, val: Option<T>) {
if val.is_some() {
*dst = val;
}
}

fn merge_switch(dst: &mut bool, val: bool) {
if val {
*dst = true;
}
}

#[allow(clippy::unnecessary_wraps)] //function pointer
fn split_comma(arg: &str) -> Result<Vec<String>> {
Ok(arg.split(',').map(String::from).collect())
Expand Down
3 changes: 0 additions & 3 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@ 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;
pub const HTTP_CONNECT_TIMEOUT_SECS: u64 = 5;
13 changes: 7 additions & 6 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use curl::easy::{Easy2, Handler, InfoType, List, WriteError};
use log::debug;
use url::Url;

use crate::constants;
use crate::{constants, ARGS};

#[derive(Debug)]
pub enum Error {
Expand All @@ -28,8 +28,10 @@ impl fmt::Display for Error {
}

fn init_curl<T: Write>(handle: &mut Easy2<RequestHandler<T>>, url: &Url) -> Result<()> {
let args = ARGS.get().unwrap();

handle.verbose(log::max_level() == log::LevelFilter::Debug)?;
handle.connect_timeout(Duration::from_secs(constants::HTTP_CONNECT_TIMEOUT_SECS))?;
handle.connect_timeout(Duration::from_secs(args.http_connect_timeout))?;
handle.tcp_nodelay(true)?;
handle.accept_encoding("")?;
handle.useragent(constants::USER_AGENT)?;
Expand All @@ -39,14 +41,13 @@ fn init_curl<T: Write>(handle: &mut Easy2<RequestHandler<T>>, url: &Url) -> Resu
}

fn perform<T: Write>(handle: &Easy2<RequestHandler<T>>) -> Result<()> {
let args = ARGS.get().unwrap();

let mut retries = 0;
loop {
match handle.perform() {
Ok(()) => break,
Err(_) if retries < constants::HTTP_RETRIES => {
retries += 1;
continue;
}
Err(_) if retries < args.http_retries => retries += 1,
Err(e) => return Err(e.into()),
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/usage
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Options:
-q, --quiet
Silences the player's output
--max-retries <COUNT>
Attempt to fetch the media playlist <COUNT> times before exiting (default: 50)
Attempt to fetch the media playlist <COUNT> times before exiting [default: 50]
--passthrough
Print the playlist URL to stdout and exit
--client-id <ID>
Expand All @@ -35,6 +35,10 @@ Options:
--never-proxy <CHANNEL>
Prevent specified channels from using a playlist proxy.
Can be multiple comma separated channels.
--http-retries <COUNT>
Attempt HTTP requests <COUNT> times before giving up [default: 3]
--http-connect-timeout <SECONDS>
HTTP connect timeout in seconds [default: 5]
-h, --help
Print help
-V, --version
Expand Down

0 comments on commit 142bd4c

Please sign in to comment.