From c11e03921867d46ee43edf473deadc5e5576d242 Mon Sep 17 00:00:00 2001 From: thom-oman Date: Fri, 15 May 2020 10:32:40 +0100 Subject: [PATCH] Lazily enable mysql gaplock protection (#402) --- lib/statesman.rb | 12 +++++++++--- lib/statesman/config.rb | 11 ++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/statesman.rb b/lib/statesman.rb index 336d7e87..b11d6984 100644 --- a/lib/statesman.rb +++ b/lib/statesman.rb @@ -20,12 +20,12 @@ module Adapters # Example: # Statesman.configure do # storage_adapter Statesman::ActiveRecordAdapter + # enable_mysql_gaplock_protection # end # def self.configure(&block) - config = Config.new(block) + @config = Config.new(block) @storage_adapter = config.adapter_class - @mysql_gaplock_protection = config.mysql_gaplock_protection end def self.storage_adapter @@ -33,6 +33,12 @@ def self.storage_adapter end def self.mysql_gaplock_protection? - @mysql_gaplock_protection + return @mysql_gaplock_protection unless @mysql_gaplock_protection.nil? + + @mysql_gaplock_protection = config.mysql_gaplock_protection? + end + + def self.config + @config ||= Config.new end end diff --git a/lib/statesman/config.rb b/lib/statesman/config.rb index 58aa1600..c18bcf1c 100644 --- a/lib/statesman/config.rb +++ b/lib/statesman/config.rb @@ -5,18 +5,23 @@ module Statesman class Config - attr_reader :adapter_class, :mysql_gaplock_protection + attr_reader :adapter_class def initialize(block = nil) instance_eval(&block) unless block.nil? end def storage_adapter(adapter_class) + @adapter_class = adapter_class + end + + def mysql_gaplock_protection? + return @mysql_gaplock_protection unless @mysql_gaplock_protection.nil? + # If our adapter class suggests we're using mysql, enable gaplock protection by # default. enable_mysql_gaplock_protection if mysql_adapter?(adapter_class) - - @adapter_class = adapter_class + @mysql_gaplock_protection end def enable_mysql_gaplock_protection