Skip to content

Commit

Permalink
WIP make proxy timeout configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Nov 26, 2018
1 parent 7515d8d commit 795f3c5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/lib/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ impl std::fmt::Display for ProgramStartError {
ProgramStartError::ConfigCliError(ConfigError::UrlInvalidPort) => {
write!(f, "{}", ConfigError::UrlInvalidPort)
}
ProgramStartError::ConfigCliError(ConfigError::TimeoutInvalid) => {
write!(f, "{}", ConfigError::TimeoutInvalid)
}
ProgramStartError::ConfigFileOpen => write!(f, "config file not found"),
ProgramStartError::ConfigFileRead => write!(f, "config file content could not be read"),
ProgramStartError::FromFile(e) => write!(f, "{}", e),
Expand Down
52 changes: 51 additions & 1 deletion src/lib/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct ProgramOptions {
pub port: u16,
pub config_file: Option<String>,
pub seed_file: Option<String>,
pub proxy_timeout_secs: u16,
}

impl ProgramOptions {
Expand All @@ -57,6 +58,12 @@ impl ProgramOptions {
{
let matches = ClapApp::new("bs-rust")
.arg(Arg::with_name("url").required(true))
.arg(
Arg::with_name("proxy_timeout_secs")
.short("t")
.long("proxy_timeout_secs")
.takes_value(true),
)
.arg(
Arg::with_name("port")
.short("p")
Expand Down Expand Up @@ -86,9 +93,16 @@ impl ProgramOptions {
.parse()
.map_err(|_e| ProgramStartError::ConfigCliError(ConfigError::UrlInvalidPort))?;

let proxy_timeout_secs: u16 = matches
.value_of("proxy_timeout_secs")
.unwrap_or("5")
.parse()
.map_err(|_e| ProgramStartError::ConfigCliError(ConfigError::TimeoutInvalid))?;

let outgoing_opts = ProgramOptions::new(host, scheme)
.with_port(port)
.with_seed_file(matches.value_of("seed"));
.with_seed_file(matches.value_of("seed"))
.with_proxy_timeout_secs(proxy_timeout_secs);

let outgoing_opts = match matches.value_of("config") {
Some(cfg_file) => outgoing_opts.with_config_file(cfg_file),
Expand All @@ -111,6 +125,10 @@ impl ProgramOptions {
});
self
}
pub fn with_proxy_timeout_secs(mut self, timeout: u16) -> ProgramOptions {
self.proxy_timeout_secs = timeout;
self
}
}

impl Default for ProgramOptions {
Expand All @@ -121,6 +139,7 @@ impl Default for ProgramOptions {
port: 0,
config_file: None,
seed_file: None,
proxy_timeout_secs: 5
}
}
}
Expand All @@ -131,6 +150,7 @@ pub enum ConfigError {
UrlInvalidHost,
UrlInvalidPort,
UrlInvalidScheme,
TimeoutInvalid,
}

impl fmt::Display for ConfigError {
Expand All @@ -144,6 +164,9 @@ impl fmt::Display for ConfigError {
),
ConfigError::UrlInvalidScheme => {
write!(f, "Could not retrieve the scheme from the URL")
},
ConfigError::TimeoutInvalid => {
write!(f, "Invalid format for timeout. Please provide a number of seconds, eg: 3")
}
}
}
Expand Down Expand Up @@ -180,6 +203,33 @@ mod tests {
port: 9000,
config_file: Some("test/fixtures/config.yml".into()),
seed_file: None,
proxy_timeout_secs: 5
}
);
}

#[test]
fn test_from_vec_with_timeout() {
let args = vec![
"/bin/fake-program",
"https://example.com",
"--port",
"9000",
"--config",
"test/fixtures/config.yml",
"--proxy_timeout_secs",
"2",
];
let p = ProgramOptions::from_args(args).unwrap();
assert_eq!(
p,
ProgramOptions {
target: "example.com".to_string(),
scheme: ProxyScheme::Https,
port: 9000,
config_file: Some("test/fixtures/config.yml".into()),
seed_file: None,
proxy_timeout_secs: 2
}
);
}
Expand Down

2 comments on commit 795f3c5

@shakyShane
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc: @Januszpl

@shakyShane
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just needs plugging into the place you added the timeout

Please sign in to comment.