-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathasync_traits.rs
501 lines (457 loc) · 17.9 KB
/
async_traits.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! Async versions of traits for issuing Diesel queries.
use crate::connection::Connection;
use async_trait::async_trait;
use diesel::{
connection::{
Connection as DieselConnection, SimpleConnection, TransactionManager,
TransactionManagerStatus,
},
dsl::Limit,
query_dsl::{
methods::{ExecuteDsl, LimitDsl, LoadQuery},
RunQueryDsl,
},
r2d2::R2D2Connection,
result::Error as DieselError,
};
use futures::future::BoxFuture;
use futures::future::FutureExt;
use std::any::Any;
use std::future::Future;
use std::sync::Arc;
use std::sync::MutexGuard;
use tokio::task::spawn_blocking;
/// An async variant of [`diesel::connection::SimpleConnection`].
#[async_trait]
pub trait AsyncSimpleConnection<Conn>
where
Conn: 'static + SimpleConnection,
{
async fn batch_execute_async(&self, query: &str) -> Result<(), DieselError>;
}
#[cfg(feature = "cockroach")]
fn retryable_error(err: &DieselError) -> bool {
use diesel::result::DatabaseErrorKind::SerializationFailure;
match err {
DieselError::DatabaseError(SerializationFailure, _boxed_error_information) => true,
_ => false,
}
}
/// An async variant of [`diesel::r2d2::R2D2Connection`].
#[async_trait]
pub trait AsyncR2D2Connection<Conn>: AsyncConnection<Conn>
where
Conn: 'static + DieselConnection + R2D2Connection,
Self: Send + Sized + 'static,
{
async fn ping_async(&mut self) -> diesel::result::QueryResult<()> {
self.as_async_conn().run(|conn| conn.ping()).await
}
async fn is_broken_async(&mut self) -> bool {
self.as_async_conn()
.run(|conn| Ok::<bool, ()>(conn.is_broken()))
.await
.unwrap()
}
}
/// An async variant of [`diesel::connection::Connection`].
#[async_trait]
pub trait AsyncConnection<Conn>: AsyncSimpleConnection<Conn>
where
Conn: 'static + DieselConnection,
Self: Send + Sized + 'static,
{
#[doc(hidden)]
fn get_owned_connection(&self) -> Self;
#[doc(hidden)]
fn as_sync_conn(&self) -> MutexGuard<'_, Conn>;
#[doc(hidden)]
fn as_async_conn(&self) -> &Connection<Conn>;
/// Runs the function `f` in an context where blocking is safe.
async fn run<R, E, Func>(&self, f: Func) -> Result<R, E>
where
R: Send + 'static,
E: Send + 'static,
Func: FnOnce(&mut Conn) -> Result<R, E> + Send + 'static,
{
let connection = self.get_owned_connection();
connection.run_with_connection(f).await
}
#[doc(hidden)]
async fn run_with_connection<R, E, Func>(self, f: Func) -> Result<R, E>
where
R: Send + 'static,
E: Send + 'static,
Func: FnOnce(&mut Conn) -> Result<R, E> + Send + 'static,
{
spawn_blocking(move || f(&mut *self.as_sync_conn()))
.await
.unwrap() // Propagate panics
}
#[doc(hidden)]
async fn run_with_shared_connection<R, E, Func>(self: &Arc<Self>, f: Func) -> Result<R, E>
where
R: Send + 'static,
E: Send + 'static,
Func: FnOnce(&mut Conn) -> Result<R, E> + Send + 'static,
{
let conn = self.clone();
spawn_blocking(move || f(&mut *conn.as_sync_conn()))
.await
.unwrap() // Propagate panics
}
#[doc(hidden)]
async fn transaction_depth(&self) -> Result<u32, DieselError> {
let conn = self.get_owned_connection();
Self::run_with_connection(conn, |conn| {
match Conn::TransactionManager::transaction_manager_status_mut(&mut *conn) {
TransactionManagerStatus::Valid(status) => {
Ok(status.transaction_depth().map(|d| d.into()).unwrap_or(0))
}
TransactionManagerStatus::InError => Err(DieselError::BrokenTransactionManager),
}
})
.await
}
// Diesel's "begin_transaction" chooses whether to issue "BEGIN" or a
// "SAVEPOINT" depending on the transaction depth.
//
// This method is a wrapper around that call, with validation that
// we're actually issuing the BEGIN statement here.
#[doc(hidden)]
async fn start_transaction(self: &Arc<Self>) -> Result<(), DieselError> {
if self.transaction_depth().await? != 0 {
return Err(DieselError::AlreadyInTransaction);
}
self.run_with_shared_connection(|conn| Conn::TransactionManager::begin_transaction(conn))
.await?;
Ok(())
}
// Diesel's "begin_transaction" chooses whether to issue "BEGIN" or a
// "SAVEPOINT" depending on the transaction depth.
//
// This method is a wrapper around that call, with validation that
// we're actually issuing our first SAVEPOINT here.
#[doc(hidden)]
async fn add_retry_savepoint(self: &Arc<Self>) -> Result<(), DieselError> {
match self.transaction_depth().await? {
0 => return Err(DieselError::NotInTransaction),
1 => (),
_ => return Err(DieselError::AlreadyInTransaction),
};
self.run_with_shared_connection(|conn| Conn::TransactionManager::begin_transaction(conn))
.await?;
Ok(())
}
#[doc(hidden)]
async fn commit_transaction(self: &Arc<Self>) -> Result<(), DieselError> {
self.run_with_shared_connection(|conn| Conn::TransactionManager::commit_transaction(conn))
.await?;
Ok(())
}
#[doc(hidden)]
async fn rollback_transaction(self: &Arc<Self>) -> Result<(), DieselError> {
self.run_with_shared_connection(|conn| {
Conn::TransactionManager::rollback_transaction(conn)
})
.await?;
Ok(())
}
/// Issues a function `f` as a transaction.
///
/// If it fails, asynchronously calls `retry` to decide if to retry.
///
/// This function throws an error if it is called from within an existing
/// transaction.
#[cfg(feature = "cockroach")]
async fn transaction_async_with_retry<R, Func, Fut, RetryFut, RetryFunc, 'a>(
&'a self,
f: Func,
retry: RetryFunc,
) -> Result<R, DieselError>
where
R: Any + Send + 'static,
Fut: FutureExt<Output = Result<R, DieselError>> + Send,
Func: (Fn(Connection<Conn>) -> Fut) + Send + Sync,
RetryFut: FutureExt<Output = bool> + Send,
RetryFunc: Fn() -> RetryFut + Send + Sync,
{
// This function sure has a bunch of generic parameters, which can cause
// a lot of code to be generated, and can slow down compile-time.
//
// This API intends to provide a convenient, generic, shim over the
// dynamic "transaction_async_with_retry_inner" function below, which
// should avoid being generic. The goal here is to instantiate only one
// significant "body" of this function, while retaining flexibility for
// clients using this library.
// Box the functions, and box the return value.
let f = |conn| {
f(conn)
.map(|result| result.map(|r| Box::new(r) as Box<dyn Any + Send>))
.boxed()
};
let retry = || retry().boxed();
// Call the dynamically dispatched function, then retrieve the return
// value out of a Box.
self.transaction_async_with_retry_inner(&f, &retry)
.await
.map(|v| *v.downcast::<R>().expect("Should be an 'R' type"))
}
// NOTE: This function intentionally avoids all generics!
#[cfg(feature = "cockroach")]
async fn transaction_async_with_retry_inner(
&self,
f: &(dyn Fn(Connection<Conn>) -> BoxFuture<'_, Result<Box<dyn Any + Send>, DieselError>>
+ Send
+ Sync),
retry: &(dyn Fn() -> BoxFuture<'_, bool> + Send + Sync),
) -> Result<Box<dyn Any + Send>, DieselError> {
// Check out a connection once, and use it for the duration of the
// operation.
let conn = Arc::new(self.get_owned_connection());
// Refer to CockroachDB's guide on advanced client-side transaction
// retries for the full context:
// https://www.cockroachlabs.com/docs/v23.1/advanced-client-side-transaction-retries
//
// In short, they expect a particular name for this savepoint, but
// Diesel has Opinions on savepoint names, so we use this session
// variable to identify that any name is valid.
//
// TODO: It may be preferable to set this once per connection -- but
// that'll require more interaction with how sessions with the database
// are constructed.
Self::start_transaction(&conn).await?;
conn.run_with_shared_connection(|conn| {
conn.batch_execute("SET LOCAL force_savepoint_restart = true")
})
.await?;
loop {
// Add a SAVEPOINT to which we can later return.
Self::add_retry_savepoint(&conn).await?;
let async_conn = Connection(Self::as_async_conn(&conn).0.clone());
match f(async_conn).await {
Ok(value) => {
// The user-level operation succeeded: try to commit the
// transaction by RELEASE-ing the retry savepoint.
if let Err(err) = Self::commit_transaction(&conn).await {
// Diesel's implementation of "commit_transaction"
// calls "rollback_transaction" in the error path.
//
// We're still in the transaction, but we at least
// tried to ROLLBACK to our savepoint.
if !retryable_error(&err) || !retry().await {
// Bail: ROLLBACK the initial BEGIN statement too.
let _ = Self::rollback_transaction(&conn).await;
return Err(err);
}
// ROLLBACK happened, we want to retry.
continue;
}
// Commit the top-level transaction too.
Self::commit_transaction(&conn).await?;
return Ok(value);
}
Err(user_error) => {
// The user-level operation failed: ROLLBACK to the retry
// savepoint.
if let Err(first_rollback_err) = Self::rollback_transaction(&conn).await {
// If we fail while rolling back, prioritize returning
// the ROLLBACK error over the user errors.
return match Self::rollback_transaction(&conn).await {
Ok(()) => Err(first_rollback_err),
Err(second_rollback_err) => Err(second_rollback_err),
};
}
// We rolled back to the retry savepoint, and now want to
// retry.
if retryable_error(&user_error) && retry().await {
continue;
}
// If we aren't retrying, ROLLBACK the BEGIN statement too.
return match Self::rollback_transaction(&conn).await {
Ok(()) => Err(user_error),
Err(err) => Err(err),
};
}
}
}
}
async fn transaction_async<R, E, Func, Fut, 'a>(&'a self, f: Func) -> Result<R, E>
where
R: Send + 'static,
E: From<DieselError> + Send + 'static,
Fut: Future<Output = Result<R, E>> + Send,
Func: FnOnce(Connection<Conn>) -> Fut + Send,
{
// This function sure has a bunch of generic parameters, which can cause
// a lot of code to be generated, and can slow down compile-time.
//
// This API intends to provide a convenient, generic, shim over the
// dynamic "transaction_async_with_retry_inner" function below, which
// should avoid being generic. The goal here is to instantiate only one
// significant "body" of this function, while retaining flexibility for
// clients using this library.
let f = Box::new(move |conn| {
f(conn)
.map(|result| result.map(|r| Box::new(r) as Box<dyn Any + Send>))
.boxed()
});
self.transaction_async_inner(f)
.await
.map(|v| *v.downcast::<R>().expect("Should be an 'R' type"))
}
// NOTE: This function intentionally avoids as many generic parameters as possible
async fn transaction_async_inner<'a, E>(
&'a self,
f: Box<
dyn FnOnce(Connection<Conn>) -> BoxFuture<'a, Result<Box<dyn Any + Send>, E>>
+ Send
+ 'a,
>,
) -> Result<Box<dyn Any + Send>, E>
where
E: From<DieselError> + Send + 'static,
{
// Check out a connection once, and use it for the duration of the
// operation.
let conn = Arc::new(self.get_owned_connection());
// This function mimics the implementation of:
// https://docs.diesel.rs/master/diesel/connection/trait.TransactionManager.html#method.transaction
//
// However, it modifies all callsites to instead issue
// known-to-be-synchronous operations from an asynchronous context.
conn.run_with_shared_connection(|conn| {
Conn::TransactionManager::begin_transaction(conn).map_err(E::from)
})
.await?;
// TODO: The ideal interface would pass the "async_conn" object to the
// underlying function "f" by reference.
//
// This would prevent the user-supplied closure + future from using the
// connection *beyond* the duration of the transaction, which would be
// bad.
//
// However, I'm struggling to get these lifetimes to work properly. If
// you can figure out a way to convince that the reference lives long
// enough to be referenceable by a Future, but short enough that we can
// guarantee it doesn't live persist after this function returns, feel
// free to make that change.
let async_conn = Connection(Self::as_async_conn(&conn).0.clone());
match f(async_conn).await {
Ok(value) => {
conn.run_with_shared_connection(|conn| {
Conn::TransactionManager::commit_transaction(conn).map_err(E::from)
})
.await?;
Ok(value)
}
Err(user_error) => {
match conn
.run_with_shared_connection(|conn| {
Conn::TransactionManager::rollback_transaction(conn).map_err(E::from)
})
.await
{
Ok(()) => Err(user_error),
Err(err) => Err(err),
}
}
}
}
}
/// An async variant of [`diesel::query_dsl::RunQueryDsl`].
#[async_trait]
pub trait AsyncRunQueryDsl<Conn, AsyncConn>
where
Conn: 'static + DieselConnection,
{
async fn execute_async(self, asc: &AsyncConn) -> Result<usize, DieselError>
where
Self: ExecuteDsl<Conn>;
async fn load_async<U>(self, asc: &AsyncConn) -> Result<Vec<U>, DieselError>
where
U: Send + 'static,
Self: LoadQuery<'static, Conn, U>;
async fn get_result_async<U>(self, asc: &AsyncConn) -> Result<U, DieselError>
where
U: Send + 'static,
Self: LoadQuery<'static, Conn, U>;
async fn get_results_async<U>(self, asc: &AsyncConn) -> Result<Vec<U>, DieselError>
where
U: Send + 'static,
Self: LoadQuery<'static, Conn, U>;
async fn first_async<U>(self, asc: &AsyncConn) -> Result<U, DieselError>
where
U: Send + 'static,
Self: LimitDsl,
Limit<Self>: LoadQuery<'static, Conn, U>;
}
#[async_trait]
impl<T, AsyncConn, Conn> AsyncRunQueryDsl<Conn, AsyncConn> for T
where
T: 'static + Send + RunQueryDsl<Conn>,
Conn: 'static + DieselConnection,
AsyncConn: Send + Sync + AsyncConnection<Conn>,
{
async fn execute_async(self, asc: &AsyncConn) -> Result<usize, DieselError>
where
Self: ExecuteDsl<Conn>,
{
asc.run(|conn| self.execute(conn)).await
}
async fn load_async<U>(self, asc: &AsyncConn) -> Result<Vec<U>, DieselError>
where
U: Send + 'static,
Self: LoadQuery<'static, Conn, U>,
{
asc.run(|conn| self.load(conn)).await
}
async fn get_result_async<U>(self, asc: &AsyncConn) -> Result<U, DieselError>
where
U: Send + 'static,
Self: LoadQuery<'static, Conn, U>,
{
asc.run(|conn| self.get_result(conn)).await
}
async fn get_results_async<U>(self, asc: &AsyncConn) -> Result<Vec<U>, DieselError>
where
U: Send + 'static,
Self: LoadQuery<'static, Conn, U>,
{
asc.run(|conn| self.get_results(conn)).await
}
async fn first_async<U>(self, asc: &AsyncConn) -> Result<U, DieselError>
where
U: Send + 'static,
Self: LimitDsl,
Limit<Self>: LoadQuery<'static, Conn, U>,
{
asc.run(|conn| self.first(conn)).await
}
}
#[async_trait]
pub trait AsyncSaveChangesDsl<Conn, AsyncConn>
where
Conn: 'static + DieselConnection,
{
async fn save_changes_async<Output>(self, asc: &AsyncConn) -> Result<Output, DieselError>
where
Self: Sized,
Conn: diesel::query_dsl::UpdateAndFetchResults<Self, Output>,
Output: Send + 'static;
}
#[async_trait]
impl<T, AsyncConn, Conn> AsyncSaveChangesDsl<Conn, AsyncConn> for T
where
T: 'static + Send + Sync + diesel::SaveChangesDsl<Conn>,
Conn: 'static + DieselConnection,
AsyncConn: Send + Sync + AsyncConnection<Conn>,
{
async fn save_changes_async<Output>(self, asc: &AsyncConn) -> Result<Output, DieselError>
where
Conn: diesel::query_dsl::UpdateAndFetchResults<Self, Output>,
Output: Send + 'static,
{
asc.run(|conn| self.save_changes(conn)).await
}
}