From 34e4c8f5738a5614339caef09a60c5d81e80d8e4 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 19 Jun 2018 10:15:29 +0800 Subject: [PATCH] FIX: `Discourse.keep_readonly_mode` spins a new thread each time it is called. --- lib/discourse.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/discourse.rb b/lib/discourse.rb index 0822630eead..f42caae4f49 100644 --- a/lib/discourse.rb +++ b/lib/discourse.rb @@ -294,10 +294,16 @@ module Discourse def self.keep_readonly_mode(key) # extend the expiry by 1 minute every 30 seconds unless Rails.env.test? - Thread.new do - while readonly_mode?(key) - $redis.expire(key, READONLY_MODE_KEY_TTL) - sleep 30.seconds + @threads ||= {} + + active_thread = @threads[key] + + unless active_thread&.alive? + @threads[key] = Thread.new do + while readonly_mode?(key) + $redis.expire(key, READONLY_MODE_KEY_TTL) + sleep 30.seconds + end end end end