#[non_exhaustive]pub struct CancelToken { /* private fields */ }Expand description
A token that can be used to cancel a running query on a connection.
The token contains the host, port, process ID, and secret key needed to send an out-of-band cancellation request. It can be cloned and sent to another task or thread.
§Example
let token = conn.cancel_token();
// Spawn a task that will cancel the query after 5 seconds
tokio::spawn(async move {
tokio::time::sleep(Duration::from_secs(5)).await;
token.cancel().await.unwrap();
});
// This long-running query will be cancelled
conn.query("SELECT pg_sleep(60)").await?;Implementations§
Source§impl CancelToken
impl CancelToken
Sourcepub async fn cancel(&self) -> Result<()>
pub async fn cancel(&self) -> Result<()>
Send a cancellation request to the server.
This opens a new TCP connection to the server, sends a
CancelRequest message, and closes the connection. The server
will then attempt to cancel the running query on the original
connection.
Note that cancellation is not guaranteed — the server may not be able to cancel the query if it’s in a non-interruptible state. The cancellation request itself is always acknowledged.
§Errors
Returns an error if the TCP connection cannot be established or the cancellation message cannot be sent.
Sourcepub async fn cancel_with_timeout(&self, timeout: Option<Duration>) -> Result<()>
pub async fn cancel_with_timeout(&self, timeout: Option<Duration>) -> Result<()>
Send a cancellation request with an optional connection timeout.
Sourcepub fn process_id(&self) -> i32
pub fn process_id(&self) -> i32
Returns the process ID of the backend this token can cancel.
Sourcepub fn secret_key(&self) -> i32
pub fn secret_key(&self) -> i32
Returns the secret key of the backend this token can cancel.
Trait Implementations§
Source§impl Clone for CancelToken
impl Clone for CancelToken
Source§fn clone(&self) -> CancelToken
fn clone(&self) -> CancelToken
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more