Class: Redlock::Client

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

Defined Under Namespace

Classes: RedisInstance

Constant Summary collapse

DEFAULT_REDIS_URLS =
['redis://localhost:6379']
DEFAULT_REDIS_TIMEOUT =
0.1
DEFAULT_RETRY_COUNT =
3
DEFAULT_RETRY_DELAY =
200
CLOCK_DRIFT_FACTOR =
0.01

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(servers = DEFAULT_REDIS_URLS, options = {}) ⇒ Client

Create a distributed lock manager implementing redlock algorithm. Params:

servers

The array of redis connection URLs or Redis connection instances. Or a mix of both.

options

You can override the default value for ‘retry_count` and `retry_delay`.

* `retry_count`   being how many times it'll try to lock a resource (default: 3)
* `retry_delay`   being how many ms to sleep before try to lock again (default: 200)
* `redis_timeout` being how the Redis timeout will be set in seconds (default: 0.1)


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/redlock/client.rb', line 19

def initialize(servers = DEFAULT_REDIS_URLS, options = {})
  redis_timeout = options[:redis_timeout] || DEFAULT_REDIS_TIMEOUT
  @servers = servers.map do |server|
    if server.is_a?(String)
      RedisInstance.new(url: server, timeout: redis_timeout)
    else
      RedisInstance.new(server)
    end
  end
  @quorum = servers.length / 2 + 1
  @retry_count = options[:retry_count] || DEFAULT_RETRY_COUNT
  @retry_delay = options[:retry_delay] || DEFAULT_RETRY_DELAY
end

Instance Attribute Details

#testing_mode=(value) ⇒ Object (writeonly)

Sets the attribute testing_mode

Parameters:

  • value

    the value to set the attribute testing_mode to.



3
4
5
# File 'lib/redlock/testing.rb', line 3

def testing_mode=(value)
  @testing_mode = value
end

Instance Method Details

#lock(resource, ttl, extend: nil, &block) ⇒ Object

Locks a resource for a given time. Params:

resource

the resource (or key) string to be locked.

ttl

The time-to-live in ms for the lock.

extend: A lock (“lock_info”) to extend.

block

an optional block that automatically unlocks the lock.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/redlock/client.rb', line 39

def lock(resource, ttl, extend: nil, &block)
  lock_info = try_lock_instances(resource, ttl, extend)

  if block_given?
    begin
      yield lock_info
      !!lock_info
    ensure
      unlock(lock_info) if lock_info
    end
  else
    lock_info
  end
end

#try_lock_instances(resource, ttl, extend) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/redlock/client.rb', line 108

def try_lock_instances(resource, ttl, extend)
  @retry_count.times do
    lock_info = lock_instances(resource, ttl, extend)
    return lock_info if lock_info

    # Wait a random delay before retrying
    sleep(rand(@retry_delay).to_f / 1000)
  end

  false
end

#try_lock_instances_without_testingObject



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/redlock/testing.rb', line 5

def try_lock_instances(resource, ttl, extend)
  @retry_count.times do
    lock_info = lock_instances(resource, ttl, extend)
    return lock_info if lock_info

    # Wait a random delay before retrying
    sleep(rand(@retry_delay).to_f / 1000)
  end

  false
end

#unlock(lock_info) ⇒ Object

Unlocks a resource. Params:

lock_info

the lock that has been acquired when you locked the resource.



57
58
59
# File 'lib/redlock/client.rb', line 57

def unlock(lock_info)
  @servers.each { |s| s.unlock(lock_info[:resource], lock_info[:value]) }
end

#unlock_without_testingObject

Unlocks a resource. Params:

lock_info

the lock that has been acquired when you locked the resource.



21
22
23
# File 'lib/redlock/testing.rb', line 21

def unlock(lock_info)
  @servers.each { |s| s.unlock(lock_info[:resource], lock_info[:value]) }
end