2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-04 08:47:37 +08:00

FIX: Add a lock to ensure only a single thread is running each time.

This commit is contained in:
Guo Xiang Tan 2016-02-05 10:33:35 +08:00
parent c532d7d1ae
commit 74dc838f5f
2 changed files with 22 additions and 7 deletions

View file

@ -11,11 +11,14 @@ class PostgreSQLFallbackHandler
def initialize
@master = true
@running = false
@mutex = Mutex.new
end
def verify_master
return if @running && recently_checked?
@running = true
@mutex.synchronize do
return if @running || recently_checked?
@running = true
end
Thread.new do
begin
@ -23,6 +26,7 @@ class PostgreSQLFallbackHandler
connection = ActiveRecord::Base.postgresql_connection(config)
if connection.active?
connection.disconnect!
logger.info "#{self.class}: Master server is active. Reconnecting..."
ActiveRecord::Base.establish_connection(config)
Discourse.disable_readonly_mode
@ -53,7 +57,7 @@ class PostgreSQLFallbackHandler
def recently_checked?
if @last_check
Time.zone.now <= @last_check + 5.seconds
Time.zone.now <= (@last_check + 5.seconds)
else
false
end
@ -66,7 +70,7 @@ module ActiveRecord
fallback_handler = ::PostgreSQLFallbackHandler.instance
config = config.symbolize_keys
if !fallback_handler.master
if !fallback_handler.master && !fallback_handler.running
connection = postgresql_connection(config.dup.merge({
host: config[:replica_host], port: config[:replica_port]
}))