Skip to content

Enable setting database pool limits from environment variables #732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@
"value": "",
"required": false
},
"DB_POOL_SIZE": {
"value": "10",
"required": false,
"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."
},
"DB_MIN_IDLE": {
"value": "5",
"required": false,
"description": "The pool will try to maintain at least this many idle connections at all times, while respecting the maximum size of the pool."
},
"DB_HELPER_THREADS": {
"value": "3",
"required": false,
"description": "The number of threads that the pool will use for asynchronous operations such as connection creation and health checks."
},
"SESSION_KEY": {
"generator": "secret"
},
Expand Down
31 changes: 25 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::env;
use std::error::Error;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -47,15 +48,33 @@ impl App {

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

let db_pool_size = match (env::var("DB_POOL_SIZE"), config.env) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it's worth just removing the env defaults and assume that if the var is unset it's in development mode?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like reasonable production defaults as well.

(Ok(num), _) => num.parse().expect("couldn't parse DB_POOL_SIZE"),
(_, ::Env::Production) => 10,
_ => 1,
};

let db_min_idle = match (env::var("DB_MIN_IDLE"), config.env) {
(Ok(num), _) => Some(num.parse().expect("couldn't parse DB_MIN_IDLE")),
(_, ::Env::Production) => Some(5),
_ => None,
};

let db_helper_threads = match (env::var("DB_HELPER_THREADS"), config.env) {
(Ok(num), _) => num.parse().expect("couldn't parse DB_HELPER_THREADS"),
(_, ::Env::Production) => 3,
_ => 1,
};

let db_config = r2d2::Config::builder()
.pool_size(if config.env == ::Env::Production {10} else {1})
.min_idle(if config.env == ::Env::Production {Some(5)} else {None})
.helper_threads(if config.env == ::Env::Production {3} else {1})
.pool_size(db_pool_size)
.min_idle(db_min_idle)
.helper_threads(db_helper_threads)
.build();
let diesel_db_config = r2d2::Config::builder()
.pool_size(if config.env == ::Env::Production {30} else {1})
.min_idle(if config.env == ::Env::Production {Some(5)} else {None})
.helper_threads(if config.env == ::Env::Production {3} else {1})
.pool_size(db_pool_size)
.min_idle(db_min_idle)
.helper_threads(db_helper_threads)
.build();

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