From 8d57310ec663660ff37b4e4effebe51179b07cd4 Mon Sep 17 00:00:00 2001 From: Nell Date: Sun, 23 Aug 2026 11:02:08 +0200 Subject: [PATCH] init --- .gitignore | 2 +- src/core/mod.rs | 3 +++ src/database/database.rs | 34 +++++++++++++++++++++++++++++--- src/services/message_reaction.rs | 2 ++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 65a1d7c..074abff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ /target /.idea -*.db \ No newline at end of file +*.db* \ No newline at end of file diff --git a/src/core/mod.rs b/src/core/mod.rs index bc8f109..e8356a2 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -133,6 +133,9 @@ impl App { // (Note: join! supporte les handles déjà terminés ou annulés) let _ = tokio::join!(http_handle, udp_handle); + Database::checkpoint_wal(&self.state.db).await?; + Database::close(&self.state.db).await?; + tracing::info!("Closed the runtime application."); Ok(()) diff --git a/src/database/database.rs b/src/database/database.rs index 8b8bdca..832060e 100644 --- a/src/database/database.rs +++ b/src/database/database.rs @@ -1,4 +1,6 @@ -use sea_orm::{ConnectOptions, Database as SeaDatabase, DatabaseConnection, DbErr}; +use sea_orm::{ + ConnectOptions, ConnectionTrait, Database as SeaDatabase, DatabaseConnection, DbBackend, DbErr, +}; use std::time::Duration; #[derive(Clone)] @@ -15,8 +17,9 @@ impl Database { println!("{}", final_dsn); let mut opt = ConnectOptions::new(final_dsn); - opt.max_connections(100) - .min_connections(5) + let is_sqlite = dsn.starts_with("sqlite:"); + opt.max_connections(20) + .min_connections(2) .connect_timeout(Duration::from_secs(8)) .acquire_timeout(Duration::from_secs(8)) .sqlx_logging(true) @@ -24,9 +27,34 @@ impl Database { let connection = SeaDatabase::connect(opt).await?; + if is_sqlite { + connection + .execute_unprepared("PRAGMA journal_mode = WAL") + .await?; + connection + .execute_unprepared("PRAGMA busy_timeout = 5000") + .await?; + } + Ok(Self { connection }) } + pub async fn checkpoint_wal(connection: &DatabaseConnection) -> Result<(), DbErr> { + if connection.get_database_backend() != DbBackend::Sqlite { + return Ok(()); + } + + connection + .execute_unprepared("PRAGMA wal_checkpoint;") + .await?; + + Ok(()) + } + + pub async fn close(connection: &DatabaseConnection) -> Result<(), DbErr> { + connection.clone().close().await + } + // Tu peux ajouter ici tes méthodes helpers si tu veux encapsuler SeaORM // ex: pub async fn find_user(...) pub fn get_connection(&self) -> &DatabaseConnection { diff --git a/src/services/message_reaction.rs b/src/services/message_reaction.rs index d1a3f23..2d64f43 100644 --- a/src/services/message_reaction.rs +++ b/src/services/message_reaction.rs @@ -7,6 +7,7 @@ use event_bus::Scope; use sea_orm::{ColumnTrait, EntityTrait, QueryFilter, Set}; use std::collections::HashMap; use std::sync::Arc; +use std::time::Instant; use uuid::Uuid; #[derive(Debug, Clone)] @@ -88,6 +89,7 @@ impl MessageReactionService { emoji_id: Uuid, skin_tone: Option, ) -> Result<(message_reaction::Model, bool), HTTPError> { + let started_at = Instant::now(); let (message, _, skin_tone, server_id) = self.validate(message_id, emoji_id, skin_tone).await?;