Skip to content

Commit 8f9dccb

Browse files
committed
Fix pool size, don't crash if we can't get a connection
When testing in staging, the runner started crashing on boot. This was because we only set the connection pool size to 1 when we meant to set it to 2 (one for the runner, one for the worker thread). So in each loop, if all jobs took greater than 1 second to run, the runner would crash. This fixes the pool size, and also does not return an error if no database connection could be retrieved from the pool.
1 parent c4fb027 commit 8f9dccb

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/background/runner.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ impl<Env: RefUnwindSafe + Send + Sync + 'static> Runner<Env> {
5757
}
5858

5959
pub fn run_all_pending_jobs(&self) -> CargoResult<()> {
60-
let available_job_count = storage::available_job_count(&*self.connection()?)?;
61-
for _ in 0..available_job_count {
62-
self.run_single_job()
60+
if let Some(conn) = self.try_connection() {
61+
let available_job_count = storage::available_job_count(&conn)?;
62+
for _ in 0..available_job_count {
63+
self.run_single_job()
64+
}
6365
}
6466
Ok(())
6567
}
@@ -112,6 +114,10 @@ impl<Env: RefUnwindSafe + Send + Sync + 'static> Runner<Env> {
112114
self.connection_pool.get().map_err(Into::into)
113115
}
114116

117+
fn try_connection(&self) -> Option<DieselPooledConn> {
118+
self.connection_pool.try_get()
119+
}
120+
115121
pub fn assert_no_failed_jobs(&self) -> CargoResult<()> {
116122
self.wait_for_jobs();
117123
let failed_jobs = storage::failed_job_count(&*self.connection()?)?;

src/bin/background-worker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn main() {
1919
let config = cargo_registry::Config::default();
2020

2121
// We're only using 1 thread, so we only need 2 connections
22-
let db_config = r2d2::Pool::builder().max_size(1);
22+
let db_config = r2d2::Pool::builder().max_size(2);
2323
let db_pool = db::diesel_pool(&config.db_url, config.env, db_config);
2424

2525
let username = env::var("GIT_HTTP_USER");

src/db.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ impl DieselPool {
2727
}
2828
}
2929

30+
pub fn try_get(&self) -> Option<DieselPooledConn> {
31+
match self {
32+
DieselPool::Pool(pool) => pool.try_get().map(DieselPooledConn::Pool),
33+
DieselPool::Test(conn) => conn.try_lock().map(DieselPooledConn::Test),
34+
}
35+
}
36+
3037
pub fn state(&self) -> r2d2::State {
3138
match self {
3239
DieselPool::Pool(pool) => pool.state(),

0 commit comments

Comments
 (0)