19 lines
718 B
Rust
19 lines
718 B
Rust
use sqlx::postgres::{PgPool, PgPoolOptions};
|
||
use std::time::Duration;
|
||
|
||
/// 初始化数据库连接池
|
||
pub async fn init_pool(database_url: &str) -> Result<PgPool, sqlx::Error> {
|
||
PgPoolOptions::new()
|
||
.max_connections(20) // 根据服务器规格调整,IAM服务通常并发高
|
||
.min_connections(5)
|
||
.acquire_timeout(Duration::from_secs(3)) // 获取连接超时时间
|
||
.connect(database_url)
|
||
.await
|
||
}
|
||
|
||
// (可选) 可以在应用启动时自动运行迁移
|
||
// pub async fn run_migrations(pool: &PgPool) -> Result<(), sqlx::migrate::MigrateError> {
|
||
// // 这要求你在项目根目录有 `migrations/` 文件夹
|
||
// sqlx::migrate!("./migrations").run(pool).await
|
||
// }
|