xitca_postgres/
cancel.rs

1//! module for canceling ongoing queries
2
3use postgres_protocol::message::frontend;
4use xitca_io::bytes::BytesMut;
5
6use super::{
7    error::{Error, RuntimeError},
8    session::Session,
9};
10
11impl Session {
12    /// Attempts to cancel the in-progress query on the connection associated with Self.
13    ///
14    /// The server provides no information about whether a cancellation attempt was successful or not. An error will
15    /// only be returned if the client was unable to connect to the database.
16    ///
17    /// Cancellation is inherently racy. There is no guarantee that the cancellation request will reach the server
18    /// before the query terminates normally, or that the connection associated with this token is still active.
19    pub async fn query_cancel(self) -> Result<(), Error> {
20        let Session { id, key, info } = self;
21        let (_tx, mut drv) = super::driver::connect_info(info).await?;
22        let mut buf = BytesMut::new();
23        frontend::cancel_request(id, key, &mut buf);
24        drv.send(buf).await
25    }
26
27    /// blocking version of [`Session::query_cancel`]
28    pub fn query_cancel_blocking(self) -> Result<(), Error> {
29        match tokio::runtime::Handle::try_current() {
30            Ok(_) => Err(RuntimeError::RequireNoTokio.into()),
31            Err(_) => tokio::runtime::Builder::new_current_thread()
32                .enable_all()
33                .build()?
34                .block_on(self.query_cancel()),
35        }
36    }
37}