mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-09-01 00:00:35 -04:00
added the retries plugin, which hops on fetch_response and resends a request while it's retryable
This commit is contained in:
parent
0c46794c20
commit
694549eb95
50
lib/httpx/plugins/retries.rb
Normal file
50
lib/httpx/plugins/retries.rb
Normal file
@ -0,0 +1,50 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module HTTPX
|
||||
module Plugins
|
||||
module Retries
|
||||
MAX_RETRIES = 3
|
||||
|
||||
module InstanceMethods
|
||||
def max_retries(n)
|
||||
branch(default_options.with_max_retries(n.to_i))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fetch_response(request)
|
||||
response = super
|
||||
if response.is_a?(ErrorResponse) &&
|
||||
request.retries > 0
|
||||
request.retries -= 1
|
||||
channel = find_channel(request)
|
||||
channel.send(request)
|
||||
return
|
||||
end
|
||||
response
|
||||
end
|
||||
end
|
||||
|
||||
module RequestMethods
|
||||
attr_accessor :retries
|
||||
|
||||
def initialize(*args)
|
||||
super
|
||||
@retries = @options.max_retries || MAX_RETRIES
|
||||
end
|
||||
end
|
||||
|
||||
module OptionsMethods
|
||||
def self.included(klass)
|
||||
super
|
||||
klass.def_option(:max_retries) do |num|
|
||||
num = Integer(num)
|
||||
raise Error, ":max_retries must be positive" unless num.positive?
|
||||
num
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
register_plugin :retries, Retries
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user