Skip to content

Commit 168a37f

Browse files
committed
migrate web::releases to sqlx
1 parent 709ac52 commit 168a37f

8 files changed

+313
-165
lines changed

.sqlx/query-340670c2844a0e5df3b5384a8c3a92c3ad35bd1484a6ef9fa02d508f65c7c3ec.json

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

.sqlx/query-545ccf8362ae92d921df5da730c10e24ddda6f411a0f5c9461f8ee5f6231c5b4.json

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

.sqlx/query-bdddad099e891bb45ba3703d7144160056d6cb620c55be459ead0f95c3523035.json

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

src/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct Config {
1313

1414
// Database connection params
1515
pub(crate) database_url: String,
16+
pub(crate) max_legacy_pool_size: u32,
1617
pub(crate) max_pool_size: u32,
1718
pub(crate) min_pool_idle: u32,
1819

@@ -148,7 +149,8 @@ impl Config {
148149
prefix: prefix.clone(),
149150

150151
database_url: require_env("DOCSRS_DATABASE_URL")?,
151-
max_pool_size: env("DOCSRS_MAX_POOL_SIZE", 90)?,
152+
max_legacy_pool_size: env("DOCSRS_MAX_LEGACY_POOL_SIZE", 45)?,
153+
max_pool_size: env("DOCSRS_MAX_POOL_SIZE", 45)?,
152154
min_pool_idle: env("DOCSRS_MIN_POOL_IDLE", 10)?,
153155

154156
storage_backend: env("DOCSRS_STORAGE_BACKEND", StorageKind::Database)?,

src/db/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub use self::{
99
file::{add_path_into_database, add_path_into_remote_archive},
1010
migrate::migrate,
1111
overrides::Overrides,
12-
pool::{Pool, PoolClient, PoolError},
12+
pool::{AsyncPoolClient, Pool, PoolClient, PoolError},
1313
};
1414

1515
mod add_package;

src/db/pool.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use futures_util::{future::BoxFuture, stream::BoxStream};
44
use postgres::{Client, NoTls};
55
use r2d2_postgres::PostgresConnectionManager;
66
use sqlx::{postgres::PgPoolOptions, Executor};
7-
use std::sync::Arc;
7+
use std::{sync::Arc, time::Duration};
88
use tokio::runtime::Runtime;
99
use tracing::debug;
1010

@@ -56,22 +56,29 @@ impl Pool {
5656
.database_url
5757
.parse()
5858
.map_err(PoolError::InvalidDatabaseUrl)?;
59+
60+
let acquire_timeout = Duration::from_secs(30);
61+
let max_lifetime = Duration::from_secs(30 * 60);
62+
let idle_timeout = Duration::from_secs(10 * 60);
63+
5964
let manager = PostgresConnectionManager::new(url, NoTls);
6065
let pool = r2d2::Pool::builder()
61-
.max_size(config.max_pool_size)
66+
.max_size(config.max_legacy_pool_size)
6267
.min_idle(Some(config.min_pool_idle))
68+
.max_lifetime(Some(max_lifetime))
69+
.connection_timeout(acquire_timeout)
70+
.idle_timeout(Some(idle_timeout))
6371
.connection_customizer(Box::new(SetSchema::new(schema)))
6472
.build(manager)
6573
.map_err(PoolError::PoolCreationFailed)?;
6674

6775
let _guard = runtime.enter();
6876
let async_pool = PgPoolOptions::new()
69-
// FIXME: these pool sizes would have to be validated before the pool is used in
70-
// a production setting.
71-
// Currently we only use it for the storage DB backend, which is only used for local
72-
// & unit-testing.
7377
.max_connections(config.max_pool_size)
74-
// .min_connections(config.min_pool_idle)
78+
.min_connections(config.min_pool_idle)
79+
.max_lifetime(max_lifetime)
80+
.acquire_timeout(acquire_timeout)
81+
.idle_timeout(idle_timeout)
7582
.after_connect({
7683
let schema = schema.to_owned();
7784
move |conn, _meta| {
@@ -102,7 +109,7 @@ impl Pool {
102109
pool,
103110
async_pool,
104111
metrics,
105-
max_size: config.max_pool_size,
112+
max_size: config.max_legacy_pool_size + config.max_pool_size,
106113
})
107114
}
108115

src/test/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod fakes;
22

33
pub(crate) use self::fakes::FakeBuild;
44
use crate::cdn::CdnBackend;
5-
use crate::db::{Pool, PoolClient};
5+
use crate::db::{AsyncPoolClient, Pool, PoolClient};
66
use crate::error::Result;
77
use crate::repositories::RepositoryStatsUpdater;
88
use crate::storage::{AsyncStorage, Storage, StorageKind};
@@ -298,6 +298,7 @@ impl TestEnvironment {
298298

299299
// Use less connections for each test compared to production.
300300
config.max_pool_size = 4;
301+
config.max_legacy_pool_size = 4;
301302
config.min_pool_idle = 0;
302303

303304
// Use the database for storage, as it's faster than S3.
@@ -571,6 +572,13 @@ impl TestDatabase {
571572
self.pool.clone()
572573
}
573574

575+
pub(crate) async fn async_conn(&self) -> AsyncPoolClient {
576+
self.pool
577+
.get_async()
578+
.await
579+
.expect("failed to get a connection out of the pool")
580+
}
581+
574582
pub(crate) fn conn(&self) -> PoolClient {
575583
self.pool
576584
.get()

0 commit comments

Comments
 (0)