diff --git a/lib/discourse_redis.rb b/lib/discourse_redis.rb index 895730b3d05..09264be9343 100644 --- a/lib/discourse_redis.rb +++ b/lib/discourse_redis.rb @@ -18,7 +18,7 @@ class DiscourseRedis def verify_master synchronize do - return if @running && !recently_checked? + return if @running || recently_checked? @running = true end @@ -58,7 +58,7 @@ class DiscourseRedis def recently_checked? if @last_checked - Time.zone.now > (@last_checked + 5.seconds) + Time.zone.now <= (@last_checked + 5.seconds) else false end diff --git a/spec/components/discourse_redis_spec.rb b/spec/components/discourse_redis_spec.rb index 29c02ff2495..73a3614d7c8 100644 --- a/spec/components/discourse_redis_spec.rb +++ b/spec/components/discourse_redis_spec.rb @@ -96,6 +96,14 @@ describe DiscourseRedis do fallback_handler.master = true end end + + it "should restrict the number of checks" do + expect { fallback_handler.verify_master }.to change { Thread.list.count }.by(1) + expect(fallback_handler.master).to eq(true) + + fallback_handler.master = false + expect { fallback_handler.verify_master }.to_not change { Thread.list.count } + end end end end