Skip to content

Commit cd89ab2

Browse files
committed
db: Convert DieselPool enum to simpler struct
1 parent 9da0bac commit cd89ab2

File tree

1 file changed

+29
-41
lines changed

1 file changed

+29
-41
lines changed

src/db.rs

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,9 @@ pub mod sql_types;
1313
pub type ConnectionPool = r2d2::Pool<ConnectionManager<PgConnection>>;
1414

1515
#[derive(Clone)]
16-
pub enum DieselPool {
17-
Pool {
18-
pool: ConnectionPool,
19-
time_to_obtain_connection_metric: Histogram,
20-
},
21-
BackgroundJobPool {
22-
pool: ConnectionPool,
23-
},
16+
pub struct DieselPool {
17+
pool: ConnectionPool,
18+
time_to_obtain_connection_metric: Option<Histogram>,
2419
}
2520

2621
impl DieselPool {
@@ -43,9 +38,9 @@ impl DieselPool {
4338
// serving errors for the first connections until the pool is initialized) and if we can't
4439
// establish any connection continue booting up the application. The database pool will
4540
// automatically be marked as unhealthy and the rest of the application will adapt.
46-
let pool = DieselPool::Pool {
41+
let pool = DieselPool {
4742
pool: r2d2_config.build_unchecked(manager),
48-
time_to_obtain_connection_metric,
43+
time_to_obtain_connection_metric: Some(time_to_obtain_connection_metric),
4944
};
5045
match pool.wait_until_healthy(Duration::from_secs(5)) {
5146
Ok(()) => {}
@@ -57,50 +52,43 @@ impl DieselPool {
5752
}
5853

5954
pub fn new_background_worker(pool: r2d2::Pool<ConnectionManager<PgConnection>>) -> Self {
60-
Self::BackgroundJobPool { pool }
55+
Self {
56+
pool,
57+
time_to_obtain_connection_metric: None,
58+
}
6159
}
6260

6361
#[instrument(name = "db.connect", skip_all)]
6462
pub fn get(&self) -> Result<DieselPooledConn, PoolError> {
65-
match self {
66-
DieselPool::Pool {
67-
pool,
68-
time_to_obtain_connection_metric,
69-
} => time_to_obtain_connection_metric.observe_closure_duration(|| {
70-
if let Some(conn) = pool.try_get() {
71-
Ok(conn)
72-
} else if !self.is_healthy() {
73-
Err(PoolError::UnhealthyPool)
74-
} else {
75-
Ok(pool.get()?)
76-
}
77-
}),
78-
DieselPool::BackgroundJobPool { pool } => Ok(pool.get()?),
63+
match self.time_to_obtain_connection_metric.as_ref() {
64+
Some(time_to_obtain_connection_metric) => time_to_obtain_connection_metric
65+
.observe_closure_duration(|| {
66+
if let Some(conn) = self.pool.try_get() {
67+
Ok(conn)
68+
} else if !self.is_healthy() {
69+
Err(PoolError::UnhealthyPool)
70+
} else {
71+
Ok(self.pool.get()?)
72+
}
73+
}),
74+
None => Ok(self.pool.get()?),
7975
}
8076
}
8177

8278
pub fn state(&self) -> PoolState {
83-
match self {
84-
DieselPool::Pool { pool, .. } | DieselPool::BackgroundJobPool { pool } => {
85-
let state = pool.state();
86-
PoolState {
87-
connections: state.connections,
88-
idle_connections: state.idle_connections,
89-
}
90-
}
79+
let state = self.pool.state();
80+
PoolState {
81+
connections: state.connections,
82+
idle_connections: state.idle_connections,
9183
}
9284
}
9385

9486
#[instrument(skip_all)]
9587
pub fn wait_until_healthy(&self, timeout: Duration) -> Result<(), PoolError> {
96-
match self {
97-
DieselPool::Pool { pool, .. } | DieselPool::BackgroundJobPool { pool } => {
98-
match pool.get_timeout(timeout) {
99-
Ok(_) => Ok(()),
100-
Err(_) if !self.is_healthy() => Err(PoolError::UnhealthyPool),
101-
Err(err) => Err(PoolError::R2D2(err)),
102-
}
103-
}
88+
match self.pool.get_timeout(timeout) {
89+
Ok(_) => Ok(()),
90+
Err(_) if !self.is_healthy() => Err(PoolError::UnhealthyPool),
91+
Err(err) => Err(PoolError::R2D2(err)),
10492
}
10593
}
10694

0 commit comments

Comments
 (0)