Skip to content

Commit

Permalink
Merge pull request #66 from leandromoreira/clock_monotonic
Browse files Browse the repository at this point in the history
Use CLOCK_MONOTONIC where it is available.
  • Loading branch information
leandromoreira authored Aug 20, 2018
2 parents a228ddc + b823334 commit 08aea1f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/redlock/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,27 @@ class Client
DEFAULT_RETRY_JITTER = 50
CLOCK_DRIFT_FACTOR = 0.01

##
# Returns default time source function depending on CLOCK_MONOTONIC availability.
#
def self.default_time_source
if defined?(Process::CLOCK_MONOTONIC)
proc { (Process.clock_gettime(Process::CLOCK_MONOTONIC) * 1000).to_i }
else
proc { (Time.now.to_f * 1000).to_i }
end
end

# 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`, `retry_delay` and `retry_gitter`.
# +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)
def initialize(servers = DEFAULT_REDIS_URLS, options = {})
redis_timeout = options[:redis_timeout] || DEFAULT_REDIS_TIMEOUT
@servers = servers.map do |server|
Expand All @@ -33,6 +46,7 @@ def initialize(servers = DEFAULT_REDIS_URLS, options = {})
@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

# Locks a resource for a given time.
Expand Down Expand Up @@ -190,8 +204,8 @@ def drift(ttl)
end

def timed
start_time = (Time.now.to_f * 1000).to_i
[yield, (Time.now.to_f * 1000).to_i - start_time]
start_time = @time_source.call()
[yield, @time_source.call() - start_time]
end
end
end
19 changes: 19 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,23 @@
end
end
end

describe '#default_time_source' do
context 'when CLOCK_MONOTONIC is available (MRI, JRuby)' do
it 'returns a callable using Process.clock_gettime()' do
skip 'CLOCK_MONOTONIC not defined' unless defined?(Process::CLOCK_MONOTONIC)
expect(Process).to receive(:clock_gettime).with(Process::CLOCK_MONOTONIC).and_call_original
Redlock::Client.default_time_source.call()
end
end

context 'when CLOCK_MONOTONIC is not available' do
it 'returns a callable using Time.now()' do
cm = Process.send(:remove_const, :CLOCK_MONOTONIC)
expect(Time).to receive(:now).and_call_original
Redlock::Client.default_time_source.call()
Process.const_set(:CLOCK_MONOTONIC, cm) if cm
end
end
end
end

0 comments on commit 08aea1f

Please sign in to comment.