Skip to content

Commit 1169382

Browse files
committed
Enable setting database pool limits from environment variables
1 parent 63d3b30 commit 1169382

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

app.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@
2323
"value": "",
2424
"required": false
2525
},
26+
"DB_POOL_SIZE": {
27+
"value": "10",
28+
"required": false,
29+
"description": "The maximum number of database connections managed by the pool. Set so that this value times the number of dynos is less than your connection limit."
30+
},
31+
"DB_MIN_IDLE": {
32+
"value": "5",
33+
"required": false,
34+
"description": "The pool will try to maintain at least this many idle connections at all times, while respecting the maximum size of the pool."
35+
},
36+
"DB_HELPER_THREADS": {
37+
"value": "3",
38+
"required": false,
39+
"description": "The number of threads that the pool will use for asynchronous operations such as connection creation and health checks."
40+
},
2641
"SESSION_KEY": {
2742
"generator": "secret"
2843
},

src/app.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::env;
12
use std::error::Error;
23
use std::path::PathBuf;
34
use std::sync::{Arc, Mutex};
@@ -47,15 +48,33 @@ impl App {
4748

4849
github.scopes.push(String::from("read:org"));
4950

51+
let db_pool_size = match (env::var("DB_POOL_SIZE"), config.env) {
52+
(Ok(num), _) => num.parse().expect("couldn't parse DB_POOL_SIZE"),
53+
(_, ::Env::Production) => 10,
54+
_ => 1,
55+
};
56+
57+
let db_min_idle = match (env::var("DB_MIN_IDLE"), config.env) {
58+
(Ok(num), _) => Some(num.parse().expect("couldn't parse DB_MIN_IDLE")),
59+
(_, ::Env::Production) => Some(5),
60+
_ => None,
61+
};
62+
63+
let db_helper_threads = match (env::var("DB_HELPER_THREADS"), config.env) {
64+
(Ok(num), _) => num.parse().expect("couldn't parse DB_HELPER_THREADS"),
65+
(_, ::Env::Production) => 3,
66+
_ => 1,
67+
};
68+
5069
let db_config = r2d2::Config::builder()
51-
.pool_size(if config.env == ::Env::Production {10} else {1})
52-
.min_idle(if config.env == ::Env::Production {Some(5)} else {None})
53-
.helper_threads(if config.env == ::Env::Production {3} else {1})
70+
.pool_size(db_pool_size)
71+
.min_idle(db_min_idle)
72+
.helper_threads(db_helper_threads)
5473
.build();
5574
let diesel_db_config = r2d2::Config::builder()
56-
.pool_size(if config.env == ::Env::Production {30} else {1})
57-
.min_idle(if config.env == ::Env::Production {Some(5)} else {None})
58-
.helper_threads(if config.env == ::Env::Production {3} else {1})
75+
.pool_size(db_pool_size)
76+
.min_idle(db_min_idle)
77+
.helper_threads(db_helper_threads)
5978
.build();
6079

6180
let repo = git2::Repository::open(&config.git_repo_checkout).unwrap();

0 commit comments

Comments
 (0)