mirror of
https://github.com/jwinterm/grin-wallet.git
synced 2026-11-26 17:13:54 +01:00
Connection timeout (#765)
This commit is contained in:
+19
-19
@@ -160,12 +160,10 @@ pub fn start_tor_client(tor_dir: &str, config: TorConfig) -> Result<Tor, Error>
|
||||
})
|
||||
}
|
||||
|
||||
/// Tor request timeout in milliseconds.
|
||||
const REQUEST_TIMEOUT_MS: u64 = 60000;
|
||||
|
||||
/// Make POST request with provided client.
|
||||
pub fn tor_post<IN>(
|
||||
client: Arc<TorClient<TokioNativeTlsRuntime>>,
|
||||
tor_config: &TorConfig,
|
||||
input: &IN,
|
||||
url: &str,
|
||||
) -> Result<String, Error>
|
||||
@@ -183,12 +181,13 @@ where
|
||||
Some(h) => h,
|
||||
}
|
||||
.to_string();
|
||||
let timeout = tor_config.request_timeout();
|
||||
let res: Result<String, Error> = thread::spawn(move || {
|
||||
let c = client.clone();
|
||||
client.runtime().block_on(async move {
|
||||
let res = c
|
||||
.runtime()
|
||||
.timeout(Duration::from_millis(REQUEST_TIMEOUT_MS), async {
|
||||
.timeout(timeout, async {
|
||||
let stream = c
|
||||
.connect((host, url.port_u16().unwrap_or(80)))
|
||||
.await
|
||||
@@ -238,21 +237,18 @@ where
|
||||
res
|
||||
}
|
||||
|
||||
/// Client Bootstrap timeout in milliseconds.
|
||||
const BOOTSTRAP_TIMEOUT_MS: u64 = 60000;
|
||||
|
||||
/// Create Tor client.
|
||||
fn init_client(
|
||||
state_path: &PathBuf,
|
||||
cache_path: &PathBuf,
|
||||
config: TorConfig,
|
||||
tor_config: TorConfig,
|
||||
reuse_client: bool,
|
||||
) -> Result<(Arc<TorClient<TokioNativeTlsRuntime>>, TorClientConfig), Error> {
|
||||
let mut builder = TorClientConfigBuilder::from_directories(&state_path, cache_path);
|
||||
builder.address_filter().allow_onion_addrs(true);
|
||||
|
||||
// Configure bridge.
|
||||
if let Some(bridge_line) = config.bridge.bridge_line {
|
||||
if let Some(bridge_line) = tor_config.bridge.bridge_line.as_ref() {
|
||||
let bridge: BridgeConfigBuilder = bridge_line
|
||||
.parse()
|
||||
.map_err(|e| Error::TorConfig(format!("{:?}", e)))?;
|
||||
@@ -266,7 +262,11 @@ fn init_client(
|
||||
}
|
||||
Some(t) => {
|
||||
// Now configure bridge transport. (Requires the "pt-client" feature)
|
||||
let bin_path = config.bridge.bridge_bin_path.unwrap_or(t.to_owned());
|
||||
let bin_path = tor_config
|
||||
.bridge
|
||||
.bridge_bin_path
|
||||
.clone()
|
||||
.unwrap_or(t.to_owned());
|
||||
let mut transport = TransportConfigBuilder::default();
|
||||
transport
|
||||
.protocols(vec![t
|
||||
@@ -294,7 +294,7 @@ fn init_client(
|
||||
*cached_client_config = None;
|
||||
}
|
||||
}
|
||||
let res = launch_client(config.clone());
|
||||
let res = launch_client(config.clone(), &tor_config);
|
||||
return match res {
|
||||
Ok(client) => {
|
||||
cached_client_config.replace((client.clone(), config.clone()));
|
||||
@@ -304,7 +304,7 @@ fn init_client(
|
||||
};
|
||||
}
|
||||
|
||||
let res = launch_client(config.clone());
|
||||
let res = launch_client(config.clone(), &tor_config);
|
||||
match res {
|
||||
Ok(client) => Ok((client, config)),
|
||||
Err(e) => Err(e),
|
||||
@@ -312,13 +312,17 @@ fn init_client(
|
||||
}
|
||||
|
||||
/// Launch tor client from provided configuration.
|
||||
fn launch_client(config: TorClientConfig) -> Result<Arc<TorClient<TokioNativeTlsRuntime>>, Error> {
|
||||
fn launch_client(
|
||||
client_config: TorClientConfig,
|
||||
tor_config: &TorConfig,
|
||||
) -> Result<Arc<TorClient<TokioNativeTlsRuntime>>, Error> {
|
||||
let r = runtime()?;
|
||||
let client = TorClient::with_runtime(r)
|
||||
.config(config)
|
||||
.config(client_config)
|
||||
.create_unbootstrapped()
|
||||
.map_err(|e| Error::TorProcess(format!("{:?}", e)))?;
|
||||
let c = client.clone();
|
||||
let timeout = tor_config.bootstrap_timeout();
|
||||
let res = client.runtime().block_on(async move {
|
||||
let bootstrap = async || {
|
||||
return match c.bootstrap().await {
|
||||
@@ -339,11 +343,7 @@ fn launch_client(config: TorClientConfig) -> Result<Arc<TorClient<TokioNativeTls
|
||||
Err(e) => Err(e),
|
||||
};
|
||||
};
|
||||
match c
|
||||
.runtime()
|
||||
.timeout(Duration::from_millis(BOOTSTRAP_TIMEOUT_MS), bootstrap())
|
||||
.await
|
||||
{
|
||||
match c.runtime().timeout(timeout, bootstrap()).await {
|
||||
Ok(r) => match r {
|
||||
Err(e) => Err(Error::TorProcess(format!("{:?}", e))),
|
||||
Ok(_) => Ok(c),
|
||||
|
||||
+12
-11
@@ -51,13 +51,13 @@ extern crate timer;
|
||||
|
||||
use regex::Regex;
|
||||
use std::fs::{self, File};
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::{Path, MAIN_SEPARATOR};
|
||||
use std::process::{Child, ChildStdout, Command, Stdio};
|
||||
use std::sync::mpsc::channel;
|
||||
use std::thread;
|
||||
use std::{cmp, io};
|
||||
use sysinfo::{Process, ProcessExt, System, SystemExt};
|
||||
|
||||
#[cfg(windows)]
|
||||
@@ -83,7 +83,7 @@ pub struct TorProcess {
|
||||
args: Vec<String>,
|
||||
torrc_path: Option<String>,
|
||||
completion_percent: u8,
|
||||
timeout: u32,
|
||||
timeout: u64,
|
||||
working_dir: Option<String>,
|
||||
pub stdout: Option<BufReader<ChildStdout>>,
|
||||
pub process: Option<Child>,
|
||||
@@ -96,8 +96,8 @@ impl TorProcess {
|
||||
tor_cmd: TOR_EXE_NAME.to_string(),
|
||||
args: vec![],
|
||||
torrc_path: None,
|
||||
completion_percent: 100 as u8,
|
||||
timeout: 0 as u32,
|
||||
completion_percent: 100,
|
||||
timeout: 0,
|
||||
working_dir: None,
|
||||
stdout: None,
|
||||
process: None,
|
||||
@@ -137,7 +137,7 @@ impl TorProcess {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn timeout(&mut self, timeout: u32) -> &mut Self {
|
||||
pub fn timeout(&mut self, timeout: u64) -> &mut Self {
|
||||
self.timeout = timeout;
|
||||
self
|
||||
}
|
||||
@@ -198,10 +198,11 @@ impl TorProcess {
|
||||
let stdout_timeout_tx = stdout_tx.clone();
|
||||
|
||||
let timer = timer::Timer::new();
|
||||
let _guard =
|
||||
timer.schedule_with_delay(chrono::Duration::seconds(self.timeout as i64), move || {
|
||||
stdout_timeout_tx.send(Err(Error::Timeout)).unwrap_or(());
|
||||
});
|
||||
// Keep this below chrono's DateTime range used by timer.
|
||||
let timeout = chrono::Duration::seconds(cmp::min(self.timeout, u32::MAX as u64) as i64);
|
||||
let _guard = timer.schedule_with_delay(timeout, move || {
|
||||
stdout_timeout_tx.send(Err(Error::Timeout)).unwrap_or(());
|
||||
});
|
||||
let stdout_thread = thread::spawn(move || {
|
||||
stdout_tx
|
||||
.send(Self::parse_tor_stdout(stdout, completion_percent))
|
||||
@@ -225,8 +226,8 @@ impl TorProcess {
|
||||
mut stdout: BufReader<ChildStdout>,
|
||||
completion_perc: u8,
|
||||
) -> Result<BufReader<ChildStdout>, Error> {
|
||||
let re_bootstrap = Regex::new(r"^\[notice\] Bootstrapped (?P<perc>[0-9]+)%(.*): ")
|
||||
.map_err(Error::Regex)?;
|
||||
let re_bootstrap =
|
||||
Regex::new(r"^\[notice] Bootstrapped (?P<perc>[0-9]+)%(.*): ").map_err(Error::Regex)?;
|
||||
|
||||
let timestamp_len = "May 16 02:50:08.792".len();
|
||||
let mut warnings = Vec::new();
|
||||
|
||||
Reference in New Issue
Block a user