Skip to content

Commit

Permalink
Allow setting DAEMON_{CONNECTION,READ,WRITE}_TIMEOUT using environmen…
Browse files Browse the repository at this point in the history
…t variables
  • Loading branch information
philippem authored and shesek committed May 17, 2024
1 parent 8c3fc1c commit 0579704
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/daemon.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<T>(value: &Value) -> Result<T>
where
Expand Down Expand Up @@ -132,15 +142,18 @@ struct Connection {

fn tcp_connect(addr: SocketAddr, signal: &Waiter) -> Result<TcpStream> {
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;
}
Expand Down

0 comments on commit 0579704

Please sign in to comment.