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