Method: Redlock::Client#initialize

Defined in:
lib/redlock/client.rb

#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
  • `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)

  • `retry_jitter` being how many ms to jitter retry delay (default: 50)

  • `redis_timeout` being how the Redis timeout will be set in seconds (default: 0.1)

  • `time_source` being a callable object returning a monotonic time in milliseconds

    (default: see #default_time_source)
    

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

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).to_i + 1
  @retry_count = options[:retry_count] || DEFAULT_RETRY_COUNT
  @retry_delay = options[:retry_delay] || DEFAULT_RETRY_DELAY
  @retry_jitter = options[:retry_jitter] || DEFAULT_RETRY_JITTER
  @time_source = options[:time_source] || self.class.default_time_source
end