Skip to content

Commit d73f32f

Browse files
fix: Replace r2d2 with deadpool
This seems like it integrates nicer with async.
1 parent 64c8186 commit d73f32f

File tree

3 files changed

+79
-91
lines changed

3 files changed

+79
-91
lines changed

Cargo.lock

Lines changed: 58 additions & 75 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/databases/sqlite/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ description = "Implements the `DatabaseConnector` for an SQLite backend."
1111

1212
[dependencies]
1313
chrono = "0.4.30"
14-
diesel = { version = "2.2.3", features = ["chrono", "r2d2", "sqlite"] }
14+
diesel = { version = "2.2.3", features = ["chrono", "sqlite"] }
1515
diesel_migrations = "2.2.0"
16+
deadpool-diesel = { version = "0.6.0", features = ["sqlite" ] }
17+
deadpool = "0.12.0"
18+
1619
serde = "1.0.184"
1720
serde_json = "1.0.29"
1821
thiserror = "2.0.0"

lib/databases/sqlite/src/databaseconn.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ use std::marker::PhantomData;
1818
use std::path::{Path, PathBuf};
1919

2020
use chrono::{NaiveDateTime, Utc};
21+
use deadpool::managed::Object;
22+
use deadpool_diesel::{Manager, Pool, PoolError};
2123
use diesel::connection::LoadConnection;
2224
use diesel::migration::MigrationSource;
23-
use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
2425
use diesel::sqlite::Sqlite;
2526
use diesel::{Connection as _, ExpressionMethods as _, QueryDsl as _, RunQueryDsl as _, SelectableHelper as _, SqliteConnection};
2627
use diesel_migrations::{FileBasedMigrations, MigrationHarness as _};
@@ -45,7 +46,7 @@ pub enum DatabaseError {
4546
Connect {
4647
path: PathBuf,
4748
#[source]
48-
err: diesel::r2d2::PoolError,
49+
err: PoolError,
4950
},
5051
/// Failed to connect to the database when creating it.
5152
#[error("Failed to first-time connect to backend database {:?}", path.display())]
@@ -87,7 +88,7 @@ pub enum DatabaseError {
8788
PoolCreate {
8889
path: PathBuf,
8990
#[source]
90-
err: diesel::r2d2::PoolError,
91+
err: deadpool::managed::BuildError,
9192
},
9293
}
9394

@@ -191,7 +192,7 @@ pub struct SQLiteDatabase<C> {
191192
/// The path to the file that we represent. Only retained during runtime for debugging.
192193
path: PathBuf,
193194
/// The pool of connections.
194-
pool: Pool<ConnectionManager<SqliteConnection>>,
195+
pool: Pool<deadpool_diesel::Manager<SqliteConnection>>,
195196
/// Remembers the type of content used.
196197
_content: PhantomData<C>,
197198
}
@@ -242,7 +243,8 @@ impl<C> SQLiteDatabase<C> {
242243

243244
// Create the pool
244245
debug!("Connecting to database {:?}...", path.display());
245-
let pool: Pool<_> = match Pool::new(ConnectionManager::new(path.display().to_string())) {
246+
let manager = Manager::new(path.display().to_string(), deadpool::Runtime::Tokio1);
247+
let pool = match Pool::builder(manager).build() {
246248
Ok(pool) => pool,
247249
Err(err) => return Err(DatabaseError::PoolCreate { path, err }),
248250
};
@@ -288,7 +290,7 @@ impl<C: Send + Sync + DeserializeOwned + Serialize> DatabaseConnector for SQLite
288290
async move {
289291
// Attempt to get a connection from the pool
290292
debug!("Creating new connection to SQLite database {:?}...", self.path.display());
291-
match self.pool.get() {
293+
match self.pool.get().await {
292294
Ok(conn) => Ok(SQLiteConnection { path: &self.path, conn, user, _content: PhantomData }),
293295
Err(err) => Err(DatabaseError::Connect { path: self.path.clone(), err }),
294296
}
@@ -303,7 +305,7 @@ pub struct SQLiteConnection<'a, C> {
303305
/// The path to the file that we represent. Only retained during runtime for debugging.
304306
path: &'a Path,
305307
/// The connection we wrap.
306-
conn: PooledConnection<ConnectionManager<SqliteConnection>>,
308+
conn: Object<Manager<SqliteConnection>>,
307309
/// The user that is doing everything in this connection.
308310
user: &'a User,
309311
/// Remembers the type of content chosen for this connection.
@@ -362,7 +364,7 @@ impl<C: Send + DeserializeOwned + Serialize> DatabaseConnection for SQLiteConnec
362364

363365
debug!("Starting transaction...");
364366
tokio::task::block_in_place(move || {
365-
self.conn.exclusive_transaction(|conn| -> Result<u64, Self::Error> {
367+
self.conn.lock().unwrap().exclusive_transaction(|conn| -> Result<u64, Self::Error> {
366368
// Trick the compiler into moving the span too
367369
let _span = span;
368370

@@ -412,7 +414,7 @@ impl<C: Send + DeserializeOwned + Serialize> DatabaseConnection for SQLiteConnec
412414

413415
debug!("Starting transaction...");
414416
tokio::task::block_in_place(move || {
415-
self.conn.exclusive_transaction(|conn| -> Result<(), Self::Error> {
417+
self.conn.lock().unwrap().exclusive_transaction(|conn| -> Result<(), Self::Error> {
416418
// Trick the compiler into moving the span too
417419
let _span = span;
418420

@@ -445,7 +447,7 @@ impl<C: Send + DeserializeOwned + Serialize> DatabaseConnection for SQLiteConnec
445447

446448
debug!("Starting transaction...");
447449
tokio::task::block_in_place(move || {
448-
self.conn.exclusive_transaction(|conn| -> Result<(), Self::Error> {
450+
self.conn.lock().unwrap().exclusive_transaction(|conn| -> Result<(), Self::Error> {
449451
// Get the current active version, if any
450452
let av = match Self::_get_active_version(self.path, conn)? {
451453
Some(av) => av,
@@ -482,7 +484,7 @@ impl<C: Send + DeserializeOwned + Serialize> DatabaseConnection for SQLiteConnec
482484
match policy::policies
483485
.order_by(crate::schema::policies::dsl::created_at.desc())
484486
.select((policy::description, policy::name, policy::language, policy::version, policy::creator, policy::created_at))
485-
.load::<(String, String, String, i64, String, NaiveDateTime)>(&mut self.conn)
487+
.load::<(String, String, String, i64, String, NaiveDateTime)>(&mut *self.conn.lock().unwrap())
486488
{
487489
Ok(r) => Ok(r
488490
.into_iter()
@@ -505,7 +507,7 @@ impl<C: Send + DeserializeOwned + Serialize> DatabaseConnection for SQLiteConnec
505507
let _span = span!(Level::INFO, "SQLiteConnection::get_active");
506508

507509
// Do a call to get the active, if any
508-
tokio::task::block_in_place(move || Self::_get_active_version(self.path, &mut self.conn))
510+
tokio::task::block_in_place(move || Self::_get_active_version(self.path, &mut *self.conn.lock().unwrap()))
509511
}
510512
}
511513

@@ -521,7 +523,7 @@ impl<C: Send + DeserializeOwned + Serialize> DatabaseConnection for SQLiteConnec
521523
.limit(1)
522524
.order_by(crate::schema::active_version::dsl::activated_on.desc())
523525
.select(SqliteActiveVersion::as_select())
524-
.load(&mut self.conn)
526+
.load(&mut *self.conn.lock().unwrap())
525527
{
526528
Ok(mut r) => match r.pop() {
527529
Some(av) => {
@@ -550,7 +552,7 @@ impl<C: Send + DeserializeOwned + Serialize> DatabaseConnection for SQLiteConnec
550552
.filter(crate::schema::policies::dsl::version.eq(version as i64))
551553
.order_by(crate::schema::policies::dsl::created_at.desc())
552554
.select((policy::description, policy::name, policy::language, policy::version, policy::creator, policy::created_at))
553-
.load::<(String, String, String, i64, String, NaiveDateTime)>(&mut self.conn)
555+
.load::<(String, String, String, i64, String, NaiveDateTime)>(&mut *self.conn.lock().unwrap())
554556
{
555557
Ok(mut r) => {
556558
// Extract the version itself
@@ -588,7 +590,7 @@ impl<C: Send + DeserializeOwned + Serialize> DatabaseConnection for SQLiteConnec
588590
.filter(crate::schema::policies::dsl::version.eq(version as i64))
589591
.order_by(crate::schema::policies::dsl::created_at.desc())
590592
.select((policy::name, policy::content))
591-
.load::<(String, String)>(&mut self.conn)
593+
.load::<(String, String)>(&mut *self.conn.lock().unwrap())
592594
{
593595
Ok(mut r) => {
594596
// Extract the version itself

0 commit comments

Comments
 (0)