lib/suo/client/memcached.rb in suo-0.1.0 vs lib/suo/client/memcached.rb in suo-0.1.1

- old
+ new

@@ -14,11 +14,16 @@ client = options[:client] begin start = Time.now.to_f - options[:retry_count].times do |i| + options[:retry_count].times do + if options[:retry_timeout] + now = Time.now.to_f + break if now - start > options[:retry_timeout] + end + val, cas = client.get_cas(key) # no key has been set yet; we could simply set it, but would lead to race conditions on the initial setting if val.nil? client.set(key, "") @@ -36,15 +41,10 @@ acquisition_token = token break end end - if options[:retry_timeout] - now = Time.now.to_f - break if now - start > options[:retry_timeout] - end - sleep(rand(options[:retry_delay] * 1000).to_f / 1000) end rescue => _ raise FailedToAcquireLock end @@ -58,10 +58,15 @@ begin 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 + val, cas = client.get_cas(key) # much like with initial set - ensure the key is here if val.nil? client.set(key, "") @@ -74,15 +79,10 @@ newval = serialize_locks(locks) break if client.set_cas(key, newval, cas) - if options[:retry_timeout] - now = Time.now.to_f - break if now - start > options[:retry_timeout] - end - sleep(rand(options[:retry_delay] * 1000).to_f / 1000) end rescue => _ raise FailedToAcquireLock end @@ -96,10 +96,15 @@ begin 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 + val, cas = client.get_cas(key) break if val.nil? # lock has expired totally locks = clear_expired_locks(deserialize_locks(val), options) @@ -111,14 +116,9 @@ newval = serialize_locks(locks) break if client.set_cas(key, newval, cas) # another client cleared a token in the interim - try again! - - if options[:retry_timeout] - now = Time.now.to_f - break if now - start > options[:retry_timeout] - end sleep(rand(options[:retry_delay] * 1000).to_f / 1000) end rescue => boom # rubocop:disable Lint/HandleExceptions # since it's optimistic locking - fine if we are unable to release