Support cancellation in synchronous client #531
Conversation
| @@ -451,6 +450,18 @@ impl Client { | |||
| Ok(Transaction::new(self)) | |||
| } | |||
|
|
|||
| /// Not part of the public API of this crate. | |||
| #[doc(hidden)] | |||
| pub fn cancel_token(&self) -> CancelToken { | |||
sfackler
Dec 27, 2019
Owner
I've tried to avoid having private APIs in tokio-postgres just for postgres's sake. I think it's reasonable for this to just be a public API though. I wonder if we should just deprecate cancel_query and cancel_query_raw?
I've tried to avoid having private APIs in tokio-postgres just for postgres's sake. I think it's reasonable for this to just be a public API though. I wonder if we should just deprecate cancel_query and cancel_query_raw?
benesch
Dec 27, 2019
Author
Contributor
That certainly works for me, if it works for you! The only potential downside is that the current (async-only) API can guarantee at compile time that the connection you're sending the cancellation request on is still alive. The CancelToken, of course, can outlive the connection it was created from. Given how racy cancellation is in general, I don't think this is a big deal, but wanted to raise it nonetheless.
That certainly works for me, if it works for you! The only potential downside is that the current (async-only) API can guarantee at compile time that the connection you're sending the cancellation request on is still alive. The CancelToken, of course, can outlive the connection it was created from. Given how racy cancellation is in general, I don't think this is a big deal, but wanted to raise it nonetheless.
sfackler
Dec 27, 2019
Owner
It cannot guarantee that. cancel_query is defined on Client, not Connection.
It cannot guarantee that. cancel_query is defined on Client, not Connection.
benesch
Dec 28, 2019
Author
Contributor
Oh, good point, I forgot that the client lifetime wasn't tied to the connection lifetime. 👍
Oh, good point, I forgot that the client lifetime wasn't tied to the connection lifetime.
|
Ok, updated to deprecate the |
| @@ -125,6 +126,8 @@ mod bind; | |||
| #[cfg(feature = "runtime")] | |||
| mod cancel_query; | |||
| mod cancel_query_raw; | |||
| #[doc(hidden)] | |||
| pub mod cancel_token; | |||
sfackler
Dec 29, 2019
Owner
This can be private.
This can be private.
benesch
Dec 29, 2019
Author
Contributor
Oops, thanks.
Oops, thanks.
| @@ -304,6 +304,7 @@ async fn cancel_query_raw() { | |||
| let client = connect("user=postgres").await; | |||
|
|
|||
| let socket = TcpStream::connect("127.0.0.1:5433").await.unwrap(); | |||
| #[allow(deprecated)] | |||
| let cancel = client.cancel_query_raw(socket, NoTls); | |||
sfackler
Dec 29, 2019
Owner
Let's just switch these tests over to use the new APIs.
Let's just switch these tests over to use the new APIs.
benesch
Dec 29, 2019
Author
Contributor
You bet. Done.
You bet. Done.
|
Thanks! |
The tl;dr is that it'd be nice to be able to send cancellation requests when using the synchronous client. This is a first pass at implementing that. Not sure if it's mergeable in its current form, as it relies on exposing some private bits of
tokio-postgresfor the sole use of thepostgrescrate, and then marking those bits asdoc(hidden).