lib/suo/client/base.rb in suo-0.1.2 vs lib/suo/client/base.rb in suo-0.1.3

- old
+ new

@@ -1,8 +1,9 @@ module Suo module Client class Base + DEFAULT_OPTIONS = { retry_timeout: 0.1, retry_delay: 0.01, stale_lock_expiration: 3600 }.freeze @@ -109,11 +110,11 @@ acquisition_lock = remove_lock(locks, acquisition_token) break unless acquisition_lock break if set(key, serialize_locks(locks), cas, options) end - rescue FailedToAcquireLock => _ # rubocop:disable Lint/HandleExceptions + rescue LockClientError => _ # rubocop:disable Lint/HandleExceptions # ignore - assume success due to optimistic locking end def clear(key, options = {}) # rubocop:disable Lint/UnusedMethodArgument fail NotImplementedError @@ -122,12 +123,10 @@ def merge_defaults(options = {}) options = self::DEFAULT_OPTIONS.merge(options) fail "Client required" unless options[:client] - options[:retry_count] = (options[:retry_timeout] / options[:retry_delay].to_f).ceil - options end private @@ -146,25 +145,25 @@ def synchronize(key, options) yield(key, options) end def retry_with_timeout(key, options) + count = (options[:retry_timeout] / options[:retry_delay].to_f).ceil + start = Time.now.to_f - options[:retry_count].times do - if options[:retry_timeout] - now = Time.now.to_f - break if now - start > options[:retry_timeout] - end + count.times do + now = Time.now.to_f + break if now - start > options[:retry_timeout] synchronize(key, options) do yield end sleep(rand(options[:retry_delay] * 1000).to_f / 1000) end rescue => _ - raise FailedToAcquireLock + raise LockClientError end def serialize_locks(locks) MessagePack.pack(locks.map { |time, token| [time.to_f, token] }) end