Skip to content

Commit

Permalink
Switch from bb8 to deadpool. Fixes #2765 (#2768)
Browse files Browse the repository at this point in the history
* Switch from bb8 to deadpool. Fixes #2765

* Remove unecessary deadpool dependency.

* Ignoring nodeinfo test.
  • Loading branch information
dessalines authored and Nutomic committed Apr 27, 2023
1 parent aba43fe commit ff6458b
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 34 deletions.
41 changes: 26 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions crates/db_schema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ doctest = false
[features]
full = ["diesel", "diesel-derive-newtype", "diesel_migrations", "bcrypt", "lemmy_utils",
"activitypub_federation", "sha2", "regex", "once_cell", "serde_json", "diesel_ltree",
"diesel-async", "bb8"]
"diesel-async"]

[dependencies]
chrono = { workspace = true }
Expand All @@ -31,15 +31,14 @@ bcrypt = { workspace = true, optional = true }
diesel = { workspace = true, features = ["postgres","chrono", "serde_json"], optional = true }
diesel-derive-newtype = { workspace = true, optional = true }
diesel_migrations = { workspace = true, optional = true }
diesel-async = { workspace = true, features = ["postgres", "bb8"], optional = true }
diesel-async = { workspace = true, features = ["postgres", "deadpool"], optional = true }
sha2 = { workspace = true, optional = true }
regex = { workspace = true, optional = true }
once_cell = { workspace = true, optional = true }
diesel_ltree = { workspace = true, optional = true }
typed-builder = { workspace = true }
async-trait = { workspace = true }
tokio = { workspace = true }
bb8 = { version = "0.8.0", optional = true }
tracing = { workspace = true }
tracing-error = { workspace = true }

Expand Down
16 changes: 6 additions & 10 deletions crates/db_schema/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
SortType,
};
use activitypub_federation::{core::object_id::ObjectId, traits::ApubObject};
use bb8::PooledConnection;
use chrono::NaiveDateTime;
use diesel::{
backend::Backend,
Expand All @@ -19,7 +18,10 @@ use diesel::{
};
use diesel_async::{
pg::AsyncPgConnection,
pooled_connection::{bb8::Pool, AsyncDieselConnectionManager},
pooled_connection::{
deadpool::{Object as PooledConnection, Pool},
AsyncDieselConnectionManager,
},
};
use diesel_migrations::EmbeddedMigrations;
use lemmy_utils::{error::LemmyError, settings::structs::Settings};
Expand All @@ -34,9 +36,7 @@ pub const FETCH_LIMIT_MAX: i64 = 50;

pub type DbPool = Pool<AsyncPgConnection>;

pub async fn get_conn(
pool: &DbPool,
) -> Result<PooledConnection<AsyncDieselConnectionManager<AsyncPgConnection>>, DieselError> {
pub async fn get_conn(pool: &DbPool) -> Result<PooledConnection<AsyncPgConnection>, DieselError> {
pool.get().await.map_err(|e| QueryBuilderError(e.into()))
}

Expand Down Expand Up @@ -135,11 +135,7 @@ async fn build_db_pool_settings_opt(settings: Option<&Settings>) -> Result<DbPoo
let db_url = get_database_url(settings);
let pool_size = settings.map(|s| s.database.pool_size).unwrap_or(5);
let manager = AsyncDieselConnectionManager::<AsyncPgConnection>::new(&db_url);
let pool = Pool::builder()
.max_size(pool_size)
.min_idle(Some(1))
.build(manager)
.await?;
let pool = Pool::builder(manager).max_size(pool_size).build()?;

// If there's no settings, that means its a unit test, and migrations need to be run
if settings.is_none() {
Expand Down
2 changes: 1 addition & 1 deletion crates/db_views_actor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ full = ["lemmy_db_schema/full", "diesel", "diesel-async"]
[dependencies]
lemmy_db_schema = { workspace = true }
diesel = { workspace = true, features = ["postgres","chrono","serde_json"], optional = true }
diesel-async = { workspace = true, features = ["postgres", "bb8"], optional = true }
diesel-async = { workspace = true, features = ["postgres", "deadpool"], optional = true }
serde = { workspace = true }
typed-builder = { workspace = true }
2 changes: 1 addition & 1 deletion crates/db_views_moderator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ full = ["lemmy_db_schema/full", "diesel", "diesel-async"]
[dependencies]
lemmy_db_schema = { workspace = true }
diesel = { workspace = true, features = ["postgres","chrono","serde_json"], optional = true }
diesel-async = { workspace = true, features = ["postgres", "bb8"], optional = true }
diesel-async = { workspace = true, features = ["postgres", "deadpool"], optional = true }
serde = { workspace = true }
2 changes: 1 addition & 1 deletion crates/utils/src/settings/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub struct DatabaseConfig {
pub(super) database: String,
/// Maximum number of active sql connections
#[default(5)]
pub pool_size: u32,
pub pool_size: usize,
}

#[derive(Debug, Deserialize, Serialize, Clone, Document, SmartDefault)]
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ pub async fn start_lemmy_server() -> Result<(), LemmyError> {

let settings = SETTINGS.to_owned();

// Set up the bb8 connection pool
// Run the DB migrations
let db_url = get_database_url(Some(&settings));
run_migrations(&db_url);

// Run the migrations from code
// Set up the connection pool
let pool = build_db_pool(&settings).await?;

// Run the Code-required migrations
run_advanced_migrations(&pool, &settings).await?;

// Schedules various cleanup tasks for the DB
Expand Down
2 changes: 1 addition & 1 deletion src/scheduled_tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,4 @@ fn drop_ccnew_indexes(conn: &mut PgConnection) {
sql_query(drop_stmt)
.execute(conn)
.expect("drop ccnew indexes");
}
}

0 comments on commit ff6458b

Please sign in to comment.