FIX: `PostgreSQLFallbackHandler` was bouncing in and out of readonly.

This commit is contained in:
Guo Xiang Tan 2016-03-08 09:37:40 +08:00
parent 74e4251aff
commit fcc86d3a9d
2 changed files with 20 additions and 6 deletions

View File

@ -39,7 +39,7 @@ class PostgreSQLFallbackHandler
end
Discourse.disable_readonly_mode
master = true
self.master = true
end
rescue => e
if e.message.include?("could not connect to server")
@ -77,6 +77,10 @@ class PostgreSQLFallbackHandler
end
end
def verify?
!master && !running && !recently_checked?
end
private
def config
@ -110,7 +114,7 @@ module ActiveRecord
fallback_handler = ::PostgreSQLFallbackHandler.instance
config = config.symbolize_keys
if !fallback_handler.master && !fallback_handler.running
if fallback_handler.verify?
connection = postgresql_connection(config.dup.merge({
host: config[:replica_host], port: config[:replica_port]
}))
@ -148,9 +152,7 @@ module ActiveRecord
end
def switch_back?
if !fallback_handler.master && !fallback_handler.running
fallback_handler.verify_master
end
fallback_handler.verify_master if fallback_handler.verify?
end
end
end

View File

@ -13,8 +13,10 @@ describe ActiveRecord::ConnectionHandling do
}).symbolize_keys!
end
let(:postgresql_fallback_handler) { PostgreSQLFallbackHandler.instance }
after do
::PostgreSQLFallbackHandler.instance.setup!
postgresql_fallback_handler.setup!
end
describe "#postgresql_fallback_connection" do
@ -58,18 +60,26 @@ describe ActiveRecord::ConnectionHandling do
})).returns(@replica_connection)
end
expect(postgresql_fallback_handler.master).to eq(true)
expect { ActiveRecord::Base.postgresql_fallback_connection(config) }
.to raise_error(PG::ConnectionBad)
expect{ ActiveRecord::Base.postgresql_fallback_connection(config) }
.to change{ Discourse.readonly_mode? }.from(false).to(true)
expect(postgresql_fallback_handler.master).to eq(false)
with_multisite_db(multisite_db) do
expect(postgresql_fallback_handler.master).to eq(true)
expect { ActiveRecord::Base.postgresql_fallback_connection(multisite_config) }
.to raise_error(PG::ConnectionBad)
expect{ ActiveRecord::Base.postgresql_fallback_connection(multisite_config) }
.to change{ Discourse.readonly_mode? }.from(false).to(true)
expect(postgresql_fallback_handler.master).to eq(false)
end
ActiveRecord::Base.unstub(:postgresql_connection)
@ -92,6 +102,8 @@ describe ActiveRecord::ConnectionHandling do
expect(Discourse.readonly_mode?).to eq(false)
expect(PostgreSQLFallbackHandler.instance.master).to eq(true)
expect(ActiveRecord::Base.connection_pool.connections.count).to eq(0)
expect(ActiveRecord::Base.connection)