diesel_async/pooled_connection/
mod.rs1use crate::{AsyncConnection, TransactionManager};
9use diesel::QueryResult;
10use futures_core::future::BoxFuture;
11use futures_util::FutureExt;
12use std::borrow::Cow;
13use std::fmt;
14use std::future::Future;
15
16#[cfg(feature = "bb8")]
17pub mod bb8;
18#[cfg(feature = "deadpool")]
19pub mod deadpool;
20#[cfg(feature = "mobc")]
21pub mod mobc;
22
23#[derive(Debug)]
25pub enum PoolError {
26 ConnectionError(diesel::result::ConnectionError),
28
29 QueryError(diesel::result::Error),
31}
32
33impl fmt::Display for PoolError {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 match *self {
36 PoolError::ConnectionError(ref e) => e.fmt(f),
37 PoolError::QueryError(ref e) => e.fmt(f),
38 }
39 }
40}
41
42impl std::error::Error for PoolError {
43 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
44 match self {
45 PoolError::ConnectionError(connection_error) => Some(connection_error),
46 PoolError::QueryError(error) => Some(error),
47 }
48 }
49}
50
51impl From<diesel::result::ConnectionError> for PoolError {
52 fn from(value: diesel::result::ConnectionError) -> Self {
53 PoolError::ConnectionError(value)
54 }
55}
56
57impl From<diesel::result::Error> for PoolError {
58 fn from(value: diesel::result::Error) -> Self {
59 PoolError::QueryError(value)
60 }
61}
62
63pub type SetupCallback<C> =
65 Box<dyn Fn(&str) -> BoxFuture<diesel::ConnectionResult<C>> + Send + Sync>;
66
67pub type RecycleCheckCallback<C> = dyn Fn(&mut C) -> BoxFuture<QueryResult<()>> + Send + Sync;
69
70#[derive(Default)]
72pub enum RecyclingMethod<C> {
73 Fast,
79 #[default]
83 Verified,
84 CustomQuery(Cow<'static, str>),
86 CustomFunction(Box<RecycleCheckCallback<C>>),
90}
91
92impl<C: fmt::Debug> fmt::Debug for RecyclingMethod<C> {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 match self {
95 Self::Fast => write!(f, "Fast"),
96 Self::Verified => write!(f, "Verified"),
97 Self::CustomQuery(arg0) => f.debug_tuple("CustomQuery").field(arg0).finish(),
98 Self::CustomFunction(_) => f.debug_tuple("CustomFunction").finish(),
99 }
100 }
101}
102
103#[non_exhaustive]
109pub struct ManagerConfig<C> {
110 pub recycling_method: RecyclingMethod<C>,
112 pub custom_setup: SetupCallback<C>,
118}
119
120impl<C> Default for ManagerConfig<C>
121where
122 C: AsyncConnection + 'static,
123{
124 fn default() -> Self {
125 Self {
126 recycling_method: Default::default(),
127 custom_setup: Box::new(|url| C::establish(url).boxed()),
128 }
129 }
130}
131
132#[allow(dead_code)]
139pub struct AsyncDieselConnectionManager<C> {
140 connection_url: String,
141 manager_config: ManagerConfig<C>,
142}
143
144impl<C> fmt::Debug for AsyncDieselConnectionManager<C> {
145 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146 write!(
147 f,
148 "AsyncDieselConnectionManager<{}>",
149 std::any::type_name::<C>()
150 )
151 }
152}
153
154impl<C> AsyncDieselConnectionManager<C>
155where
156 C: AsyncConnection + 'static,
157{
158 #[must_use]
161 pub fn new(connection_url: impl Into<String>) -> Self
162 where
163 C: AsyncConnection + 'static,
164 {
165 Self::new_with_config(connection_url, Default::default())
166 }
167
168 #[must_use]
172 pub fn new_with_config(
173 connection_url: impl Into<String>,
174 manager_config: ManagerConfig<C>,
175 ) -> Self {
176 Self {
177 connection_url: connection_url.into(),
178 manager_config,
179 }
180 }
181}
182
183#[doc(hidden)]
184pub trait PoolableConnection: AsyncConnection {
185 fn ping(
190 &mut self,
191 config: &RecyclingMethod<Self>,
192 ) -> impl Future<Output = diesel::QueryResult<()>> + Send
193 where
194 for<'a> Self: 'a,
195 for<'a> diesel::dsl::select<diesel::dsl::AsExprOf<i32, diesel::sql_types::Integer>>:
196 crate::methods::LoadQuery<'a, Self, i32>,
197 diesel::query_builder::SqlQuery: crate::methods::ExecuteDsl<Self>,
198 {
199 use crate::run_query_dsl::RunQueryDsl;
200 use diesel::IntoSql;
201
202 async move {
203 match config {
204 RecyclingMethod::Fast => Ok(()),
205 RecyclingMethod::Verified => {
206 diesel::select(1_i32.into_sql::<diesel::sql_types::Integer>())
207 .get_result::<i32>(self)
208 .await
209 .map(|_| ())
210 }
211 RecyclingMethod::CustomQuery(query) => diesel::sql_query(query.as_ref())
212 .execute(self)
213 .await
214 .map(|_| ()),
215 RecyclingMethod::CustomFunction(c) => c(self).await,
216 }
217 }
218 }
219
220 fn is_broken(&mut self) -> bool {
229 Self::TransactionManager::is_broken_transaction_manager(self)
230 }
231}