lapin/
connection_closer.rs1use crate::{ConnectionStatus, internal_rpc::InternalRPCHandle, protocol};
2use std::sync::atomic::{AtomicBool, Ordering};
3
4#[derive(Debug)]
5pub(crate) struct ConnectionCloser {
6 status: ConnectionStatus,
7 internal_rpc: InternalRPCHandle,
8 noop: AtomicBool,
9}
10
11impl ConnectionCloser {
12 pub(crate) fn new(status: ConnectionStatus, internal_rpc: InternalRPCHandle) -> Self {
13 Self {
14 status,
15 internal_rpc,
16 noop: AtomicBool::new(false),
17 }
18 }
19
20 pub(crate) fn noop(&self) {
21 self.noop.store(true, Ordering::SeqCst);
22 }
23}
24
25impl Drop for ConnectionCloser {
26 fn drop(&mut self) {
27 if self.noop.load(Ordering::SeqCst) {
28 return;
29 }
30
31 if self.status.auto_close() {
32 self.internal_rpc.close_connection(
33 protocol::constants::REPLY_SUCCESS,
34 "OK".into(),
35 0,
36 0,
37 );
38 }
39 }
40}