Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
junderw committed Oct 7, 2023
1 parent f90c222 commit 5366a1a
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/electrum/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,11 +634,8 @@ impl Connection {
fn handle_requests(
mut reader: BufReader<ConnectionStream>,
tx: &crossbeam_channel::Sender<Message>,
addr: Option<SocketAddr>,
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::<u8>::new();
match read_until(&mut reader, b'\n', &mut recv_data) {
Expand All @@ -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)
}
}
}
Expand All @@ -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
Expand Down Expand Up @@ -1140,7 +1142,7 @@ fn read_until(
buf: &mut Vec<u8>,
) -> std::io::Result<usize> {
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) = {
Expand Down Expand Up @@ -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?
Expand All @@ -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();
Expand Down

0 comments on commit 5366a1a

Please sign in to comment.