Skip to content

Commit

Permalink
hacking on retries, not done.
Browse files Browse the repository at this point in the history
  • Loading branch information
treeder committed Mar 4, 2012
1 parent 2a4efdf commit a4926f0
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions lib/rest/client.rb
Expand Up @@ -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]

Expand All @@ -58,24 +60,44 @@ def initialize(options={})

end


def get(url, req_hash={})
@wrapper.get(url, req_hash)
end
max_retries = @options[:retries] || 0
current_retry = 0
success = false
while current_retry <= max_retries do
#fmt.Println(num, "Pushing", i, "try", currentRetry, "total:", TotalCount)
res = @wrapper.get(url, req_hash)
if current_retry >= max_retries
return res
end
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
sleep sleep_secs
else
success = true
break
end
current_retry += 1
end

# req_hash options:
# - :body => post body
#
def post(url, req_hash={})
@wrapper.post(url, req_hash)
end
# req_hash options:
# - :body => post body
#
def post(url, req_hash={})
@wrapper.post(url, req_hash)
end

def put(url, req_hash={})
@wrapper.put(url, req_hash)
end
def put(url, req_hash={})
@wrapper.put(url, req_hash)
end

def delete(url, req_hash={})
@wrapper.delete(url, req_hash)
def delete(url, req_hash={})
@wrapper.delete(url, req_hash)
end
end
end
end
end

0 comments on commit a4926f0

Please sign in to comment.