Exemple #1
0
 def start(self):
     with threading.Lock():
         self._server_thread = threading.Thread(target=self._start_server)
         self._server_thread.start()
         self._server_thread.join()
         if not self._port:
             raise errors.GrpcPortError('Error assigning grpc api port!')
    def _start_grpc_server(self):
        """Starts gRPC API server."""
        grpc_service_pb2 = __import__(
            'google.appengine.tools.devappserver2.'
            'grpc_service_pb2', globals(), locals(), ['grpc_service_pb2'])

        class CallHandler(grpc_service_pb2.BetaCallHandlerServicer):
            """Handles gRPC method calls."""
            def HandleCall(self, request, context):
                # TODO: b/36590656#comment3 - Add exception handling logic here.
                api_response = _execute_request(request, use_proto3=True)
                response = grpc_service_pb2.Response(
                    response=api_response.Encode())
                return response

        self._grpc_server = grpc_service_pb2.beta_create_CallHandler_server(
            CallHandler())

        # add_insecure_port() returns positive port number when port allocation is
        # successful. Otherwise it returns 0.
        # 'localhost' works with both ipv4 and ipv6.
        self._grpc_api_port = self._grpc_server.add_insecure_port(
            'localhost:' + str(self._grpc_api_port))
        if not self._grpc_api_port:
            raise errors.GrpcPortError('Error assigning grpc api port!')

        # We set this GRPC_PORT in environment variable as it is only accessed by
        # the devappserver process.
        os.environ['GRPC_PORT'] = str(self._grpc_api_port)
        logging.info('Starting GRPC_API_server at: http://localhost:%d',
                     self._grpc_api_port)
        self._grpc_server.start()
Exemple #3
0
class APIServer(wsgi_server.WsgiServer):
  """Serves API calls over HTTP and GRPC(optional)."""

  def __init__(self, host, port, app_id, use_grpc=False, grpc_api_port=0):
    self._app_id = app_id
    self._host = host
    super(APIServer, self).__init__((host, port), self)
    self.set_balanced_address('localhost:8080')

    self._use_grpc = use_grpc
    self._grpc_api_port = grpc_api_port

  def _start_grpc_server(self):
    """Starts gRPC API server."""
    grpc_service_pb2 = __import__('google.appengine.tools.devappserver2.'
                                  'grpc_service_pb2', globals(), locals(),
                                  ['grpc_service_pb2'])

    class CallHandler(grpc_service_pb2.BetaCallHandlerServicer):
      """Handles gRPC method calls."""

      def HandleCall(self, request, context):
        response = grpc_service_pb2.Response()
        try:
          api_response = _execute_request(request, use_proto3=True)
          response.response = api_response.Encode()
        except apiproxy_errors.ApplicationError, e:
          response.application_error.code = e.application_error
          response.application_error.detail = e.error_detail
        return response

    self._grpc_server = grpc_service_pb2.beta_create_CallHandler_server(
        CallHandler())

    # add_insecure_port() returns positive port number when port allocation is
    # successful. Otherwise it returns 0.
    # 'localhost' works with both ipv4 and ipv6.
    self._grpc_api_port = self._grpc_server.add_insecure_port(
        'localhost:' + str(self._grpc_api_port))
    if not self._grpc_api_port:
      raise errors.GrpcPortError('Error assigning grpc api port!')

    datastore_v3_stub = apiproxy_stub_map.apiproxy.GetStub('datastore_v3')
    if isinstance(datastore_v3_stub, datastore_grpc_stub.DatastoreGrpcStub):
      datastore_v3_stub.SetTxnAddTaskCallbackHostPort(
          'localhost:%d' % self._grpc_api_port)

    # We set this GRPC_PORT in environment variable as it is only accessed by
    # the devappserver process.
    os.environ['GRPC_PORT'] = str(self._grpc_api_port)
    logging.info('Starting GRPC_API_server at: http://localhost:%d',
                 self._grpc_api_port)
    self._grpc_server.start()