From 5366a1a1e0ec54a7dd4cf3c61ed8888fc750e55c Mon Sep 17 00:00:00 2001 From: junderw Date: Sat, 7 Oct 2023 15:45:40 -0700 Subject: [PATCH] Improve logging --- src/electrum/server.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/electrum/server.rs b/src/electrum/server.rs index b97ad7c5..7aabd29b 100644 --- a/src/electrum/server.rs +++ b/src/electrum/server.rs @@ -634,11 +634,8 @@ impl Connection { fn handle_requests( mut reader: BufReader, tx: &crossbeam_channel::Sender, - addr: Option, + addr_str: &str, ) -> Result<()> { - let addr_str = addr - .map(|a| a.to_string()) - .unwrap_or_else(|| reader.get_ref().addr_string()); loop { let mut recv_data = Vec::::new(); match read_until(&mut reader, b'\n', &mut recv_data) { @@ -658,10 +655,10 @@ impl Connection { match serde_json::from_slice(&recv_data) { Ok(req) => tx .send(Message::Request(req)) - .chain_err(|| "channel closed")?, + .chain_err(|| format!("[{}] channel closed", addr_str))?, Err(err) => { let _ = tx.send(Message::Done); - bail!("invalid UTF8: {}", err) + bail!("[{}] invalid UTF8: {}", addr_str, err) } } } @@ -677,13 +674,18 @@ impl Connection { let addr_str = self.get_source_addr_str(); let shutdown_send = self.shutdown_send.clone(); let child = spawn_thread("reader", move || { + let addr_str = rpc_addr + .map(|a| a.to_string()) + .unwrap_or_else(|| reader.get_ref().addr_string()); let result = - std::panic::catch_unwind(|| Connection::handle_requests(reader, &tx, rpc_addr)) + std::panic::catch_unwind(|| Connection::handle_requests(reader, &tx, &addr_str)) .unwrap_or_else(|e| { - Err( - format!("RPC Panic in request handler: {}", parse_panic_error(&e)) - .into(), + Err(format!( + "[{}] RPC Panic in request handler: {}", + addr_str, + parse_panic_error(&e) ) + .into()) }); // This shuts down the other graceful shutdown thread, // which also shuts down the handle_replies loop @@ -1140,7 +1142,7 @@ fn read_until( buf: &mut Vec, ) -> std::io::Result { let mut read = 0; - let mut carry_over_arr = [0_u8; 1024]; + let mut carry_over_arr = [0_u8; 256]; let mut carrying_over = 0; loop { let (done, used) = { @@ -1254,7 +1256,7 @@ fn parse_panic_error(e: &(dyn std::any::Any + Send)) -> &str { /// A simple static array of 1024 size should be quick and easy. fn process_carry_over( carrying_over: &mut usize, - carry_over: &mut [u8; 1024], + carry_over: &mut [u8], available: &mut &[u8], ) -> usize { // How much space do we have left in the array? @@ -1266,6 +1268,7 @@ fn process_carry_over( .copy_from_slice(&available[..copy_bytes]); // Figure out if it was a proxy header or not. + #[allow(clippy::redundant_slicing)] let mut cursor = &carry_over[..]; let before_len = cursor.len(); let was_proxy = proxy_protocol::parse(&mut cursor).is_ok();