Skip to content

Commit

Permalink
adds configurable timout feature (#62)
Browse files Browse the repository at this point in the history
* Request timeouts should be configurable #56

Co-authored-by: bhaskarkishore <50350646+bhaskarkishore@users.noreply.github.com>
  • Loading branch information
SiddharthV1 and bhaskarkishore authored Sep 7, 2021
1 parent 95dcc7b commit d756998
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ log/**
.env
*.txt
*.log
/lib/npm/**/*.js
/lib/npm/**/*.js
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ built = { version = "0.5", features = ["git2"] }

[dependencies]
hyper = { version = "0.14", features = ["full"] }
hyper-timeout = "0.4"
tokio = { version = "1", features = ["full"] }
log = "0.4.14"
log4rs = "1.0.0"
Expand Down
3 changes: 3 additions & 0 deletions proxy.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
# Maximum image size the proxy attempt to fetch in bytes.
# Omit this entry if want to support any size imageas
"max_document_size": 26214400

# Time out in seconds for request,response and connecting to a url
"timeout": 100

# Api access keys. Random ones provided below for testing, replace with your
# list.
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub struct Configuration {
pub workers: u16,
pub bind_address: Ipv4Addr,
pub port: u16,
pub timeout: u64,
pub metrics_enabled: bool,
pub dashboard_enabled: bool,
pub max_document_size: Option<u64>,
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub enum Errors {
UnsupportedUriScheme,
InvalidUri,
InvalidOrBlockedHost,
TimedOut,
}

impl Errors {
Expand All @@ -45,6 +46,7 @@ impl Errors {
Errors::InvalidOrBlockedHost => {
(110, "Invalid or blocked destination host".to_string())
}
Errors::TimedOut => (111, "Connection/Request/Response from the destination timed out".to_string()),
};

RpcError {
Expand Down
28 changes: 26 additions & 2 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use std::borrow::Borrow;
use std::time::Duration;

use std::error::Error as StdError;
use std::io::ErrorKind;

use hyper::client::HttpConnector;
use hyper::http::uri::Scheme;
use hyper::Client;
use hyper::{body::to_bytes, Uri};
use hyper_timeout::TimeoutConnector;
use hyper_tls::HttpsConnector;
use log::error;
use log::info;
Expand All @@ -15,12 +20,15 @@ use crate::document::Document;
use crate::metrics;
use crate::rpc::error::Errors;

use hyper::client::connect::dns::GaiResolver;
use hyper::Body;

use self::filters::UriFilter;

pub mod filters;

pub struct HttpClient {
client: Client<HttpsConnector<HttpConnector>>,
client: Client<TimeoutConnector<HttpsConnector<HttpConnector<GaiResolver>>>, Body>,
_max_document_size: Option<u64>,
ipfs_config: Host,
uri_filters: Vec<Box<dyn UriFilter + Send + Sync>>,
Expand All @@ -31,9 +39,16 @@ impl HttpClient {
ipfs_config: Host,
max_document_size: Option<u64>,
uri_filters: Vec<Box<dyn UriFilter + Send + Sync>>,
timeout: u64,
) -> HttpClient {
let https = HttpsConnector::new();
let client = Client::builder().build(https);
let mut connector = TimeoutConnector::new(https);

connector.set_connect_timeout(Some(Duration::from_secs(timeout)));
connector.set_read_timeout(Some(Duration::from_secs(timeout)));
connector.set_write_timeout(Some(Duration::from_secs(timeout)));

let client = Client::builder().build::<_, hyper::Body>(connector);
assert!(
!uri_filters.is_empty(),
"No URI filters provided. This is insecure, check code. Exiting..."
Expand Down Expand Up @@ -145,6 +160,7 @@ impl HttpClient {
error!("Document not found on remote, id={}", req_id);
Err(Errors::NotFound)
}

e => {
metrics::DOCUMENT.with_label_values(&["fetch_error"]).inc();
error!(
Expand All @@ -156,6 +172,14 @@ impl HttpClient {
},
Err(e) => {
metrics::DOCUMENT.with_label_values(&["fetch_error"]).inc();
if let Some(err_ref) = e.source() {
if let Some(err) = err_ref.downcast_ref::<std::io::Error>() {
if let ErrorKind::TimedOut = err.kind() {
error!("Unable to fetch document, connection/response/request to the server timed out, id={}", req_id);
return Err(Errors::TimedOut);
}
}
}
error!(
"Unable to fetch document, id={}, reason={}, url={}",
req_id, e, url
Expand Down
10 changes: 7 additions & 3 deletions src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ impl Context {
//TODO: Add more filters here
let uri_filters: Vec<Box<dyn UriFilter + Send + Sync>> =
vec![Box::new(PrivateNetworkFilter::new(Box::new(dns_resolver)))];
let http_client =
HttpClient::new(config.ipfs.clone(), config.max_document_size, uri_filters);
Ok(Context {
let http_client = HttpClient::new(
config.ipfs.clone(),
config.max_document_size,
uri_filters,
config.timeout
);
Ok(Context{
config: config.clone(),
database,
moderation_provider,
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub enum Errors {
UnsupportedUriScheme,
InvalidUri,
InvalidOrBlockedHost,
TimedOut,
}

impl Errors {
Expand All @@ -44,6 +45,7 @@ impl Errors {
Errors::InvalidOrBlockedHost => {
(110, "Invalid or blocked destination host".to_string())
}
Errors::TimedOut => (111, "Connection/Request/Response from the destination timed out".to_string())
};

RpcError {
Expand Down

0 comments on commit d756998

Please sign in to comment.