Skip to content

Commit

Permalink
Lazily enable mysql gaplock protection (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
thom-oman authored May 15, 2020
1 parent 2f4be41 commit c11e039
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
12 changes: 9 additions & 3 deletions lib/statesman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,25 @@ 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
@storage_adapter || Adapters::Memory
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
11 changes: 8 additions & 3 deletions lib/statesman/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c11e039

Please sign in to comment.