DEV: Use a longer TTL for pg readonly mode.

This commit is contained in:
Guo Xiang Tan 2020-07-14 16:15:58 +08:00
parent 2196d0b9ae
commit 94a2a70462
No known key found for this signature in database
GPG Key ID: FBD110179AAC1F20
2 changed files with 17 additions and 7 deletions

View File

@ -431,6 +431,7 @@ module Discourse
READONLY_MODE_KEY_TTL ||= 60 READONLY_MODE_KEY_TTL ||= 60
READONLY_MODE_KEY ||= 'readonly_mode' READONLY_MODE_KEY ||= 'readonly_mode'
PG_READONLY_MODE_KEY ||= 'readonly_mode:postgres' PG_READONLY_MODE_KEY ||= 'readonly_mode:postgres'
PG_READONLY_MODE_KEY_TTL ||= 300
USER_READONLY_MODE_KEY ||= 'readonly_mode:user' USER_READONLY_MODE_KEY ||= 'readonly_mode:user'
PG_FORCE_READONLY_MODE_KEY ||= 'readonly_mode:postgres_force' PG_FORCE_READONLY_MODE_KEY ||= 'readonly_mode:postgres_force'
@ -445,16 +446,24 @@ module Discourse
if key == USER_READONLY_MODE_KEY || key == PG_FORCE_READONLY_MODE_KEY if key == USER_READONLY_MODE_KEY || key == PG_FORCE_READONLY_MODE_KEY
Discourse.redis.set(key, 1) Discourse.redis.set(key, 1)
else else
Discourse.redis.setex(key, READONLY_MODE_KEY_TTL, 1) ttl =
keep_readonly_mode(key) if !Rails.env.test? case key
when PG_READONLY_MODE_KEY
PG_READONLY_MODE_KEY_TTL
else
READONLY_MODE_KEY_TTL
end
Discourse.redis.setex(key, ttl, 1)
keep_readonly_mode(key, ttl: ttl) if !Rails.env.test?
end end
MessageBus.publish(readonly_channel, true) MessageBus.publish(readonly_channel, true)
true true
end end
def self.keep_readonly_mode(key) def self.keep_readonly_mode(key, ttl:)
# extend the expiry by 1 minute every 30 seconds # extend the expiry by ttl minute every ttl/2 seconds
@mutex ||= Mutex.new @mutex ||= Mutex.new
@mutex.synchronize do @mutex.synchronize do
@ -465,12 +474,12 @@ module Discourse
unless @threads[key]&.alive? unless @threads[key]&.alive?
@threads[key] = Thread.new do @threads[key] = Thread.new do
while @dbs.size > 0 do while @dbs.size > 0 do
sleep 30 sleep ttl / 2
@mutex.synchronize do @mutex.synchronize do
@dbs.each do |db| @dbs.each do |db|
RailsMultisite::ConnectionManagement.with_connection(db) do RailsMultisite::ConnectionManagement.with_connection(db) do
if !Discourse.redis.expire(key, READONLY_MODE_KEY_TTL) if !Discourse.redis.expire(key, ttl)
@dbs.delete(db) @dbs.delete(db)
end end
end end

View File

@ -235,7 +235,8 @@ describe Discourse do
describe ".disable_readonly_mode" do describe ".disable_readonly_mode" do
context 'user disabled readonly mode' do context 'user disabled readonly mode' do
it "removes readonly key in redis and publish a message through the message bus" do it "removes readonly key in redis and publish a message through the message bus" do
Discourse.enable_readonly_mode(user_enabled: true) message = MessageBus.track_publish { Discourse.disable_readonly_mode(user_readonly_mode_key) }.first
assert_readonly_mode_disabled(message, user_readonly_mode_key)
end end
end end
end end