Python httplib.SERVICE_UNAVAILABLE Examples

The following are 17 code examples of httplib.SERVICE_UNAVAILABLE(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module httplib , or try the search function .
Example #1
Source File: backend.py    From aurproxy with Apache License 2.0 6 votes vote down vote up
def _load_proxy_route(self, route):
    locations = self._load_config_item('locations', route, required=True)
    sources = self._load_config_item('sources', route, required=True)
    proxy_sources = self._load_proxy_sources(sources)
    overflow_sources = self._load_config_item('overflow_sources',
                                              route,
                                              required=False,
                                              default=[])
    empty_endpoint_status_code = self._load_config_item(
        'empty_endpoint_status_code',
        route,
        required=False,
        default=httplib.SERVICE_UNAVAILABLE)
    proxy_overflow_sources = self._load_proxy_sources(overflow_sources)
    overflow_threshold_pct = self._load_config_item('overflow_threshold_pct',
                                                    route,
                                                    required=False)
    source_group_manager = SourceGroupManager(proxy_sources,
                                              proxy_overflow_sources,
                                              overflow_threshold_pct,
                                              self._signal_update_fn,)

    return ProxyRoute(
        locations, empty_endpoint_status_code, source_group_manager) 
Example #2
Source File: handlers_test.py    From appengine-mapreduce with Apache License 2.0 6 votes vote down vote up
def testLeaseFreedOnSliceRetry(self):
    # Reinitialize with faulty map function.
    self._init_job(__name__ + "." + test_handler_raise_exception.__name__)
    self._init_shard()
    handler, _ = self._create_handler()
    handler.post()
    self.assertEqual(httplib.SERVICE_UNAVAILABLE, handler.response.status)

    shard_state = model.ShardState.get_by_shard_id(self.shard_id)
    self.assertTrue(shard_state.active)
    # Slice stays the same.
    self.assertEquals(self.CURRENT_SLICE_ID, shard_state.slice_id)
    # Lease is freed.
    self.assertFalse(shard_state.slice_start_time)
    self.assertFalse(shard_state.slice_request_id)
    # Slice retry is increased.
    self.assertEqual(self.shard_state.slice_retries + 1,
                     shard_state.slice_retries) 
Example #3
Source File: handlers_test.py    From appengine-mapreduce with Apache License 2.0 6 votes vote down vote up
def testRequestHasNotEnd(self):
    # Previous request's lease has timed out but the request has not.
    now = datetime.datetime.now()
    old = (now -
           datetime.timedelta(seconds=parameters._LEASE_DURATION_SEC + 1))
    self.shard_state.slice_start_time = old
    self.shard_state.slice_request_id = self.PREVIOUS_REQUEST_ID
    self.shard_state.put()
    handler, _ = self._create_handler()
    # Lease has ended.
    self.assertEqual(0,
                     handler._wait_time(self.shard_state,
                                        parameters._LEASE_DURATION_SEC),
                     lambda: now)
    # Logs API doesn't think the request has ended.
    self.assertFalse(handler._has_old_request_ended(self.shard_state))
    # Request has not timed out.
    self.assertTrue(handler._wait_time(
        self.shard_state,
        parameters._MAX_LEASE_DURATION_SEC,
        lambda: now))
    handler.post()
    self.assertEqual(httplib.SERVICE_UNAVAILABLE, handler.response.status) 
Example #4
Source File: handlers_test.py    From appengine-mapreduce with Apache License 2.0 5 votes vote down vote up
def testFutureTask(self):
    handler, _ = self._create_handler(slice_id=self.CURRENT_SLICE_ID + 1)
    handler.post()
    self.assertEqual(httplib.SERVICE_UNAVAILABLE, handler.response.status) 
Example #5
Source File: batch.py    From pushkin with MIT License 5 votes vote down vote up
def handle_request(self):
        parsed_request = self.parse_request(self.request.body)
        unpacked_requests = self.unpack_batch(parsed_request)
        context.main_logger.debug("Received an event batch of {num_requests} requests in batch handler {handler}"
                                  .format(num_requests=len(unpacked_requests),
                                          handler=self.__class__.__name__))

        if not context.request_processor.submit(self.create_request(unpacked_requests)):
            context.main_logger.warning("RequestProcessor queue size limit reached, sending back off response...")
            self.set_status(httplib.SERVICE_UNAVAILABLE)
        else:
            self.set_status(httplib.OK) 
Example #6
Source File: client.py    From python-phoenixdb with Apache License 2.0 5 votes vote down vote up
def _post_request(self, body, headers):
        retry_count = self.max_retries
        while True:
            logger.debug("POST %s %r %r", self.url.path, body, headers)
            try:
                self.connection.request('POST', self.url.path, body=body, headers=headers)
                response = self.connection.getresponse()
            except httplib.HTTPException as e:
                if retry_count > 0:
                    delay = math.exp(-retry_count)
                    logger.debug("HTTP protocol error, will retry in %s seconds...", delay, exc_info=True)
                    self.close()
                    self.connect()
                    time.sleep(delay)
                    retry_count -= 1
                    continue
                raise errors.InterfaceError('RPC request failed', cause=e)
            else:
                if response.status == httplib.SERVICE_UNAVAILABLE:
                    if retry_count > 0:
                        delay = math.exp(-retry_count)
                        logger.debug("Service unavailable, will retry in %s seconds...", delay, exc_info=True)
                        time.sleep(delay)
                        retry_count -= 1
                        continue
                return response 
Example #7
Source File: handlers_test.py    From appengine-mapreduce with Apache License 2.0 5 votes vote down vote up
def testExceptionInHandler(self):
    """Test behavior when handler throws exception."""
    self.init(__name__ + ".test_handler_raise_exception")
    TestEntity().put()

    # Stub out context._set
    m = mox.Mox()
    m.StubOutWithMock(context.Context, "_set", use_mock_anything=True)

    # Record calls
    context.Context._set(mox.IsA(context.Context))
    context.Context._set(None)

    m.ReplayAll()
    try: # test, verify
      self.handler.post()
      self.assertEqual(httplib.SERVICE_UNAVAILABLE,
                       self.handler.response.status)

      # slice should be still active
      shard_state = model.ShardState.get_by_shard_id(self.shard_id)
      self.verify_shard_state(shard_state, processed=0, slice_retries=1)
      # mapper calls counter should not be incremented
      self.assertEquals(0, shard_state.counters_map.get(
          context.COUNTER_MAPPER_CALLS))

      # new task should not be spawned
      tasks = self.taskqueue.GetTasks("default")
      self.assertEquals(0, len(tasks))

      m.VerifyAll()
    finally:
      m.UnsetStubs() 
Example #8
Source File: handlers_test.py    From appengine-mapreduce with Apache License 2.0 5 votes vote down vote up
def testSliceAndShardRetries(self):
    """Test when a handler throws a non fatal exception."""
    self.init(__name__ + ".test_handler_raise_exception")
    TestEntity().put()

    # First time, the task gets retried.
    self._handle_request(expect_finalize=False)
    self.assertEqual(httplib.SERVICE_UNAVAILABLE, self.handler.response.status)
    self.verify_shard_state(
        model.ShardState.get_by_shard_id(self.shard_id),
        active=True,
        processed=0,
        slice_retries=1)

    # After the Nth attempt on slice, we retry the shard.
    shard_state = model.ShardState.get_by_shard_id(self.shard_id)
    shard_state.slice_retries = (
        parameters.config.TASK_MAX_DATA_PROCESSING_ATTEMPTS)
    shard_state.put()
    # TODO(user): fix
    self.handler.post()
    self.verify_shard_state(
        model.ShardState.get_by_shard_id(self.shard_id),
        active=True,
        result_status=None,
        processed=0,
        slice_retries=0,
        retries=1)

  # TODO(user): test MR jobs that only allow slice or shard retry when
  # it is configurable per job. 
Example #9
Source File: handlers_test.py    From appengine-mapreduce with Apache License 2.0 5 votes vote down vote up
def testLeaseHasNotEnd(self):
    self.shard_state.slice_start_time = datetime.datetime.now()
    self.shard_state.put()
    handler, _ = self._create_handler()

    with mock.patch("datetime.datetime", autospec=True) as dt:
      # One millisecons after.
      dt.now.return_value = (self.shard_state.slice_start_time +
                             datetime.timedelta(milliseconds=1))
      self.assertEqual(
          math.ceil(parameters._LEASE_DURATION_SEC),
          handler._wait_time(self.shard_state,
                             parameters._LEASE_DURATION_SEC))
      handler.post()
      self.assertEqual(httplib.SERVICE_UNAVAILABLE, handler.response.status) 
Example #10
Source File: base_handler.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def retry_task(self):
    """Ask taskqueue to retry this task.

    Even though raising an exception can cause a task retry, it
    will flood logs with highly visible ERROR logs. Handlers should uses
    this method to perform controlled task retries. Only raise exceptions
    for those deserve ERROR log entries.
    """
    self.response.set_status(httplib.SERVICE_UNAVAILABLE, "Retry task")
    self.response.clear() 
Example #11
Source File: base_handler.py    From appengine-mapreduce with Apache License 2.0 5 votes vote down vote up
def retry_task(self):
    """Ask taskqueue to retry this task.

    Even though raising an exception can cause a task retry, it
    will flood logs with highly visible ERROR logs. Handlers should uses
    this method to perform controlled task retries. Only raise exceptions
    for those deserve ERROR log entries.
    """
    self.response.set_status(httplib.SERVICE_UNAVAILABLE, "Retry task")
    self.response.clear() 
Example #12
Source File: base_handler.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def retry_task(self):
    """Ask taskqueue to retry this task.

    Even though raising an exception can cause a task retry, it
    will flood logs with highly visible ERROR logs. Handlers should uses
    this method to perform controlled task retries. Only raise exceptions
    for those deserve ERROR log entries.
    """
    self.response.set_status(httplib.SERVICE_UNAVAILABLE, "Retry task")
    self.response.clear() 
Example #13
Source File: base_handler.py    From locality-sensitive-hashing with MIT License 5 votes vote down vote up
def retry_task(self):
    """Ask taskqueue to retry this task.

    Even though raising an exception can cause a task retry, it
    will flood logs with highly visible ERROR logs. Handlers should uses
    this method to perform controlled task retries. Only raise exceptions
    for those deserve ERROR log entries.
    """
    self.response.set_status(httplib.SERVICE_UNAVAILABLE, "Retry task")
    self.response.clear() 
Example #14
Source File: dev_appserver_multiprocess.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def handle_one_request(self):
    """Override. Invoked from BaseHTTPRequestHandler constructor."""
    self.raw_requestline = self.rfile.readline()
    if not self.raw_requestline:
      self.close_connection = 1
      return
    if not self.parse_request():
      return

    process = GlobalProcess()
    balance_set = process.GetBalanceSet()
    request_size = int(self.headers.get('content-length', 0))
    payload = self.rfile.read(request_size)




    for port in balance_set:
      logging.debug('balancer to port %d',  port)
      connection = self.connection_handler(process.host, port=port)


      connection.response_class = ForwardResponse
      connection.request(self.command, self.path, payload, dict(self.headers))
      try:
        response = connection.getresponse()
      except httplib.HTTPException, e:


        self.send_error(httplib.INTERNAL_SERVER_ERROR, str(e))
        return

      if response.status != httplib.SERVICE_UNAVAILABLE:
        self.wfile.write(response.data)
        return 
Example #15
Source File: dev_appserver_multiprocess.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def handle_one_request(self):
    """Override."""
    self.raw_requestline = self.rfile.readline()
    if not self.raw_requestline:
      self.close_connection = 1
      return
    if not self.parse_request():
      return
    self.send_error(httplib.SERVICE_UNAVAILABLE, 'Busy.') 
Example #16
Source File: client.py    From nacos-sdk-python with Apache License 2.0 4 votes vote down vote up
def _do_sync_req(self, url, headers=None, params=None, data=None, timeout=None, method="GET"):
        url = "?".join([url, urlencode(params)]) if params else url
        all_headers = self._get_common_headers(params, data)
        if headers:
            all_headers.update(headers)
        logger.debug(
            "[do-sync-req] url:%s, headers:%s, params:%s, data:%s, timeout:%s" % (
                url, all_headers, params, data, timeout))
        tries = 0
        while True:
            try:
                server_info = self.get_server()
                if not server_info:
                    logger.error("[do-sync-req] can not get one server.")
                    raise NacosRequestException("Server is not available.")
                address, port = server_info
                server = ":".join([address, str(port)])
                server_url = "%s://%s" % ("http", server)
                if python_version_bellow("3"):
                    req = Request(url=server_url + url, data=urlencode(data).encode() if data else None,
                                  headers=all_headers)
                    req.get_method = lambda: method
                else:
                    req = Request(url=server_url + url, data=urlencode(data).encode() if data else None,
                                  headers=all_headers, method=method)

                # for python version compatibility
                if python_version_bellow("2.7.9"):
                    resp = urlopen(req, timeout=timeout)
                else:
                    resp = urlopen(req, timeout=timeout, context=None)
                logger.debug("[do-sync-req] info from server:%s" % server)
                return resp
            except HTTPError as e:
                if e.code in [HTTPStatus.INTERNAL_SERVER_ERROR, HTTPStatus.BAD_GATEWAY,
                              HTTPStatus.SERVICE_UNAVAILABLE]:
                    logger.warning("[do-sync-req] server:%s is not available for reason:%s" % (server, e.msg))
                else:
                    raise
            except socket.timeout:
                logger.warning("[do-sync-req] %s request timeout" % server)
            except URLError as e:
                logger.warning("[do-sync-req] %s connection error:%s" % (server, e.reason))

            tries += 1
            if tries >= len(self.server_list):
                logger.error("[do-sync-req] %s maybe down, no server is currently available" % server)
                raise NacosRequestException("All server are not available")
            self.change_server()
            logger.warning("[do-sync-req] %s maybe down, skip to next" % server) 
Example #17
Source File: client.py    From acm-sdk-python with Apache License 2.0 4 votes vote down vote up
def _do_sync_req(self, url, headers=None, params=None, data=None, timeout=None):
        url = "?".join([url, urlencode(params)]) if params else url
        all_headers = self._get_common_headers(params, data)
        if headers:
            all_headers.update(headers)
        logger.debug(
            "[do-sync-req] url:%s, headers:%s, params:%s, data:%s, timeout:%s" % (
                url, all_headers, params, data, timeout))
        tries = 0
        while True:
            try:
                server_info = self.get_server()
                if not server_info:
                    logger.error("[do-sync-req] can not get one server.")
                    raise ACMRequestException("Server is not available.")
                address, port, is_ip_address = server_info
                server = ":".join([address, str(port)])
                # if tls is enabled and server address is in ip, turn off verification

                server_url = "%s://%s" % ("https" if self.tls_enabled else "http", server)
                req = Request(url=server_url + url, data=urlencode(data).encode() if data else None,
                              headers=all_headers)

                # for python version compatibility
                if python_version_bellow("2.7.9"):
                    resp = urlopen(req, timeout=timeout)
                else:
                    if self.tls_enabled and is_ip_address:
                        context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
                        context.check_hostname = False
                    else:
                        context = None
                    resp = urlopen(req, timeout=timeout, context=context)
                logger.debug("[do-sync-req] info from server:%s" % server)
                return resp
            except HTTPError as e:
                if e.code in [HTTPStatus.INTERNAL_SERVER_ERROR, HTTPStatus.BAD_GATEWAY,
                              HTTPStatus.SERVICE_UNAVAILABLE]:
                    logger.warning("[do-sync-req] server:%s is not available for reason:%s" % (server, e.msg))
                else:
                    raise
            except socket.timeout:
                logger.warning("[do-sync-req] %s request timeout" % server)
            except URLError as e:
                logger.warning("[do-sync-req] %s connection error:%s" % (server, e.reason))

            tries += 1
            if tries >= len(self.server_list):
                logger.error("[do-sync-req] %s maybe down, no server is currently available" % server)
                raise ACMRequestException("All server are not available")
            self.change_server()
            logger.warning("[do-sync-req] %s maybe down, skip to next" % server)