Permalink
Browse files

somewhat exponential randomized backoff in the blocking proxy

  • Loading branch information...
1 parent 41a93be commit b70f4505b040096bbb6627c951a6efb3029b7175 @freels committed Aug 26, 2010
Showing with 15 additions and 5 deletions.
  1. +15 −5 lib/kestrel/client/blocking.rb
@@ -1,26 +1,36 @@
module Kestrel
class Client
class Blocking < Proxy
- SLEEP_TIME = 0.5
+
+ # random backoff sleeping
+
+ SLEEP_TIMES = [[0] * 1, [0.01] * 2, [0.1] * 2, [0.5] * 2, [1.0] * 1].flatten
def get(*args)
- times = 0
+ count = 0
- loop do
- times += 1
+ while count += 1
if response = client.get(*args)
return response
end
- sleep SLEEP_TIME if times > 5
+ sleep_for_count(count)
end
end
def get_without_blocking(*args)
client.get(*args)
end
+ private
+
+ def sleep_for_count(count)
+ base = SLEEP_TIMES[count] || SLEEP_TIMES.last
+
+ time = ((rand * base) + base) / 2
+ sleep time if time > 0
+ end
end
end
end

0 comments on commit b70f450

Please sign in to comment.