Class: DistributeLock
- Inherits:
-
Object
- Object
- DistributeLock
- Includes:
- RedisLua
- Defined in:
- lib/multi_dbs_load_balancer/distribute_lock.rb
Instance Attribute Summary collapse
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
-
#initialize(redis) ⇒ DistributeLock
constructor
A new instance of DistributeLock.
- #synchronize(name, lock_time = 3600) ⇒ Object
Methods included from RedisLua
Constructor Details
#initialize(redis) ⇒ DistributeLock
Returns a new instance of DistributeLock.
5 6 7 |
# File 'lib/multi_dbs_load_balancer/distribute_lock.rb', line 5 def initialize(redis) @redis = redis end |
Instance Attribute Details
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
3 4 5 |
# File 'lib/multi_dbs_load_balancer/distribute_lock.rb', line 3 def redis @redis end |
Instance Method Details
#synchronize(name, lock_time = 3600) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/multi_dbs_load_balancer/distribute_lock.rb', line 9 def synchronize(name, lock_time = 3600) return yield if @redis.nil? lock_name = "#{name}:lock" time_lock = eval_lua_script(LOCK_SCRIPT, LOCK_SCRIPT_SHA1, [lock_name], [lock_time]) return if time_lock.nil? begin yield ensure eval_lua_script(UNLOCK_SCRIPT, UNLOCK_SCRIPT_SHA1, [name], [time_lock.to_s]) end rescue yield end |