Class: MySQLMutex

Inherits:
DistributedMutex show all
Defined in:
lib/mysql_mutex.rb

Constant Summary collapse

@@thread_locks =

Accounting for nested locks.

Hash.new { |h,k| h[k] = Hash.new(0) }

Constants inherited from DistributedMutex

DistributedMutex::DEFAULT_EXCEPTION_ON_TIMEOUT, DistributedMutex::DEFAULT_TIMEOUT

Instance Attribute Summary

Attributes inherited from DistributedMutex

#exception_on_timeout, #key, #timeout

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DistributedMutex

#excluse_unlock, #lock, #locked?, #synchronize, #try_lock, #unlock

Constructor Details

#initialize(key, timeout = DEFAULT_TIMEOUT, exception_on_timeout = DEFAULT_EXCEPTION_ON_TIMEOUT, connection = ActiveRecord::Base.connection) ⇒ MySQLMutex

Returns a new instance of MySQLMutex.



8
9
10
11
12
13
14
15
16
# File 'lib/mysql_mutex.rb', line 8

def initialize(key, timeout = DEFAULT_TIMEOUT, exception_on_timeout = DEFAULT_EXCEPTION_ON_TIMEOUT, connection = ActiveRecord::Base.connection)
  super(key, timeout, exception_on_timeout)
  sanitized_key     = connection.quote(key)
  sanitized_timeout = connection.quote(timeout)
  @connection       = connection
  @connection_id    = connection.show_variable('pseudo_thread_id')
  @get_sql          = "SELECT GET_LOCK(#{sanitized_key},#{sanitized_timeout})"
  @release_sql      = "SELECT RELEASE_LOCK(#{sanitized_key})"
end

Class Method Details

.active_locksObject



23
24
25
# File 'lib/mysql_mutex.rb', line 23

def self.active_locks
  @@thread_locks
end

.synchronize(key, timeout = DEFAULT_TIMEOUT, exception_on_timeout = DEFAULT_TIMEOUT, con = ActiveRecord::Base.connection, &block) ⇒ Object



18
19
20
21
# File 'lib/mysql_mutex.rb', line 18

def self.synchronize(key, timeout = DEFAULT_TIMEOUT, exception_on_timeout = DEFAULT_TIMEOUT, con = ActiveRecord::Base.connection, &block)
  mutex = new(key, timeout, exception_on_timeout, con)
  mutex.synchronize(&block)
end