From d4ba9f948f5acc5f6c22db0b5f2a1666262acc59 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sat, 28 Mar 2020 21:23:36 +0100 Subject: controller: Set keepalive on sockets --- controller/src/lib.rs | 9 +++++++++ utils/.gitignore | 1 + utils/src/protocol.rs | 4 ++-- 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 utils/.gitignore diff --git a/controller/src/lib.rs b/controller/src/lib.rs index b53502f..60b3639 100644 --- a/controller/src/lib.rs +++ b/controller/src/lib.rs @@ -120,6 +120,15 @@ enum ThreadCollect { async fn thread_handshake_handler(mut listener: TcpListener, sink: mpsc::Sender) { loop { let (mut sock, _) = listener.accept().await.expect("Accept failed on TCP server socket"); + + // Error setting keepalive is not fatal + match sock.set_keepalive(Some(std::time::Duration::from_secs(60))) { + Ok(()) => {} + Err(e) => { + eprintln!("WARNING: Error setting keepalive on worker socket: {}", e); + } + } + let mut sink = sink.clone(); task::spawn(async move { let payload = encode_message(MessageBody::Version(1)); diff --git a/utils/.gitignore b/utils/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/utils/.gitignore @@ -0,0 +1 @@ +/target diff --git a/utils/src/protocol.rs b/utils/src/protocol.rs index 4820f79..fce168d 100644 --- a/utils/src/protocol.rs +++ b/utils/src/protocol.rs @@ -23,7 +23,7 @@ pub struct RawMessage { impl RawMessage { pub fn receive(reader: &mut BufReader) -> io::Result> { let mut header = [0u8; 17]; - if let Err(e) = reader.read(&mut header) { + if let Err(e) = reader.read_exact(&mut header) { if e.kind() == ErrorKind::UnexpectedEof { return Ok(None); } else { return Err(e); } } @@ -34,7 +34,7 @@ impl RawMessage { let mut payload = Vec::new(); payload.resize(length, 0u8); - if let Err(e) = reader.read(&mut payload) { + if let Err(e) = reader.read_exact(&mut payload) { if e.kind() == ErrorKind::UnexpectedEof { return Ok(None); } else { return Err(e); } } -- cgit v1.2.3-70-g09d2