modbus_relay/connection/
guard.rs

1use std::{net::SocketAddr, sync::Arc};
2use tokio::sync::OwnedSemaphorePermit;
3use tracing::{trace, warn};
4
5use crate::connection::StatEvent;
6
7use super::ConnectionManager;
8
9/// RAII guard for the connection
10#[derive(Debug)]
11pub struct ConnectionGuard {
12    pub manager: Arc<ConnectionManager>,
13    pub addr: SocketAddr,
14    pub _global_permit: OwnedSemaphorePermit,
15    pub _per_ip_permit: Option<OwnedSemaphorePermit>,
16}
17
18impl Drop for ConnectionGuard {
19    fn drop(&mut self) {
20        trace!("Dropping connection guard for {}", self.addr);
21
22        if let Err(e) = self
23            .manager
24            .stats_tx()
25            .try_send(StatEvent::ClientDisconnected(self.addr))
26        {
27            warn!("Failed to send disconnect event: {}", e);
28        }
29
30        self.manager.decrease_connection_count(self.addr);
31
32        trace!("Connection guard dropped for {}", self.addr);
33    }
34}