Skip to content

Commit

Permalink
Refactoring core logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dtaniwaki committed Sep 1, 2014
1 parent f0c07e9 commit 289b42e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 56 deletions.
31 changes: 3 additions & 28 deletions lib/faraday_middleware/retry_proxy_0_8.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,10 @@
module FaradayMiddleware
class RetryProxy < Faraday::Middleware
TARGET_ERRORS = [Errno::ETIMEDOUT, Timeout::Error, Faraday::Error::TimeoutError, Faraday::Error::ConnectionFailed]
require_relative 'retry_proxy_core'

module FaradayMiddleware
class RetryProxy < RetryProxyCore
def initialize(app, options = {})
super(app)
@options = options
end

def call(env)
env[:request] ||= {}
proxies = self.normalize_proxies(env[:request][:proxy])
request_body = env[:body]
begin
proxy = proxies.pop
env[:request][:proxy] = proxy ? {uri: URI.parse(proxy)} : nil
env[:body] = request_body
@app.call(env)
rescue *TARGET_ERRORS => exception
if proxies.size > 0
sleep (1 + SecureRandom.random_number * @options[:interval_randomness].to_f) * @options[:interval].to_f
retry
end
raise
end
end

def normalize_proxies(proxy)
if proxy.respond_to?(:call)
proxy = proxy.call(self)
end
proxy ? Array(proxy) : []
end
end
end
31 changes: 3 additions & 28 deletions lib/faraday_middleware/retry_proxy_0_9.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module FaradayMiddleware
class RetryProxy < Faraday::Middleware
TARGET_ERRORS = [Errno::ETIMEDOUT, Timeout::Error, Faraday::Error::TimeoutError, Faraday::Error::ConnectionFailed]
require_relative 'retry_proxy_core'

module FaradayMiddleware
class RetryProxy < RetryProxyCore
class Options < Faraday::Options.new(:interval, :interval_randomness)
def self.from(value)
if Fixnum === value
Expand All @@ -24,30 +24,5 @@ def initialize(app, options = nil)
super(app)
@options = Options.from(options)
end

def call(env)
env[:request] ||= {}
proxies = self.normalize_proxies(env[:request][:proxy])
request_body = env[:body]
begin
proxy = proxies.pop
env[:request][:proxy] = proxy ? {uri: URI.parse(proxy)} : nil
env[:body] = request_body
@app.call(env)
rescue *TARGET_ERRORS => exception
if proxies.size > 0
sleep (1 + SecureRandom.random_number * @options[:interval_randomness].to_f) * @options[:interval].to_f
retry
end
raise
end
end

def normalize_proxies(proxy)
if proxy.respond_to?(:call)
proxy = proxy.call(self)
end
proxy ? Array(proxy) : []
end
end
end
30 changes: 30 additions & 0 deletions lib/faraday_middleware/retry_proxy_core.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module FaradayMiddleware
class RetryProxyCore < Faraday::Middleware
TARGET_ERRORS = [Errno::ETIMEDOUT, Timeout::Error, Faraday::Error::TimeoutError, Faraday::Error::ConnectionFailed]

def call(env)
env[:request] ||= {}
proxies = self.normalize_proxies(env[:request][:proxy])
request_body = env[:body]
begin
proxy = proxies.pop
env[:request][:proxy] = proxy ? {uri: URI.parse(proxy)} : nil
env[:body] = request_body
@app.call(env)
rescue *TARGET_ERRORS => exception
if proxies.size > 0
sleep (1 + SecureRandom.random_number * @options[:interval_randomness].to_f) * @options[:interval].to_f
retry
end
raise
end
end

def normalize_proxies(proxy)
if proxy.respond_to?(:call)
proxy = proxy.call(self)
end
proxy ? Array(proxy) : []
end
end
end

0 comments on commit 289b42e

Please sign in to comment.