Skip to content

Commit 2cccc84

Browse files
committed
Minor improvements
1 parent 60dbc78 commit 2cccc84

File tree

4 files changed

+28
-29
lines changed

4 files changed

+28
-29
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ mod transaction_manager;
107107
pub use self::mysql::AsyncMysqlConnection;
108108
#[cfg(feature = "mysql")]
109109
#[doc(inline)]
110-
pub use self::mysql::CancelToken;
110+
pub use self::mysql::MysqlCancelToken;
111111
#[cfg(feature = "postgres")]
112112
#[doc(inline)]
113113
pub use self::pg::AsyncPgConnection;

src/mysql/cancel_token.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use crate::mysql::error_helper::ErrorHelper;
66
/// The capability to request cancellation of in-progress queries on a
77
/// connection.
88
#[derive(Clone)]
9-
pub struct CancelToken {
9+
pub struct MysqlCancelToken {
1010
pub(crate) opts: Opts,
1111
pub(crate) kill_id: u32,
1212
}
1313

14-
impl CancelToken {
14+
impl MysqlCancelToken {
1515
/// Attempts to cancel the in-progress query on the connection associated
1616
/// with this `CancelToken`.
1717
///
@@ -22,7 +22,7 @@ impl CancelToken {
2222
/// cancellation request will reach the server before the query terminates
2323
/// normally, or that the connection associated with this token is still
2424
/// active.
25-
pub async fn cancel_query(&self) -> Result<(), diesel::result::ConnectionError> {
25+
pub async fn cancel_query(&self) -> diesel::result::ConnectionResult<()> {
2626
let builder = OptsBuilder::from_opts(self.opts.clone());
2727

2828
let conn = mysql_async::Conn::new(builder).await.map_err(ErrorHelper)?;

src/mysql/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ mod error_helper;
2424
mod row;
2525
mod serialize;
2626

27-
pub use self::cancel_token::CancelToken;
27+
pub use self::cancel_token::MysqlCancelToken;
2828
use self::error_helper::ErrorHelper;
2929
use self::row::MysqlRow;
3030
use self::serialize::ToSqlHelper;
@@ -257,11 +257,11 @@ impl AsyncMysqlConnection {
257257
}
258258

259259
/// Constructs a cancellation token that can later be used to request cancellation of a query running on the connection associated with this client.
260-
pub fn cancel_token(&self) -> CancelToken {
260+
pub fn cancel_token(&self) -> MysqlCancelToken {
261261
let kill_id = self.conn.id();
262262
let opts = self.conn.opts().clone();
263263

264-
CancelToken { kill_id, opts }
264+
MysqlCancelToken { kill_id, opts }
265265
}
266266

267267
fn with_prepared_statement<'conn, T, F, R>(

tests/lib.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,13 @@ async fn postgres_cancel_token() {
168168
#[cfg(feature = "mysql")]
169169
#[tokio::test]
170170
async fn mysql_cancel_token() {
171-
use std::time::Duration;
172-
173171
use diesel::result::{DatabaseErrorKind, Error};
172+
use std::time::Duration;
174173

175174
let (sender, receiver) = tokio::sync::oneshot::channel();
176175

177-
// execute a long-running query on a separate thread
178-
let task = tokio::spawn(async move {
176+
// execute a long-running query in a separate future
177+
let query_future = async move {
179178
let conn = &mut connection().await;
180179
let token = conn.cancel_token();
181180

@@ -187,26 +186,26 @@ async fn mysql_cancel_token() {
187186
diesel::dsl::sql::<diesel::sql_types::Integer>("SELECT SLEEP(5)")
188187
.load::<i32>(conn)
189188
.await
190-
});
191-
192-
// wait for the cancellation token to be sent
193-
if let Ok(token) = receiver.await {
194-
// give the query time to start before invoking the token
195-
tokio::time::sleep(Duration::from_millis(500)).await;
196-
token.cancel_query().await.unwrap();
197-
}
189+
};
190+
let cancel_future = async move {
191+
// wait for the cancellation token to be sent
192+
if let Ok(token) = receiver.await {
193+
// give the query time to start before invoking the token
194+
tokio::time::sleep(Duration::from_millis(100)).await;
195+
token.cancel_query().await.unwrap();
196+
} else {
197+
panic!("Failed to receive cancel token");
198+
}
199+
};
200+
201+
let (task, _) = tokio::join!(query_future, cancel_future);
198202

199203
// make sure the query task resulted in a cancellation error or a return value of 1:
200-
match task.await.unwrap() {
201-
Err(e) => match e {
202-
Error::DatabaseError(DatabaseErrorKind::Unknown, v)
203-
if v.message() == "Query execution was interrupted" => {}
204-
_ => panic!("unexpected error: {:?}", e),
205-
},
206-
Ok(r) => match r[0] {
207-
1 => {}
208-
_ => panic!("query completed successfully without cancellation"),
209-
},
204+
match task {
205+
Err(Error::DatabaseError(DatabaseErrorKind::Unknown, v))
206+
if v.message() == "Query execution was interrupted" => {}
207+
Err(e) => panic!("unexpected error: {:?}", e),
208+
Ok(_) => panic!("query completed successfully without cancellation"),
210209
}
211210
}
212211

0 commit comments

Comments
 (0)