Line data Source code
1 : use crate::config::SslMode;
2 : use crate::tls::TlsConnect;
3 :
4 : use crate::{cancel_query, client::SocketConfig, tls::MakeTlsConnect};
5 : use crate::{cancel_query_raw, Error};
6 : use serde::{Deserialize, Serialize};
7 : use tokio::io::{AsyncRead, AsyncWrite};
8 : use tokio::net::TcpStream;
9 :
10 : /// The capability to request cancellation of in-progress queries on a
11 : /// connection.
12 0 : #[derive(Clone, Serialize, Deserialize)]
13 : pub struct CancelToken {
14 : pub socket_config: Option<SocketConfig>,
15 : pub ssl_mode: SslMode,
16 : pub process_id: i32,
17 : pub secret_key: i32,
18 : }
19 :
20 : impl CancelToken {
21 : /// Attempts to cancel the in-progress query on the connection associated
22 : /// with this `CancelToken`.
23 : ///
24 : /// The server provides no information about whether a cancellation attempt was successful or not. An error will
25 : /// only be returned if the client was unable to connect to the database.
26 : ///
27 : /// Cancellation is inherently racy. There is no guarantee that the
28 : /// cancellation request will reach the server before the query terminates
29 : /// normally, or that the connection associated with this token is still
30 : /// active.
31 : ///
32 : /// Requires the `runtime` Cargo feature (enabled by default).
33 0 : pub async fn cancel_query<T>(&self, tls: T) -> Result<(), Error>
34 0 : where
35 0 : T: MakeTlsConnect<TcpStream>,
36 0 : {
37 0 : cancel_query::cancel_query(
38 0 : self.socket_config.clone(),
39 0 : self.ssl_mode,
40 0 : tls,
41 0 : self.process_id,
42 0 : self.secret_key,
43 0 : )
44 0 : .await
45 0 : }
46 :
47 : /// Like `cancel_query`, but uses a stream which is already connected to the server rather than opening a new
48 : /// connection itself.
49 0 : pub async fn cancel_query_raw<S, T>(&self, stream: S, tls: T) -> Result<(), Error>
50 0 : where
51 0 : S: AsyncRead + AsyncWrite + Unpin,
52 0 : T: TlsConnect<S>,
53 0 : {
54 0 : cancel_query_raw::cancel_query_raw(
55 0 : stream,
56 0 : self.ssl_mode,
57 0 : tls,
58 0 : self.process_id,
59 0 : self.secret_key,
60 0 : )
61 0 : .await
62 0 : }
63 : }
|