halephant_core/proxy/
intercept.rs1use std::sync::Arc;
14
15use futures_util::SinkExt;
16use tokio::net::TcpStream;
17use tokio_util::codec::Framed;
18use tracing::debug;
19
20use crate::listener::ClientNotifications;
21use crate::pool::PoolManager;
22use crate::proto;
23use crate::proxy::prepared::ClientPrepared;
24use crate::sql;
25
26pub(super) async fn handle_close_intercept(
30 msg: &proto::frontend::FrontendMessage,
31 client_prepared: &mut ClientPrepared,
32 client: &mut Framed<TcpStream, proto::codec::FrontendCodec>,
33 pools: &Arc<PoolManager>,
34) -> anyhow::Result<bool> {
35 if let proto::frontend::FrontendMessage::Close(close) = msg
36 && close.kind == proto::frontend::TargetKind::Statement
37 {
38 {
39 let mut store = pools.stmt_store.lock();
40 client_prepared.remove(&close.name, &mut store);
41 }
42 client
43 .send(proto::backend::BackendMessage::CloseComplete)
44 .await?;
45 return Ok(true);
46 }
47 Ok(false)
48}
49
50pub(super) async fn handle_deallocate_intercept(
56 stmt: &sql::Statement,
57 client_prepared: &mut ClientPrepared,
58 client: &mut Framed<TcpStream, proto::codec::FrontendCodec>,
59 pools: &Arc<PoolManager>,
60 status: proto::types::TransactionStatus,
61) -> anyhow::Result<bool> {
62 let sql::Statement::Deallocate { target } = stmt else {
63 return Ok(false);
64 };
65
66 match target {
67 sql::DeallocateTarget::Name(name) => {
68 debug!(%name, "intercepting DEALLOCATE for named statement");
69 let mut store = pools.stmt_store.lock();
70 client_prepared.remove(name, &mut store);
78 }
79 sql::DeallocateTarget::All => {
80 debug!("intercepting DEALLOCATE ALL, releasing all client prepared statements");
81 let mut store = pools.stmt_store.lock();
82 client_prepared.release_all(&mut store);
83 }
84 }
85
86 crate::messages::synthetic::send_synthetic_ok(client, "DEALLOCATE", status).await?;
87 Ok(true)
88}
89
90pub(super) async fn handle_listen_intercept(
96 stmt: &sql::Statement,
97 notifications: &mut Option<ClientNotifications>,
98 client: &mut Framed<TcpStream, proto::codec::FrontendCodec>,
99 status: proto::types::TransactionStatus,
100) -> anyhow::Result<bool> {
101 match stmt {
102 sql::Statement::Listen { channel } => {
103 if let Some(notifs) = notifications.as_mut() {
104 debug!(%channel, "multiplex: subscribing client to channel");
105 notifs.listen(channel);
106 crate::messages::synthetic::send_synthetic_ok(client, "LISTEN", status).await?;
107 return Ok(true);
108 }
109 }
110 sql::Statement::Unlisten { target } => {
111 if let Some(notifs) = notifications.as_mut() {
112 match target {
113 sql::UnlistenTarget::Star => {
114 debug!("multiplex: unsubscribing client from all channels");
115 notifs.unlisten_all();
116 }
117 sql::UnlistenTarget::Channel(channel) => {
118 debug!(%channel, "multiplex: unsubscribing client from channel");
119 notifs.unlisten(channel);
120 }
121 }
122 crate::messages::synthetic::send_synthetic_ok(client, "UNLISTEN", status).await?;
123 return Ok(true);
124 }
125 }
126 _ => {}
127 }
128
129 Ok(false)
130}