Commit 04180eaf authored by Kamil Trzciński's avatar Kamil Trzciński

Duing application init connection can be assigned to `NullPool`

This appears to be a case for lazy init of performance bar
that in some circumstances results in a exception raised
as `db_config` is undefined
parent b4fbef35
...@@ -133,6 +133,11 @@ module Gitlab ...@@ -133,6 +133,11 @@ module Gitlab
# as this is dependent on a execution context # as this is dependent on a execution context
return ROLE_UNKNOWN if connection.is_a?(ConnectionProxy) return ROLE_UNKNOWN if connection.is_a?(ConnectionProxy)
# During application init we might receive `NullPool`
return ROLE_UNKNOWN unless connection.respond_to?(:pool) &&
connection.pool.respond_to?(:db_config) &&
connection.pool.db_config.respond_to?(:name)
if connection.pool.db_config.name.ends_with?(LoadBalancer::REPLICA_SUFFIX) if connection.pool.db_config.name.ends_with?(LoadBalancer::REPLICA_SUFFIX)
ROLE_REPLICA ROLE_REPLICA
else else
......
...@@ -304,6 +304,15 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -304,6 +304,15 @@ RSpec.describe Gitlab::Database::LoadBalancing do
end end
end end
context 'when the NullPool is used for connection' do
let(:pool) { ActiveRecord::ConnectionAdapters::NullPool.new }
let(:connection) { double(:connection, pool: pool) }
it 'returns unknown' do
expect(described_class.db_role_for_connection(connection)).to eq(:unknown)
end
end
context 'when the load balancing is configured' do context 'when the load balancing is configured' do
let(:db_host) { ActiveRecord::Base.connection_pool.db_config.host } let(:db_host) { ActiveRecord::Base.connection_pool.db_config.host }
let(:proxy) { described_class::ConnectionProxy.new([db_host]) } let(:proxy) { described_class::ConnectionProxy.new([db_host]) }
......
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