postgres/cancel_token.rs
1use tokio::runtime;
2use tokio_postgres::tls::MakeTlsConnect;
3use tokio_postgres::{Error, Socket};
4
5/// The capability to request cancellation of in-progress queries on a
6/// connection.
7#[derive(Clone)]
8pub struct CancelToken(tokio_postgres::CancelToken);
9
10impl CancelToken {
11 pub(crate) fn new(inner: tokio_postgres::CancelToken) -> CancelToken {
12 CancelToken(inner)
13 }
14
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 fn cancel_query<T>(&self, tls: T) -> Result<(), Error>
26 where
27 T: MakeTlsConnect<Socket>,
28 {
29 runtime::Builder::new_current_thread()
30 .enable_all()
31 .build()
32 .unwrap() // FIXME don't unwrap
33 .block_on(self.0.cancel_query(tls))
34 }
35}