Skip to content

Commit

Permalink
Close connection to taker if idle too long
Browse files Browse the repository at this point in the history
In practice the taker never leaves the connection open for any serious
length of time, so any delay would be either from a very slow connection,
broken connection or DOS.
  • Loading branch information
chris-belcher committed Jan 31, 2022
1 parent a5ec8de commit 55dac4a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -391,6 +391,7 @@ pub fn run_maker(
} else {
kill_flag.unwrap().clone()
},
idle_connection_timeout: 300,
};
maker_protocol::start_maker(rpc_ptr, wallet_ptr, config);
}
Expand Down
28 changes: 19 additions & 9 deletions src/maker_protocol.rs
Expand Up @@ -47,6 +47,7 @@ pub struct MakerConfig {
pub watchtower_ping_interval: u64,
pub maker_behavior: MakerBehavior,
pub kill_flag: Arc<RwLock<bool>>,
pub idle_connection_timeout: u64,
}

#[tokio::main]
Expand Down Expand Up @@ -157,6 +158,7 @@ async fn run(
let client_wallet = Arc::clone(&wallet);
let server_loop_comms_tx = server_loop_comms_tx.clone();
let maker_behavior = config.maker_behavior;
let idle_connection_timeout = config.idle_connection_timeout;

tokio::spawn(async move {
let (socket_reader, mut socket_writer) = socket.split();
Expand Down Expand Up @@ -184,16 +186,24 @@ async fn run(

loop {
let mut line = String::new();
match reader.read_line(&mut line).await {
Ok(n) if n == 0 => {
log::info!("[{}] Reached EOF", addr.port());
break;
}
Ok(_n) => (),
Err(e) => {
log::error!("error reading from socket: {:?}", e);
select! {
readline_ret = reader.read_line(&mut line) => {
match readline_ret {
Ok(n) if n == 0 => {
log::info!("[{}] Reached EOF", addr.port());
break;
}
Ok(_n) => (),
Err(e) => {
log::error!("error reading from socket: {:?}", e);
break;
}
}
},
_ = sleep(Duration::from_secs(idle_connection_timeout)) => {
log::info!("[{}] Idle connection closed", addr.port());
break;
}
},
};

line = line.trim_end().to_string();
Expand Down

0 comments on commit 55dac4a

Please sign in to comment.