Skip to content

Commit

Permalink
add --backend-connection-timeout arg
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSim committed Sep 27, 2023
1 parent 11658be commit 300bc04
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ DS encryption proxy.
Usage:
ds_proxy encrypt <input-file> <output-file> [--password-file=<password-file>] [--salt=<salt>] [--chunk-size=<chunk-size>] [--keyring-file=<keyring-file>]
ds_proxy decrypt <input-file> <output-file> [--password-file=<password-file>] [--salt=<salt>] [--chunk-size=<chunk-size>] [--keyring-file=<keyring-file>]
ds_proxy proxy [--address=<address>] [--password-file=<password-file>] [--salt=<salt>] [--chunk-size=<chunk-size>] [--upstream-url=<upstream-url>] [--local-encryption-directory=<local-encryption-directory>] [--keyring-file=<keyring-file>] [--aws-access-key=<aws-access-key>] [--aws-secret-key=<aws-secret-key>] [--aws-region=<aws-region>]
ds_proxy proxy [--address=<address>] [--password-file=<password-file>] [--salt=<salt>] [--chunk-size=<chunk-size>] [--upstream-url=<upstream-url>] [--local-encryption-directory=<local-encryption-directory>] [--keyring-file=<keyring-file>] [--aws-access-key=<aws-access-key>] [--aws-secret-key=<aws-secret-key>] [--aws-region=<aws-region>] [--backend-connection-timeout=<backend-connection-timeout>]
ds_proxy add-key [--password-file=<password-file>] [--salt=<salt>] [--keyring-file=<keyring-file>]
ds_proxy (-h | --help)
ds_proxy --version
Expand All @@ -30,6 +30,7 @@ pub struct Args {
pub flag_aws_access_key: Option<String>,
pub flag_aws_secret_key: Option<String>,
pub flag_aws_region: Option<String>,
pub flag_backend_connection_timeout: Option<u64>,
pub cmd_encrypt: bool,
pub cmd_decrypt: bool,
pub cmd_proxy: bool,
Expand Down
14 changes: 14 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::env;
use std::net::{SocketAddr, ToSocketAddrs};
use std::path::PathBuf;
use url::Url;
use std::time::Duration;

// match nginx default (proxy_buffer_size in ngx_stream_proxy_module)
pub const DEFAULT_CHUNK_SIZE: usize = 16 * 1024;
Expand Down Expand Up @@ -42,6 +43,7 @@ pub struct HttpConfig {
pub aws_access_key: Option<String>,
pub aws_secret_key: Option<String>,
pub aws_region: Option<String>,
pub backend_connection_timeout: Duration,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -149,6 +151,16 @@ impl Config {
}
.unwrap();

let backend_connection_timeout = match&args.flag_backend_connection_timeout {
Some(timeout_u64) => Duration::from_secs(*timeout_u64),
None => match env::var("BACKEND_CONNECTION_TIMEOUT") {
Ok(timeout_string) => Duration::from_secs(timeout_string.parse().expect("BACKEND_CONNECTION_TIMEOUT is not a u64")),
_ => Duration::from_secs(1)
}
};

log::info!("backend_connection_timeout: {:?}", backend_connection_timeout);

Config::Http(HttpConfig {
keyring,
chunk_size,
Expand All @@ -158,6 +170,7 @@ impl Config {
aws_access_key: args.flag_aws_access_key.clone(),
aws_secret_key: args.flag_aws_secret_key.clone(),
aws_region: args.flag_aws_region.clone(),
backend_connection_timeout,
})
}
}
Expand Down Expand Up @@ -322,6 +335,7 @@ mod tests {
aws_access_key: None,
aws_secret_key: None,
aws_region: None,
backend_connection_timeout: Duration::from_secs(1)
}
}
}
3 changes: 1 addition & 2 deletions src/http/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use actix_web::dev::Service;
use futures::FutureExt;
use std::time::Duration;

const CONNECT_TIMEOUT: Duration = Duration::from_secs(1);
const RESPONSE_TIMEOUT: Duration = Duration::from_secs(30);

#[actix_web::main]
Expand All @@ -24,7 +23,7 @@ pub async fn main(config: HttpConfig) -> std::io::Result<()> {
.app_data(Data::new(
awc::Client::builder()
.connector(
awc::Connector::new().timeout(CONNECT_TIMEOUT), // max time to connect to remote host including dns name resolution
awc::Connector::new().timeout(config.backend_connection_timeout), // max time to connect to remote host including dns name resolution
)
.timeout(RESPONSE_TIMEOUT) // the total time before a response must be received
.finish(),
Expand Down

0 comments on commit 300bc04

Please sign in to comment.