From 12fdb93ba863988652459eb827f1be00161b5e42 Mon Sep 17 00:00:00 2001 From: Mike Bourgeous Date: Thu, 13 Nov 2014 16:14:47 -0700 Subject: [PATCH 1/3] Allow specifying the HTTP methods retried by Request::Retry. --- lib/faraday/request/retry.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/faraday/request/retry.rb b/lib/faraday/request/retry.rb index 08bc83766..73efb73b2 100644 --- a/lib/faraday/request/retry.rb +++ b/lib/faraday/request/retry.rb @@ -22,7 +22,8 @@ class Request::Retry < Faraday::Middleware IDEMPOTENT_METHODS = [:delete, :get, :head, :options, :put] - class Options < Faraday::Options.new(:max, :interval, :interval_randomness, :backoff_factor, :exceptions, :retry_if) + class Options < Faraday::Options.new(:max, :interval, :interval_randomness, :backoff_factor, + :exceptions, :methods, :retry_if) DEFAULT_CHECK = lambda { |env,exception| false } def self.from(value) @@ -54,6 +55,10 @@ def exceptions Error::TimeoutError]) end + def methods + Array(self[:methods] ||= Faraday::Request::Retry::IDEMPOTENT_METHODS) + end + def retry_if self[:retry_if] ||= DEFAULT_CHECK end @@ -75,6 +80,9 @@ def retry_if # given as Class, Module, or String. (default: # [Errno::ETIMEDOUT, Timeout::Error, # Error::TimeoutError]) + # methods - A list of HTTP methods to retry without calling retry_if. Pass + # an empty Array to call retry_if for all exceptions. + # (defaults to the idempotent HTTP methods in IDEMPOTENT_METHODS) # retry_if - block that will receive the env object and the exception raised # and should decide if the code should retry still the action or # not independent of the retry count. This would be useful @@ -133,7 +141,7 @@ def build_exception_matcher(exceptions) private def retry_request?(env, exception) - IDEMPOTENT_METHODS.include?(env[:method]) || @options.retry_if.call(env, exception) + @options.methods.include?(env[:method]) || @options.retry_if.call(env, exception) end end From b1948c994b25c601d31f7c1d0b8f1f4efbc0e8a2 Mon Sep 17 00:00:00 2001 From: Mike Bourgeous Date: Thu, 13 Nov 2014 16:30:22 -0700 Subject: [PATCH 2/3] Added tests of customizable methods to retry without calling retry_if. --- test/middleware/retry_test.rb | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/test/middleware/retry_test.rb b/test/middleware/retry_test.rb index 845b44659..14f485fbb 100644 --- a/test/middleware/retry_test.rb +++ b/test/middleware/retry_test.rb @@ -126,7 +126,7 @@ def test_should_stop_retrying_if_block_returns_false_checking_exception assert_equal 1, @times_called end - def test_should_not_call_retry_if_for_idempotent_methods + def test_should_not_call_retry_if_for_idempotent_methods_if_methods_unspecified @explode = lambda {|n| raise Errno::ETIMEDOUT } check = lambda { |env,exception| raise "this should have never been called" } assert_raises(Errno::ETIMEDOUT) { @@ -135,7 +135,7 @@ def test_should_not_call_retry_if_for_idempotent_methods assert_equal 3, @times_called end - def test_should_not_retry_for_non_idempotent_method + def test_should_not_retry_for_non_idempotent_method_if_methods_unspecified @explode = lambda {|n| raise Errno::ETIMEDOUT } assert_raises(Errno::ETIMEDOUT) { conn.post("/unstable") @@ -143,5 +143,23 @@ def test_should_not_retry_for_non_idempotent_method assert_equal 1, @times_called end + def test_should_not_call_retry_if_for_specified_methods + @explode = lambda {|n| raise Errno::ETIMEDOUT } + check = lambda { |env,exception| raise "this should have never been called" } + assert_raises(Errno::ETIMEDOUT) { + conn(:retry_if => check, :methods => [:post]).post("/unstable") + } + assert_equal 3, @times_called + end + + def test_should_call_retry_if_for_empty_method_list + @explode = lambda {|n| raise Errno::ETIMEDOUT } + check = lambda { |env,exception| @times_called < 2 } + assert_raises(Errno::ETIMEDOUT) { + conn(:retry_if => check, :methods => []).get("/unstable") + } + assert_equal 2, @times_called + end + end end From baa537d89c03660031d2ad307c4a0532aa462838 Mon Sep 17 00:00:00 2001 From: Ruslan Korolev Date: Wed, 26 Nov 2014 15:58:03 +0300 Subject: [PATCH 3/3] clean constant IDEMPOTENT_METHODS --- lib/faraday/request/retry.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/faraday/request/retry.rb b/lib/faraday/request/retry.rb index 73efb73b2..cf3169e96 100644 --- a/lib/faraday/request/retry.rb +++ b/lib/faraday/request/retry.rb @@ -56,7 +56,7 @@ def exceptions end def methods - Array(self[:methods] ||= Faraday::Request::Retry::IDEMPOTENT_METHODS) + Array(self[:methods] ||= IDEMPOTENT_METHODS) end def retry_if