Skip to content

Commit 672dd2a

Browse files
committed
db: Remove obsolete DieselPool implementation
1 parent 95d8ed5 commit 672dd2a

File tree

2 files changed

+1
-122
lines changed

2 files changed

+1
-122
lines changed

src/db.rs

Lines changed: 1 addition & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,13 @@
11
use deadpool_diesel::postgres::{Hook, HookError};
22
use diesel::prelude::*;
3-
use diesel::r2d2::{self, ConnectionManager, CustomizeConnection, State};
4-
use prometheus::Histogram;
5-
use secrecy::{ExposeSecret, SecretString};
6-
use std::ops::Deref;
3+
use secrecy::ExposeSecret;
74
use std::time::Duration;
8-
use thiserror::Error;
95
use url::Url;
106

117
use crate::config;
128

139
pub mod sql_types;
1410

15-
pub type ConnectionPool = r2d2::Pool<ConnectionManager<PgConnection>>;
16-
17-
#[derive(Clone)]
18-
pub struct DieselPool {
19-
pool: ConnectionPool,
20-
time_to_obtain_connection_metric: Option<Histogram>,
21-
}
22-
23-
impl DieselPool {
24-
pub(crate) fn new(
25-
url: &SecretString,
26-
config: &config::DatabasePools,
27-
r2d2_config: r2d2::Builder<ConnectionManager<PgConnection>>,
28-
time_to_obtain_connection_metric: Histogram,
29-
) -> Result<DieselPool, PoolError> {
30-
let manager = ConnectionManager::new(connection_url(config, url.expose_secret()));
31-
32-
// For crates.io we want the behavior of creating a database pool to be slightly different
33-
// than the defaults of R2D2: the library's build() method assumes its consumers always
34-
// need a database connection to operate, so it blocks creating a pool until a minimum
35-
// number of connections is available.
36-
//
37-
// crates.io can actually operate in a limited capacity without a database connections,
38-
// especially by serving download requests to our users. Because of that we don't want to
39-
// block indefinitely waiting for a connection: we instead need to wait for a bit (to avoid
40-
// serving errors for the first connections until the pool is initialized) and if we can't
41-
// establish any connection continue booting up the application. The database pool will
42-
// automatically be marked as unhealthy and the rest of the application will adapt.
43-
let pool = DieselPool {
44-
pool: r2d2_config.build_unchecked(manager),
45-
time_to_obtain_connection_metric: Some(time_to_obtain_connection_metric),
46-
};
47-
match pool.wait_until_healthy(Duration::from_secs(5)) {
48-
Ok(()) => {}
49-
Err(PoolError::UnhealthyPool) => {}
50-
Err(err) => return Err(err),
51-
}
52-
53-
Ok(pool)
54-
}
55-
56-
pub fn new_background_worker(pool: r2d2::Pool<ConnectionManager<PgConnection>>) -> Self {
57-
Self {
58-
pool,
59-
time_to_obtain_connection_metric: None,
60-
}
61-
}
62-
63-
#[instrument(name = "db.connect", skip_all)]
64-
pub fn get(&self) -> Result<DieselPooledConn, PoolError> {
65-
match self.time_to_obtain_connection_metric.as_ref() {
66-
Some(time_to_obtain_connection_metric) => time_to_obtain_connection_metric
67-
.observe_closure_duration(|| {
68-
if let Some(conn) = self.pool.try_get() {
69-
Ok(conn)
70-
} else if !self.is_healthy() {
71-
Err(PoolError::UnhealthyPool)
72-
} else {
73-
Ok(self.pool.get()?)
74-
}
75-
}),
76-
None => Ok(self.pool.get()?),
77-
}
78-
}
79-
80-
pub fn state(&self) -> State {
81-
self.pool.state()
82-
}
83-
84-
#[instrument(skip_all)]
85-
pub fn wait_until_healthy(&self, timeout: Duration) -> Result<(), PoolError> {
86-
match self.pool.get_timeout(timeout) {
87-
Ok(_) => Ok(()),
88-
Err(_) if !self.is_healthy() => Err(PoolError::UnhealthyPool),
89-
Err(err) => Err(PoolError::R2D2(err)),
90-
}
91-
}
92-
93-
fn is_healthy(&self) -> bool {
94-
self.state().connections > 0
95-
}
96-
}
97-
98-
impl Deref for DieselPool {
99-
type Target = ConnectionPool;
100-
101-
fn deref(&self) -> &Self::Target {
102-
&self.pool
103-
}
104-
}
105-
106-
pub type DieselPooledConn = r2d2::PooledConnection<ConnectionManager<PgConnection>>;
107-
10811
pub fn oneoff_connection_with_config(
10912
config: &config::DatabasePools,
11013
) -> ConnectionResult<PgConnection> {
@@ -160,12 +63,6 @@ impl ConnectionConfig {
16063
}
16164
}
16265

163-
impl CustomizeConnection<PgConnection, r2d2::Error> for ConnectionConfig {
164-
fn on_acquire(&self, conn: &mut PgConnection) -> Result<(), r2d2::Error> {
165-
self.apply(conn).map_err(r2d2::Error::QueryError)
166-
}
167-
}
168-
16966
impl From<ConnectionConfig> for Hook {
17067
fn from(config: ConnectionConfig) -> Self {
17168
Hook::async_fn(move |conn, _| {
@@ -178,11 +75,3 @@ impl From<ConnectionConfig> for Hook {
17875
})
17976
}
18077
}
181-
182-
#[derive(Debug, Error)]
183-
pub enum PoolError {
184-
#[error(transparent)]
185-
R2D2(#[from] r2d2::PoolError),
186-
#[error("unhealthy database pool")]
187-
UnhealthyPool,
188-
}

src/util/errors.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use diesel::result::{DatabaseErrorKind, Error as DieselError};
2424
use http::StatusCode;
2525
use tokio::task::JoinError;
2626

27-
use crate::db::PoolError;
2827
use crate::middleware::log_request::ErrorField;
2928

3029
mod json;
@@ -172,15 +171,6 @@ impl From<EmailError> for BoxedAppError {
172171
}
173172
}
174173

175-
impl From<PoolError> for BoxedAppError {
176-
fn from(err: PoolError) -> BoxedAppError {
177-
match err {
178-
PoolError::UnhealthyPool => service_unavailable(),
179-
_ => Box::new(err),
180-
}
181-
}
182-
}
183-
184174
impl From<deadpool_diesel::PoolError> for BoxedAppError {
185175
fn from(_err: deadpool_diesel::PoolError) -> BoxedAppError {
186176
service_unavailable()

0 commit comments

Comments
 (0)