Skip to content
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

Make production db pool size configurable #2019

Merged
merged 1 commit into from
Feb 26, 2018
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
8 changes: 8 additions & 0 deletions config/application.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ development:
database_host: ''
database_name: ''
database_password: ''
database_pool_idp: '5'
database_pool_worker: '5'
database_readonly_password: ''
database_readonly_username: ''
database_timeout: '5000'
database_username: ''
domain_name: 'localhost:3000'
Expand Down Expand Up @@ -256,6 +260,10 @@ test:
database_host: ''
database_name: ''
database_password: ''
database_pool_idp:
database_pool_worker:
database_readonly_password: ''
database_readonly_username: ''
database_timeout: '5000'
database_username: ''
dashboard_api_token: '123ABC'
Expand Down
4 changes: 3 additions & 1 deletion config/database.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<% require 'production_database_configuration' %>

postgresql: &postgresql
adapter: postgresql
encoding: utf8
Expand Down Expand Up @@ -33,6 +35,6 @@ production:
username: <%= Figaro.env.database_username! %>
host: <%= Figaro.env.database_host! %>
password: <%= Figaro.env.database_password! %>
pool: <%= (File.exist?('/etc/login.gov/info') && File.read('/etc/login.gov/info/role').chomp == 'worker') ? 26 : 5 %>
pool: <%= ProductionDatabaseConfiguration.pool %>
sslmode: 'verify-full'
sslrootcert: '/usr/local/share/aws/rds-combined-ca-bundle.pem'
14 changes: 14 additions & 0 deletions lib/production_database_configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class ProductionDatabaseConfiguration
def self.pool
env = Figaro.env
role = File.read('/etc/login.gov/info') if File.exist?('/etc/login.gov/info')
case role
when 'idp'
env.database_pool_idp.presence || 5
when 'worker'
env.database_pool_worker.presence || 26
else
5
end
end
end
69 changes: 69 additions & 0 deletions spec/lib/production_database_configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require 'rails_helper'

describe ProductionDatabaseConfiguration do
describe '.pool' do
context 'when the app is running on an idp host' do
before { stub_role_config('idp') }

it 'returns the idp pool size' do
allow(Figaro.env).to receive(:database_pool_idp).and_return(7)

expect(ProductionDatabaseConfiguration.pool).to eq(7)
end

it 'defaults to 5' do
allow(Figaro.env).to receive(:database_pool_idp).and_return(nil)

expect(ProductionDatabaseConfiguration.pool).to eq(5)

allow(Figaro.env).to receive(:database_pool_idp).and_return('')

expect(ProductionDatabaseConfiguration.pool).to eq(5)
end
end

context 'when the app is running on a worker host' do
before { stub_role_config('worker') }

it 'returns the worker pool size' do
allow(Figaro.env).to receive(:database_pool_worker).and_return(8)

expect(ProductionDatabaseConfiguration.pool).to eq(8)
end

it 'defaults to 26' do
allow(Figaro.env).to receive(:database_pool_worker).and_return(nil)

expect(ProductionDatabaseConfiguration.pool).to eq(26)

allow(Figaro.env).to receive(:database_pool_worker).and_return('')

expect(ProductionDatabaseConfiguration.pool).to eq(26)
end
end

context 'when the app is running on an host with an ambigous role' do
before { stub_role_config('fake') }

it 'returns a default of 5' do
expect(ProductionDatabaseConfiguration.pool).to eq(5)
end
end

context 'when the app is running on a host without a role config file' do
before do
allow(File).to receive(:exist?).with('/etc/login.gov/info').and_return(false)
end

it 'returns 5 and does not read the role config' do
expect(File).to_not receive(:read)
expect(ProductionDatabaseConfiguration.pool).to eq(5)
end
end
end

def stub_role_config(role)
allow(File).to receive(:exist?).with('/etc/login.gov/info').and_return(true)
allow(File).to receive(:read).with('/etc/login.gov/info').and_return(role)
end
end