def _RetryRequest(self, request):
    """Retry the request a few times before we determine it failed.

    Sometimes the frontend becomes loaded and issues a 500 error to throttle the
    clients. We wait Client.error_poll_min seconds between each attempt to back
    off the frontend. Note that this does not affect any timing algorithm in the
    client itself which is controlled by the Timer() class.

    Args:
      request: A urllib2 request object.

    Returns:
      a tuple of duration, urllib2.urlopen response.

    """
    while True:
      try:
        now = time.time()
        result = urllib2.urlopen(request, timeout=config_lib.CONFIG[
            "Client.http_timeout"])

        return time.time() - now, result

      except (urllib2.HTTPError, urllib2.URLError) as e:
        self.consecutive_connection_errors += 1

        # Request failed. If we connected successfully before we attempt a few
        # connections before we determine that it really failed. This might
        # happen if the front end is loaded and returns a few throttling 500
        # messages.
        if self.active_base_url is not None:
          # Propagate 406 immediately without retrying, as 406 is a valid
          # response that inidicate a need for enrollment.
          if getattr(e, "code", None) == 406:
            raise

          if self.consecutive_connection_errors >= self.retry_error_limit:
            # We tried several times but this really did not work, just fail it.
            logging.info(
                "Too many connection errors to %s, retrying another URL",
                self.active_base_url)
            self.active_base_url = None
            raise e

          # Back off hard to allow the front end to recover.
          logging.debug(
              "Unable to connect to frontend. Backing off %s seconds.",
              self.error_poll_min)
          self.Wait(self.error_poll_min)

        # We never previously connected, maybe the URL/proxy is wrong? Just fail
        # right away to allow callers to try a different URL.
        else:
          raise e
dataset/ETHPy150Open google/grr/grr/client/comms.py/HTTPManager._RetryRequest/original