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 14 def initialize(retry_limit, max_sleep) 15 @retry_limit = retry_limit 16 @max_sleep = max_sleep 17 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 29 def retry_after?(request, response) 30 case response.code 31 when 429, 503 32 true 33 else 34 false 35 end 36 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 50 def retry_after_interval(request, response, retries) 51 raise Puppet::HTTP::TooManyRetryAfters.new(request.uri) if retries >= @retry_limit 52 53 retry_after = response['Retry-After'] 54 return nil unless retry_after 55 56 seconds = parse_retry_after(retry_after) 57 58 # if retry-after is far in the future, we could end up sleeping repeatedly 59 # for 30 minutes, effectively waiting indefinitely, seems like we should wait 60 # in total for 30 minutes, in which case this upper limit needs to be enforced 61 # by the client. 62 [seconds, @max_sleep].min 63 end
Private Instance Methods
# File lib/puppet/http/retry_after_handler.rb 67 def parse_retry_after(retry_after) 68 Integer(retry_after) 69 rescue TypeError, ArgumentError 70 begin 71 tm = DateTime.rfc2822(retry_after) 72 seconds = (tm.to_time - DateTime.now.to_time).to_i 73 [seconds, 0].max 74 rescue ArgumentError 75 raise Puppet::HTTP::ProtocolError.new(_("Failed to parse Retry-After header '%{retry_after}' as an integer or RFC 2822 date") % { retry_after: retry_after }) 76 end 77 end