Commit 1a53abd4 authored by Matthias Käppler's avatar Matthias Käppler Committed by Andreas Brandl

Dynamically set db pool size in Puma

Before Puma, we had 1 rails process talk to the database
at any given time, which meant a connection pool size of 1
was sufficient to support that model. With Puma and its worker
thread pool, there can now be many threads simultaneously
trying to talk to the database. This change makes sure that
we allow the db pool size to scale along with the maximum
number of worker threads configured.
parent b3361c30
---
title: 'Puma only: database connection pool now always >= number of worker threads'
merge_request: 19286
author:
type: performance
# frozen_string_literal: true
# when running on puma, scale connection pool size with the number
# of threads per worker process
if defined?(::Puma)
db_config = Gitlab::Database.config ||
Rails.application.config.database_configuration[Rails.env]
puma_options = Puma.cli_config.options
# We use either the maximum number of threads per worker process, or
# the user specified value, whichever is larger.
desired_pool_size = [db_config['pool'].to_i, puma_options[:max_threads]].max
db_config['pool'] = desired_pool_size
# recreate the connection pool from the new config
ActiveRecord::Base.establish_connection(db_config)
end
# frozen_string_literal: true
require 'spec_helper'
describe 'Database config initializer' do
subject do
load Rails.root.join('config/initializers/database_config.rb')
end
before do
allow(ActiveRecord::Base).to receive(:establish_connection)
end
context "when using Puma" do
let(:puma) { double('puma') }
let(:puma_options) { { max_threads: 8 } }
before do
stub_const("Puma", puma)
allow(puma).to receive_message_chain(:cli_config, :options).and_return(puma_options)
end
context "and no existing pool size is set" do
before do
stub_database_config(pool_size: nil)
end
it "sets it to the max number of worker threads" do
expect { subject }.to change { Gitlab::Database.config['pool'] }.from(nil).to(8)
end
end
context "and the existing pool size is smaller than the max number of worker threads" do
before do
stub_database_config(pool_size: 7)
end
it "sets it to the max number of worker threads" do
expect { subject }.to change { Gitlab::Database.config['pool'] }.from(7).to(8)
end
end
context "and the existing pool size is larger than the max number of worker threads" do
before do
stub_database_config(pool_size: 9)
end
it "keeps the configured pool size" do
expect { subject }.not_to change { Gitlab::Database.config['pool'] }
end
end
end
context "when not using Puma" do
before do
stub_database_config(pool_size: 7)
end
it "does nothing" do
expect { subject }.not_to change { Gitlab::Database.config['pool'] }
end
end
def stub_database_config(pool_size:)
config = {
'adapter' => 'postgresql',
'host' => 'db.host.com',
'pool' => pool_size
}.compact
allow(Gitlab::Database).to receive(:config).and_return(config)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment