More error handling/debugging

This commit is contained in:
Greg Heartsfield 2021-11-23 12:35:25 -06:00
parent 854531112d
commit 25ac86cc68

View File

@ -1,10 +1,11 @@
use std::{env, io::Error}; use std::{env, io::Error};
use futures_util::{SinkExt, StreamExt}; use futures_util::{SinkExt, StreamExt};
use log::{debug, info}; use log::{debug, info, warn};
use tokio::net::{TcpListener, TcpStream}; use tokio::net::{TcpListener, TcpStream};
use tokio::runtime::Builder; use tokio::runtime::Builder;
use tokio_tungstenite::WebSocketStream; use tokio_tungstenite::WebSocketStream;
use tungstenite::error::Error::*;
use tungstenite::protocol::frame::coding::CloseCode; use tungstenite::protocol::frame::coding::CloseCode;
use tungstenite::protocol::frame::CloseFrame; use tungstenite::protocol::frame::CloseFrame;
use tungstenite::protocol::{Message, WebSocketConfig}; use tungstenite::protocol::{Message, WebSocketConfig};
@ -13,7 +14,7 @@ fn main() -> Result<(), Error> {
let _ = env_logger::try_init(); let _ = env_logger::try_init();
let addr = env::args() let addr = env::args()
.nth(1) .nth(1)
.unwrap_or_else(|| "127.0.0.1:8080".to_string()); .unwrap_or_else(|| "0.0.0.0:8888".to_string());
// configure tokio runtime // configure tokio runtime
let rt = Builder::new_multi_thread() let rt = Builder::new_multi_thread()
.worker_threads(2) .worker_threads(2)
@ -75,7 +76,9 @@ async fn process_client(stream: WebSocketStream<TcpStream>) {
// as long as connection is not closed... // as long as connection is not closed...
match mes_res { match mes_res {
Ok(Message::Text(cmd)) => { Ok(Message::Text(cmd)) => {
info!("Message received");
let length = cmd.len(); let length = cmd.len();
debug!("Message: {}", cmd);
write write
.send(Message::Text(format!( .send(Message::Text(format!(
"got your message of length {}", "got your message of length {}",
@ -99,13 +102,14 @@ async fn process_client(stream: WebSocketStream<TcpStream>) {
info!("Got request to close connection"); info!("Got request to close connection");
return; return;
} }
Err(e) => { Err(tungstenite::error::Error::Capacity(
// TODO: check for specific error: (Capacity(MessageTooLong { size: xxxxx, max_size: 131072 })) tungstenite::error::CapacityError::MessageTooLong { size, max_size },
)) => {
info!( info!(
"Message size too large, disconnecting this client: ({:?})", "Message size too large, disconnecting this client. ({} > {})",
e size, max_size
); );
write.send(Message::Text("[\"NOTICE\", \"MAX_EVENT_SIZE_EXCEEDED: Exceeded maximum event size for this relay. Closing Connection.\"]".to_owned())).await.expect("send failed"); write.send(Message::Text("[\"NOTICE\", \"MAX_EVENT_SIZE_EXCEEDED: Exceeded maximum event size for this relay. Closing Connection.\"]".to_owned())).await.expect("send notice failed");
write write
.reunite(read) .reunite(read)
.expect("reunite failed") .expect("reunite failed")
@ -117,6 +121,22 @@ async fn process_client(stream: WebSocketStream<TcpStream>) {
.expect("failed to send close frame"); .expect("failed to send close frame");
return; return;
} }
Err(AlreadyClosed) => {
warn!("this connection was already closed, and we tried to operate on it");
return;
}
Err(ConnectionClosed) | Err(Io(_)) => {
debug!("Closing this connection normally");
return;
}
Err(Tls(_)) | Err(Protocol(_)) | Err(Utf8) | Err(Url(_)) => {
info!("websocket/tls/enc protocol error, dropping connection");
return;
}
Err(e) => {
warn!("Some new kind of error, bailing: {:?}", e);
return;
}
} }
} }
println!("Client is exiting, no longer reading websocket messages"); println!("Client is exiting, no longer reading websocket messages");