def handle_retry_after(response)
      retry_after = response['Retry-After']
      return response if retry_after.nil?

      retry_sleep = parse_retry_after_header(retry_after)
      # Recover remote hostname if Net::HTTPResponse was generated by a
      # method that fills in the uri attribute.
      #
      server_hostname = if response.uri.is_a?(URI)
                          response.uri.host
                        else
                          # TRANSLATORS: Used in the phrase:
                          # "Received a response from the remote server."
                          _('the remote server')
                        end

      if retry_sleep.nil?
        Puppet.err(_('Received a %{status_code} response from %{server_hostname}, but the Retry-After header value of "%{retry_after}" could not be converted to an integer or RFC 2822 date.') %
                   {status_code: response.code,
                    server_hostname: server_hostname,
                    retry_after: retry_after.inspect})

        return response
      end

      # Cap maximum sleep at the run interval of the Puppet agent.
      retry_sleep = [retry_sleep, Puppet[:runinterval]].min

      Puppet.warning(_('Received a %{status_code} response from %{server_hostname}. Sleeping for %{retry_sleep} seconds before retrying the request.') %
                     {status_code: response.code,
                      server_hostname: server_hostname,
                      retry_sleep: retry_sleep})

      ::Kernel.sleep(retry_sleep)

      return nil
    end
def handle_retry_after(response)
      retry_after = response['Retry-After']
      return response if retry_after.nil?

      retry_sleep = parse_retry_after_header(retry_after)
      # Recover remote hostname if Net::HTTPResponse was generated by a
      # method that fills in the uri attribute.
      #
      server_hostname = if response.uri.is_a?(URI)
                          response.uri.host
                        else
                          # TRANSLATORS: Used in the phrase:
                          # "Received a response from the remote server."
                          _('the remote server')
                        end

      if retry_sleep.nil?
        Puppet.err(_('Received a %{status_code} response from %{server_hostname}, but the Retry-After header value of "%{retry_after}" could not be converted to an integer or RFC 2822 date.') %
                   {status_code: response.code,
                    server_hostname: server_hostname,
                    retry_after: retry_after.inspect})

        return response
      end

      # Cap maximum sleep at the run interval of the Puppet agent.
      retry_sleep = [retry_sleep, Puppet[:runinterval]].min

      Puppet.warning(_('Received a %{status_code} response from %{server_hostname}. Sleeping for %{retry_sleep} seconds before retrying the request.') %
                     {status_code: response.code,
                      server_hostname: server_hostname,
                      retry_sleep: retry_sleep})

      ::Kernel.sleep(retry_sleep)

      return nil
    end
Handles the Retry-After header of a HTTPResponse

 This method checks the response for a Retry-After header and handles
 it by sleeping for the indicated number of seconds. The response is
 returned unmodified if no Retry-After header is present.

 @param response [Net::HTTPResponse] A response received from the
   HTTP client.

 @return [nil] Sleeps and returns nil if the response contained a
   Retry-After header that indicated the request should be retried.
 @return [Net::HTTPResponse] Returns the `response` unmodified if
   no Retry-After header was present or the Retry-After header could
   not be parsed as an integer or RFC 2822 date.