Skip to content

Commit a24f23e

Browse files
committed
move diesel_pool() to DieselPool::new()
1 parent 7c548f4 commit a24f23e

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

src/app.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Application-wide components in a struct accessible from each request
22
3-
use crate::{db, Config, Env};
3+
use crate::db::{ConnectionConfig, DieselPool};
4+
use crate::{Config, Env};
45
use std::{sync::Arc, time::Duration};
56

67
use crate::downloads_counter::DownloadsCounter;
@@ -18,10 +19,10 @@ use scheduled_thread_pool::ScheduledThreadPool;
1819
#[allow(missing_debug_implementations)]
1920
pub struct App {
2021
/// The primary database connection pool
21-
pub primary_database: db::DieselPool,
22+
pub primary_database: DieselPool,
2223

2324
/// The read-only replica database connection pool
24-
pub read_only_replica_database: Option<db::DieselPool>,
25+
pub read_only_replica_database: Option<DieselPool>,
2526

2627
/// GitHub API client
2728
pub github: GitHubClient,
@@ -103,7 +104,7 @@ impl App {
103104
_ => 30,
104105
};
105106

106-
let primary_db_connection_config = db::ConnectionConfig {
107+
let primary_db_connection_config = ConnectionConfig {
107108
statement_timeout: db_connection_timeout,
108109
read_only: config.db_primary_config.read_only_mode,
109110
};
@@ -118,11 +119,11 @@ impl App {
118119
.thread_pool(thread_pool.clone());
119120

120121
let primary_database =
121-
db::diesel_pool(&config.db_primary_config.url, config.env, primary_db_config);
122+
DieselPool::new(&config.db_primary_config.url, config.env, primary_db_config);
122123

123124
let replica_database = if let Some(url) = config.db_replica_config.as_ref().map(|c| &c.url)
124125
{
125-
let replica_db_connection_config = db::ConnectionConfig {
126+
let replica_db_connection_config = ConnectionConfig {
126127
statement_timeout: db_connection_timeout,
127128
read_only: true,
128129
};
@@ -134,7 +135,7 @@ impl App {
134135
.connection_customizer(Box::new(replica_db_connection_config))
135136
.thread_pool(thread_pool);
136137

137-
Some(db::diesel_pool(&url, config.env, replica_db_config))
138+
Some(DieselPool::new(&url, config.env, replica_db_config))
138139
} else {
139140
None
140141
};

src/db.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ pub enum DieselPool {
1717
}
1818

1919
impl DieselPool {
20+
pub(crate) fn new(
21+
url: &str,
22+
env: Env,
23+
config: r2d2::Builder<ConnectionManager<PgConnection>>,
24+
) -> DieselPool {
25+
let url = connection_url(url);
26+
27+
if env == Env::Test {
28+
let conn = PgConnection::establish(&url).expect("failed to establish connection");
29+
return DieselPool::Test(Arc::new(ReentrantMutex::new(conn)));
30+
}
31+
32+
let manager = ConnectionManager::new(url);
33+
DieselPool::Pool(config.build(manager).unwrap())
34+
}
35+
2036
pub fn get(&self) -> Result<DieselPooledConn<'_>, r2d2::PoolError> {
2137
match self {
2238
DieselPool::Pool(pool) => Ok(DieselPooledConn::Pool(pool.get()?)),
@@ -39,10 +55,6 @@ impl DieselPool {
3955
},
4056
}
4157
}
42-
43-
fn test_conn(conn: PgConnection) -> Self {
44-
DieselPool::Test(Arc::new(ReentrantMutex::new(conn)))
45-
}
4658
}
4759

4860
#[derive(Debug, Copy, Clone)]
@@ -83,21 +95,6 @@ pub fn connection_url(url: &str) -> String {
8395
url.into_string()
8496
}
8597

86-
pub fn diesel_pool(
87-
url: &str,
88-
env: Env,
89-
config: r2d2::Builder<ConnectionManager<PgConnection>>,
90-
) -> DieselPool {
91-
let url = connection_url(url);
92-
if env == Env::Test {
93-
let conn = PgConnection::establish(&url).expect("failed to establish connection");
94-
DieselPool::test_conn(conn)
95-
} else {
96-
let manager = ConnectionManager::new(url);
97-
DieselPool::Pool(config.build(manager).unwrap())
98-
}
99-
}
100-
10198
pub trait RequestTransaction {
10299
/// Obtain a read/write database connection from the primary pool
103100
fn db_conn(&self) -> Result<DieselPooledConn<'_>, r2d2::PoolError>;

0 commit comments

Comments
 (0)