From 047e37a101ca74ccf918a6e7541a63beba35eb1b Mon Sep 17 00:00:00 2001 From: Oleksandr Mazur Date: Wed, 11 Dec 2024 16:02:30 +0200 Subject: [PATCH] Connection processor: add a force TLS-accept timeout Whenever TLS accept (tungstenite::accept(tls_stream)) blocks for too long (>15 seconds), stop trying to accept the stream using tokio_selector. This is done to ensure we don't have a hang connection processor that might hang for a very long period of time waiting for a connection to be accepted. Also run cargo fmt to fix some import indentation. Signed-off-by: Oleksandr Mazur --- src/cgw_connection_processor.rs | 19 ++++++++++++++++++- src/cgw_ucentral_switch_parser.rs | 8 ++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/cgw_connection_processor.rs b/src/cgw_connection_processor.rs index 24e3a8a..8567734 100644 --- a/src/cgw_connection_processor.rs +++ b/src/cgw_connection_processor.rs @@ -105,7 +105,24 @@ impl CGWConnectionProcessor { client_cn: MacAddress, allow_mismatch: bool, ) -> Result<()> { - let ws_stream = tokio_tungstenite::accept_async(tls_stream).await?; + let ws_stream = tokio::select! { + _val = tokio_tungstenite::accept_async(tls_stream) => { + match _val { + Ok(s) => s, + Err(e) => { + error!("Failed to accept TLS stream from: {}! Reason: {}. Closing connection", + self.addr, e); + return Err(Error::ConnectionProcessor("Failed to accept TLS stream!")); + } + } + } + // TODO: configurable duration (upon server creation) + _val = sleep(Duration::from_millis(15000)) => { + error!("Failed to accept TLS stream from: {}! Closing connection", self.addr); + return Err(Error::ConnectionProcessor("Failed to accept TLS stream for too long")); + } + + }; let (sink, mut stream) = ws_stream.split(); diff --git a/src/cgw_ucentral_switch_parser.rs b/src/cgw_ucentral_switch_parser.rs index ece37cf..0c7d70a 100644 --- a/src/cgw_ucentral_switch_parser.rs +++ b/src/cgw_ucentral_switch_parser.rs @@ -5,10 +5,10 @@ use std::{collections::HashMap, str::FromStr}; use crate::cgw_errors::{Error, Result}; use crate::cgw_ucentral_parser::{ - CGWUCentralEvent, CGWUCentralEventLog, CGWUCentralEventState, CGWUCentralEventStateClients, - CGWUCentralEventStateClientsData, CGWUCentralEventStateClientsType, - CGWUCentralEventStateLLDPData, CGWUCentralEventStateLinks, CGWUCentralEventStatePort, - CGWUCentralEventType, CGWUCentralJRPCMessage, CGWUCentralEventReply + CGWUCentralEvent, CGWUCentralEventLog, CGWUCentralEventReply, CGWUCentralEventState, + CGWUCentralEventStateClients, CGWUCentralEventStateClientsData, + CGWUCentralEventStateClientsType, CGWUCentralEventStateLLDPData, CGWUCentralEventStateLinks, + CGWUCentralEventStatePort, CGWUCentralEventType, CGWUCentralJRPCMessage, }; fn parse_lldp_data(