diff --git a/src/daemon.rs b/src/daemon.rs index 739383a86..457bf4230 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -1,4 +1,5 @@ use std::collections::{HashMap, HashSet}; +use std::env; use std::io::{BufRead, BufReader, Lines, Write}; use std::net::{SocketAddr, TcpStream}; use std::path::PathBuf; @@ -23,8 +24,17 @@ use crate::util::{HeaderList, DEFAULT_BLOCKHASH}; use crate::errors::*; -// Used for the connection, read and write timeouts -const DAEMON_TIMEOUT: Duration = Duration::from_secs(10); +lazy_static! { + static ref DAEMON_CONNECTION_TIMEOUT: Duration = Duration::from_secs( + env::var("DAEMON_CONNECTION_TIMEOUT").map_or(10, |s| s.parse().unwrap()) + ); + static ref DAEMON_READ_TIMEOUT: Duration = Duration::from_secs( + env::var("DAEMON_READ_TIMEOUT").map_or(10 * 60, |s| s.parse().unwrap()) + ); + static ref DAEMON_WRITE_TIMEOUT: Duration = Duration::from_secs( + env::var("DAEMON_WRITE_TIMEOUT").map_or(10 * 60, |s| s.parse().unwrap()) + ); +} fn parse_hash(value: &Value) -> Result where @@ -132,15 +142,18 @@ struct Connection { fn tcp_connect(addr: SocketAddr, signal: &Waiter) -> Result { loop { - match TcpStream::connect_timeout(&addr, DAEMON_TIMEOUT) { + match TcpStream::connect_timeout(&addr, *DAEMON_CONNECTION_TIMEOUT) { Ok(conn) => { // can only fail if DAEMON_TIMEOUT is 0 - conn.set_read_timeout(Some(DAEMON_TIMEOUT)).unwrap(); - conn.set_write_timeout(Some(DAEMON_TIMEOUT)).unwrap(); + conn.set_read_timeout(Some(*DAEMON_READ_TIMEOUT)).unwrap(); + conn.set_write_timeout(Some(*DAEMON_WRITE_TIMEOUT)).unwrap(); return Ok(conn); } Err(err) => { - warn!("failed to connect daemon at {}: {}", addr, err); + warn!( + "failed to connect daemon at {}: {} (backoff 3 seconds)", + addr, err + ); signal.wait(Duration::from_secs(3), false)?; continue; }