Skip to main content

diesel_async/mysql_like/
cancel_token.rs

1use core::marker::PhantomData;
2
3use diesel::mysql_like::MysqlLikeBackend;
4use mysql_async::prelude::Query;
5use mysql_async::{Opts, OptsBuilder};
6
7use crate::mysql_like::error_helper::ErrorHelper;
8
9/// The capability to request cancellation of in-progress queries on a
10/// connection.
11#[derive(Clone)]
12pub struct MysqlLikeCancelToken<DB: MysqlLikeBackend> {
13    pub(crate) opts: Opts,
14    pub(crate) kill_id: u32,
15    pub(crate) phantom: PhantomData<DB>,
16}
17
18#[expect(private_bounds)]
19impl<DB: MysqlLikeBackend> MysqlLikeCancelToken<DB>
20where
21    diesel::result::Error: From<ErrorHelper<DB>>,
22{
23    /// Attempts to cancel the in-progress query on the connection associated
24    /// with this `CancelToken`.
25    ///
26    /// The server provides no information about whether a cancellation attempt was successful or not. An error will
27    /// only be returned if the client was unable to connect to the database.
28    ///
29    /// Cancellation is inherently racy. There is no guarantee that the
30    /// cancellation request will reach the server before the query terminates
31    /// normally, or that the connection associated with this token is still
32    /// active.
33    pub async fn cancel_query(&self) -> diesel::result::ConnectionResult<()> {
34        let builder = OptsBuilder::from_opts(self.opts.clone());
35
36        let conn = mysql_async::Conn::new(builder)
37            .await
38            .map_err(ErrorHelper::<DB>::new)?;
39
40        format!("KILL QUERY {};", self.kill_id)
41            .ignore(conn)
42            .await
43            .map_err(ErrorHelper::<DB>::new)?;
44
45        Ok(())
46    }
47}