feat: add database connection pooling

user-register
Jef Roosens 2023-05-16 09:00:12 +02:00
parent e834b3308a
commit 303e3ffd4e
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 34 additions and 7 deletions

21
Cargo.lock generated
View File

@ -422,6 +422,7 @@ checksum = "72eb77396836a4505da85bae0712fa324b74acfe1876d7c2f7e694ef3d0ee373"
dependencies = [ dependencies = [
"diesel_derives", "diesel_derives",
"libsqlite3-sys", "libsqlite3-sys",
"r2d2",
] ]
[[package]] [[package]]
@ -1073,6 +1074,17 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "r2d2"
version = "0.8.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51de85fb3fb6524929c8a2eb85e6b6d363de4e8c48f9e2c2eac4944abc181c93"
dependencies = [
"log",
"parking_lot",
"scheduled-thread-pool",
]
[[package]] [[package]]
name = "rand" name = "rand"
version = "0.8.5" version = "0.8.5"
@ -1231,6 +1243,15 @@ version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041"
[[package]]
name = "scheduled-thread-pool"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3cbc66816425a074528352f5789333ecff06ca41b36b0b0efdfbb29edc391a19"
dependencies = [
"parking_lot",
]
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.1.0" version = "1.1.0"

View File

@ -22,4 +22,4 @@ chrono = "*"
uuid = "*" uuid = "*"
poise = "0.5.5" poise = "0.5.5"
async-minecraft-ping = "0.8.0" async-minecraft-ping = "0.8.0"
diesel = { version = "2.0.4", features = ["sqlite", "returning_clauses_for_sqlite_3_35"] } diesel = { version = "2.0.4", features = ["sqlite", "returning_clauses_for_sqlite_3_35", "r2d2"] }

View File

@ -16,7 +16,7 @@ pub async fn register(
}; };
{ {
let mut conn = ctx.data().conn.lock().unwrap(); let mut conn = ctx.data().pool.get()?;
user_insert(&mut conn, &user); user_insert(&mut conn, &user);
} }
@ -26,7 +26,7 @@ pub async fn register(
#[poise::command(prefix_command, slash_command)] #[poise::command(prefix_command, slash_command)]
pub async fn registered(ctx: Context<'_>) -> Result<(), Error> { pub async fn registered(ctx: Context<'_>) -> Result<(), Error> {
let users = { let users = {
let mut conn = ctx.data().conn.lock().unwrap(); let mut conn = ctx.data().pool.get()?;
user_all(&mut conn) user_all(&mut conn)
}; };

View File

@ -6,6 +6,8 @@ use diesel::Connection;
use poise::serenity_prelude as serenity; use poise::serenity_prelude as serenity;
use std::sync::Mutex; use std::sync::Mutex;
use std::{env::var, time::Duration}; use std::{env::var, time::Duration};
use diesel::r2d2::{ConnectionManager, Pool};
use diesel::sqlite::SqliteConnection;
// Types used by all command functions // Types used by all command functions
type Error = Box<dyn std::error::Error + Send + Sync>; type Error = Box<dyn std::error::Error + Send + Sync>;
@ -14,7 +16,7 @@ type Context<'a> = poise::Context<'a, Data, Error>;
// Custom user data passed to all command functions // Custom user data passed to all command functions
pub struct Data { pub struct Data {
client: AffluencesClient, client: AffluencesClient,
conn: Mutex<diesel::sqlite::SqliteConnection>, pool: Pool<ConnectionManager<SqliteConnection>>
} }
async fn on_error(error: poise::FrameworkError<'_, Data, Error>) { async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
@ -34,6 +36,12 @@ async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
} }
} }
fn db_connection_pool(url: &str) -> Pool<ConnectionManager<SqliteConnection>> {
let manager = ConnectionManager::new(url);
Pool::builder().test_on_check_out(true).build(manager).expect("oops")
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
// FrameworkOptions contains all of poise's configuration option in one struct // FrameworkOptions contains all of poise's configuration option in one struct
@ -95,9 +103,7 @@ async fn main() {
poise::builtins::register_globally(ctx, &framework.options().commands).await?; poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data { Ok(Data {
client: AffluencesClient::new(), client: AffluencesClient::new(),
conn: Mutex::new( pool: db_connection_pool("affy.db")
diesel::sqlite::SqliteConnection::establish("affy.db").unwrap(),
),
}) })
}) })
}) })