class Puppet::HTTP::RetryAfterHandler
Parse information relating to responses containing a Retry-After headers
@api private
Public Class Methods
Create a handler to allow the system to sleep between HTTP requests
@param [Integer] retry_limit number of retries allowed @param [Integer] max_sleep maximum sleep time allowed
# File lib/puppet/http/retry_after_handler.rb 13 def initialize(retry_limit, max_sleep) 14 @retry_limit = retry_limit 15 @max_sleep = max_sleep 16 end
Public Instance Methods
Does the response from the server tell us to wait until we attempt the next retry?
@param [Net::HTTP] request @param [Puppet::HTTP::Response] response
@return [Boolean] Return true if the response code is 429 or 503, return
false otherwise
@api private
# File lib/puppet/http/retry_after_handler.rb 28 def retry_after?(request, response) 29 case response.code 30 when 429, 503 31 true 32 else 33 false 34 end 35 end
The amount of time to wait before attempting a retry
@param [Net::HTTP] request @param [Puppet::HTTP::Response] response @param [Integer] retries number of retries attempted so far
@return [Integer] the amount of time to wait
@raise [Puppet::HTTP::TooManyRetryAfters] raise if we have hit our retry
limit
@api private
# File lib/puppet/http/retry_after_handler.rb 49 def retry_after_interval(request, response, retries) 50 raise Puppet::HTTP::TooManyRetryAfters.new(request.uri) if retries >= @retry_limit 51 52 retry_after = response['Retry-After'] 53 return nil unless retry_after 54 55 seconds = parse_retry_after(retry_after) 56 57 # if retry-after is far in the future, we could end up sleeping repeatedly 58 # for 30 minutes, effectively waiting indefinitely, seems like we should wait 59 # in total for 30 minutes, in which case this upper limit needs to be enforced 60 # by the client. 61 [seconds, @max_sleep].min 62 end
Private Instance Methods
# File lib/puppet/http/retry_after_handler.rb 66 def parse_retry_after(retry_after) 67 Integer(retry_after) 68 rescue TypeError, ArgumentError 69 begin 70 tm = DateTime.rfc2822(retry_after) 71 seconds = (tm.to_time - DateTime.now.to_time).to_i 72 [seconds, 0].max 73 rescue ArgumentError 74 raise Puppet::HTTP::ProtocolError.new(_("Failed to parse Retry-After header '%{retry_after}' as an integer or RFC 2822 date") % { retry_after: retry_after }) 75 end 76 end