Skip to content

Commit

Permalink
fix: reorganize request statistics tracking
Browse files Browse the repository at this point in the history
- Remove redundant request tracking from read_frame
- Move all statistics tracking to handle_client_inner
- Track TCP frame errors and Modbus errors separately
- Ensure each request is counted only once
  • Loading branch information
aljen committed Dec 6, 2024
1 parent 7a9608c commit eb7e12d
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/modbus_relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ async fn read_frame(
}
Ok(Ok(n)) => {
if n < 7 {
manager.record_request(peer_addr, false).await;
return Err(RelayError::frame(
FrameErrorKind::TooShort,
format!("Frame too short: {} bytes", n),
Expand All @@ -371,13 +370,11 @@ async fn read_frame(
n
}
Ok(Err(e)) => {
manager.record_request(peer_addr, false).await;
return Err(RelayError::Connection(ConnectionError::InvalidState(
format!("Connection lost: {}", e),
)));
}
Err(_) => {
manager.record_request(peer_addr, false).await;
return Err(RelayError::Connection(ConnectionError::Timeout(
"Read operation timed out".to_string(),
)));
Expand All @@ -394,7 +391,6 @@ async fn read_frame(
let transaction_id = [tcp_buf[0], tcp_buf[1]];
let protocol_id = u16::from_be_bytes([tcp_buf[2], tcp_buf[3]]);
if protocol_id != 0 {
manager.record_request(peer_addr, false).await;
return Err(RelayError::protocol(
ProtocolErrorKind::InvalidProtocolId,
format!("Invalid protocol ID: {}", protocol_id),
Expand All @@ -403,7 +399,6 @@ async fn read_frame(

let length = u16::from_be_bytes([tcp_buf[4], tcp_buf[5]]) as usize;
if length > 249 {
manager.record_request(peer_addr, false).await;
return Err(RelayError::frame(
FrameErrorKind::TooLong,
format!("Frame too long: {} bytes", length),
Expand All @@ -412,7 +407,6 @@ async fn read_frame(
}

if length + 6 != n {
manager.record_request(peer_addr, false).await;
return Err(RelayError::frame(
FrameErrorKind::InvalidFormat,
format!("Invalid frame length, expected {}, got {}", length + 6, n),
Expand Down Expand Up @@ -512,20 +506,38 @@ async fn handle_client_inner(
let modbus = ModbusProcessor::new(transport);

loop {
let frame_start = Instant::now();

// 1. Read frame
let (frame, transaction_id) = match read_frame(&mut reader, peer_addr, &manager).await {
Ok((frame, id)) => (frame, id),
Err(RelayError::Connection(ConnectionError::Disconnected)) => {
info!("Client {} disconnected", peer_addr);
break;
}
Err(e) => return Err(e),
Err(e) => {
// Record TCP frame error
manager.record_errors();
manager.record_request(peer_addr, false).await;
manager.record_response_time(frame_start.elapsed()).await;
return Err(e);
}
};

// 2. Process frame
let response = match process_frame(&modbus, &frame, transaction_id).await {
Ok(response) => response,
Ok(response) => {
// Record successful Modbus request
manager.record_requests();
manager.record_request(peer_addr, true).await;
manager.record_response_time(frame_start.elapsed()).await;
response
}
Err(e) => {
// Record failed Modbus request
manager.record_errors();
manager.record_request(peer_addr, false).await;
manager.record_response_time(frame_start.elapsed()).await;
return Err(e);
}
};
Expand Down

0 comments on commit eb7e12d

Please sign in to comment.