Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of bitcoind-related connection/timeout issues #85

Merged
merged 2 commits into from
May 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 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,6 +24,18 @@ use crate::util::{HeaderList, DEFAULT_BLOCKHASH};

use crate::errors::*;

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
T: FromStr,
Expand Down Expand Up @@ -129,10 +142,18 @@ struct Connection {

fn tcp_connect(addr: SocketAddr, signal: &Waiter) -> Result<TcpStream> {
loop {
match TcpStream::connect(addr) {
Ok(conn) => return Ok(conn),
match TcpStream::connect_timeout(&addr, *DAEMON_CONNECTION_TIMEOUT) {
Ok(conn) => {
// can only fail if DAEMON_TIMEOUT is 0
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 Expand Up @@ -187,7 +208,7 @@ impl Connection {
.chain_err(|| {
ErrorKind::Connection("disconnected from daemon while receiving".to_owned())
})?
.chain_err(|| "failed to read status")?;
.chain_err(|| ErrorKind::Connection("failed to read status".to_owned()))?;
let mut headers = HashMap::new();
for line in iter {
let line = line.chain_err(|| ErrorKind::Connection("failed to read".to_owned()))?;
Expand Down
Loading