Class: Redlock::Client::RedisInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/redlock/client.rb,
lib/redlock/testing.rb

Instance Method Summary collapse

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(options)
  if options.key?(:sentinels)
    if url = options.delete(:url)
      uri = URI.parse(url)
      if !options.key?(:name) && uri.host
        options[:name] = uri.host
      end

      if !options.key?(:password) && uri.password && !uri.password.empty?
        options[:password] = uri.password
      end

      if !options.key?(:username) && uri.user && !uri.user.empty?
        options[:username] = uri.user
      end
    end

    RedisClient.sentinel(**options).new_client
  else
    RedisClient.config(**options).new_client
  end
end

#load_scriptsObject



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_testingObject



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

#synchronizeObject



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