Class: Redlock::Client::RedisInstance
- Inherits:
-
Object
- Object
- Redlock::Client::RedisInstance
- Defined in:
- lib/redlock/client.rb,
lib/redlock/testing.rb
Instance Method Summary collapse
- #get_remaining_ttl(resource) ⇒ Object
-
#initialize(connection) ⇒ RedisInstance
constructor
A new instance of RedisInstance.
- #initialize_client(options) ⇒ Object
- #load_scripts ⇒ Object
- #load_scripts_without_testing ⇒ Object
- #lock(resource, val, ttl, allow_new_lock) ⇒ Object
- #synchronize ⇒ Object
- #unlock(resource, val) ⇒ Object
Constructor Details
#initialize(connection) ⇒ RedisInstance
Returns a new instance of RedisInstance.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/redlock/client.rb', line 160 def initialize(connection) @monitor = Monitor.new if connection.respond_to?(:call) @redis = connection else if connection.respond_to?(:client) @redis = connection elsif connection.respond_to?(:key?) @redis = initialize_client(connection) else @redis = connection end end end |
Instance Method Details
#get_remaining_ttl(resource) ⇒ Object
221 222 223 224 225 226 227 228 229 |
# File 'lib/redlock/client.rb', line 221 def get_remaining_ttl(resource) recover_from_script_flush do synchronize { |conn| conn.call('EVALSHA', Scripts::PTTL_SCRIPT_SHA, 1, resource) } end rescue RedisClient::ConnectionError nil end |
#initialize_client(options) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/redlock/client.rb', line 176 def initialize_client() if .key?(:sentinels) if url = .delete(:url) uri = URI.parse(url) if !.key?(:name) && uri.host [:name] = uri.host end if !.key?(:password) && uri.password && !uri.password.empty? [:password] = uri.password end if !.key?(:username) && uri.user && !uri.user.empty? [:username] = uri.user end end RedisClient.sentinel(**).new_client else RedisClient.config(**).new_client end end |
#load_scripts ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/redlock/client.rb', line 233 def load_scripts scripts = [ Scripts::UNLOCK_SCRIPT, Scripts::LOCK_SCRIPT, Scripts::PTTL_SCRIPT ] synchronize do |connnection| scripts.each do |script| connnection.call('SCRIPT', 'LOAD', script) end end end |
#load_scripts_without_testing ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/redlock/testing.rb', line 40 def load_scripts scripts = [ Scripts::UNLOCK_SCRIPT, Scripts::LOCK_SCRIPT, Scripts::PTTL_SCRIPT ] synchronize do |connnection| scripts.each do |script| connnection.call('SCRIPT', 'LOAD', script) end end end |
#lock(resource, val, ttl, allow_new_lock) ⇒ Object
203 204 205 206 207 208 209 |
# File 'lib/redlock/client.rb', line 203 def lock(resource, val, ttl, allow_new_lock) recover_from_script_flush do synchronize { |conn| conn.call('EVALSHA', Scripts::LOCK_SCRIPT_SHA, 1, resource, val, ttl, allow_new_lock) } end end |
#synchronize ⇒ Object
199 200 201 |
# File 'lib/redlock/client.rb', line 199 def synchronize @monitor.synchronize { @redis.then { |connection| yield(connection) } } end |
#unlock(resource, val) ⇒ Object
211 212 213 214 215 216 217 218 219 |
# File 'lib/redlock/client.rb', line 211 def unlock(resource, val) recover_from_script_flush do synchronize { |conn| conn.call('EVALSHA', Scripts::UNLOCK_SCRIPT_SHA, 1, resource, val) } end rescue # Nothing to do, unlocking is just a best-effort attempt. end |