lib/httpx/plugins/retries.rb in httpx-0.6.7 vs lib/httpx/plugins/retries.rb in httpx-0.7.0

- old
+ new

@@ -26,15 +26,15 @@ def self.extra_options(options) Class.new(options.class) do # number of seconds after which one can retry the request def_option(:retry_after) do |num| # return early if callable - return num if num.respond_to?(:call) + unless num.respond_to?(:call) + num = Integer(num) + raise Error, ":retry_after must be positive" unless num.positive? + end - num = Integer(num) - raise Error, ":retry_after must be positive" unless num.positive? - num end def_option(:max_retries) do |num| num = Integer(num) @@ -44,11 +44,11 @@ end def_option(:retry_change_requests) def_option(:retry_on) do |callback| - raise ":retry_on must be called with the response" unless callback.respond_to?(:call) && callback.method(:call).arity == 1 + raise ":retry_on must be called with the response" unless callback.respond_to?(:call) callback end end.new(options).merge(max_retries: MAX_RETRIES) end @@ -71,12 +71,26 @@ __retryable_error?(response.error) && (!retry_on || retry_on.call(response)) request.retries -= 1 log { "failed to get response, #{request.retries} tries to go..." } request.transition(:idle) - connection = find_connection(request, connections, options) - __retry_request(connection, request, options) + + retry_after = options.retry_after + if retry_after + retry_after = retry_after.call(request) if retry_after.respond_to?(:call) + + log { "retrying after #{retry_after} secs..." } + pool.after(retry_after) do + log { "retrying!!" } + connection = find_connection(request, connections, options) + connection.send(request) + end + else + connection = find_connection(request, connections, options) + connection.send(request) + end + return end response end @@ -84,26 +98,9 @@ IDEMPOTENT_METHODS.include?(request.verb) || options.retry_change_requests end def __retryable_error?(ex) RETRYABLE_ERRORS.any? { |klass| ex.is_a?(klass) } - end - - def __retry_request(connection, request, options) - retry_after = options.retry_after - unless retry_after - connection.send(request) - set_request_timeout(connection, request, options) - return - end - - retry_after = retry_after.call(request) if retry_after.respond_to?(:call) - log { "retrying after #{retry_after} secs..." } - pool.after(retry_after) do - log { "retrying!!" } - connection.send(request) - set_request_timeout(connection, request, options) - end end end module RequestMethods attr_accessor :retries