Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1 from iron-io/retries

Retries
  • Loading branch information...
commit 3097796f8935c9ede1888f5ad147a92252e86223 2 parents 2a4efdf + 0aca698
@iced iced authored
Showing with 50 additions and 7 deletions.
  1. +49 −6 lib/rest/client.rb
  2. +1 −1  test/test_rest.rb
View
55 lib/rest/client.rb
@@ -41,12 +41,14 @@ def self.gem
class Client
+ attr_accessor :options
# options:
# - :gem => specify gem explicitly
#
def initialize(options={})
@logger = Logger.new(STDOUT)
@logger.level=Logger::INFO
+ @options = options
Rest.gem = options[:gem] if options[:gem]
@@ -58,24 +60,65 @@ def initialize(options={})
end
-
def get(url, req_hash={})
- @wrapper.get(url, req_hash)
+ res = nil
+ perform_op do
+ res = @wrapper.get(url, req_hash)
+ end
+ return res
+ end
+
+ def perform_op(&blk)
+ max_retries = @options[:max_retries] || 5
+ current_retry = 0
+ success = false
+ res = nil
+ while current_retry < max_retries do
+ res = yield blk
+ #p res
+ #p res.code
+ if res.code == 503
+ pow = (4 ** (current_retry)) * 100 # milliseconds
+ #puts 'pow=' + pow.to_s
+ s = Random.rand * pow
+ #puts 's=' + s.to_s
+ sleep_secs = 1.0 * s / 1000.0
+ #puts 'sleep for ' + sleep_secs.to_s
+ current_retry += 1
+ @logger.debug "503 Error. Retrying #{current_retry} out of #{max_retries} max."
+ sleep sleep_secs
+ else
+ break
+ end
+ end
+ res
end
# req_hash options:
# - :body => post body
#
def post(url, req_hash={})
- @wrapper.post(url, req_hash)
+ res = nil
+ perform_op do
+ res = @wrapper.post(url, req_hash)
+ end
+ return res
end
def put(url, req_hash={})
- @wrapper.put(url, req_hash)
+ res = nil
+ perform_op do
+ res = @wrapper.put(url, req_hash)
+ end
+ return res
end
def delete(url, req_hash={})
- @wrapper.delete(url, req_hash)
+ res = nil
+ perform_op do
+ res = @wrapper.delete(url, req_hash)
+ end
+ return res
end
end
-end
+end
View
2  test/test_rest.rb
@@ -15,8 +15,8 @@ def setup
def test_basics
response = @rest.get("http://www.github.com")
assert response.code == 200
- assert response.body
#p response.body
+ assert response.body.include?("Social Coding")
end
Please sign in to comment.
Something went wrong with that request. Please try again.