From d8f8350d1cc7735ce733d630019e3967960f1e7e Mon Sep 17 00:00:00 2001 From: Nick Elser Date: Sun, 12 Apr 2015 15:57:32 -0700 Subject: [PATCH] check timeout at the entry of the loop --- lib/suo/client/memcached.rb | 32 ++++++++++++++++---------------- lib/suo/client/redis.rb | 33 ++++++++++++++++----------------- 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/lib/suo/client/memcached.rb b/lib/suo/client/memcached.rb index 08a1d6d..3c69afd 100644 --- a/lib/suo/client/memcached.rb +++ b/lib/suo/client/memcached.rb @@ -16,7 +16,12 @@ module Suo 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 @@ -38,11 +43,6 @@ module Suo 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 => _ @@ -60,6 +60,11 @@ module Suo 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 @@ -76,11 +81,6 @@ module Suo 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 => _ @@ -98,6 +98,11 @@ module Suo 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 @@ -114,11 +119,6 @@ module Suo # 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 diff --git a/lib/suo/client/redis.rb b/lib/suo/client/redis.rb index 113072f..9d6275b 100644 --- a/lib/suo/client/redis.rb +++ b/lib/suo/client/redis.rb @@ -17,6 +17,11 @@ module Suo 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 + client.watch(key) do begin val = client.get(key) @@ -41,15 +46,9 @@ module Suo break if acquisition_token - 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 - raise boom + rescue => _ raise Suo::Client::FailedToAcquireLock end @@ -65,6 +64,11 @@ module Suo 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 + client.watch(key) do begin val = client.get(key) @@ -87,11 +91,6 @@ module Suo break if refreshed - 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 => _ @@ -111,6 +110,11 @@ module Suo options[:retry_count].times do cleared = false + if options[:retry_timeout] + now = Time.now.to_f + break if now - start > options[:retry_timeout] + end + client.watch(key) do begin val = client.get(key) @@ -144,11 +148,6 @@ module Suo break if cleared - 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