예제 #1
0
def start_grpc_server(server: Server):
    """
    Start the given gRPC `server`.

    This does not block so, if you don't have a mainloop, this will simply
    finish immediatly with the thread/process.
    """
    server.start()
예제 #2
0
def _configure_test_server(server: grpc.Server, port: int, secure_mode: bool,
                           server_id: str) -> None:
    test_pb2_grpc.add_TestServiceServicer_to_server(
        TestService(server_id, socket.gethostname()), server)
    listen_address = f"{_LISTEN_HOST}:{port}"
    if not secure_mode:
        server.add_insecure_port(listen_address)
    else:
        logger.info("Running with xDS Server credentials")
        server_fallback_creds = grpc.insecure_server_credentials()
        server_creds = grpc.xds_server_credentials(server_fallback_creds)
        server.add_secure_port(listen_address, server_creds)
예제 #3
0
파일: server.py 프로젝트: yashykt/grpc
def _configure_greeter_server(server: grpc.Server, port: int,
                              secure_mode: bool, hostname) -> None:
    # Add the application servicer to the server.
    helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(hostname),
                                                      server)
    listen_address = f"{_LISTEN_HOST}:{port}"
    if not secure_mode:
        server.add_insecure_port(listen_address)
    else:
        # Use xDS credentials.
        logger.info("Running with xDS Server credentials")

        # Fall back to insecure credentials.
        server_fallback_creds = grpc.insecure_server_credentials()
        server_creds = grpc.xds_server_credentials(server_fallback_creds)
        server.add_secure_port(listen_address, server_creds)
예제 #4
0
def handler(signum, frame, server: grpc.Server = None) -> None:
    _LOGGER.info('received signal {}'.format(signum))

    if server is not None:
        event = server.stop(
            _SERVER_GRACEFUL_STOP_PERIOD)  # type: threading.Event
        # wait until server stops all in-flight rpcs or graceful period expires
        event.wait()
        exit(0)
예제 #5
0
def stop_grpc_server(server: Server, timeout: int = 1):
    """
    Stop the given gRPC `server`.

    Allow a grace period set by the provided `timeout` (in seconds). Block
    until that timeout is reached.
    """
    terminated_event = server.stop(grace=timeout)
    terminated_event.wait(timeout=timeout + 0.2)
예제 #6
0
def _configure_maintenance_server(server: grpc.Server,
                                  maintenance_port: int) -> None:
    channelz.add_channelz_servicer(server)
    listen_address = f"{_LISTEN_HOST}:{maintenance_port}"
    server.add_insecure_port(listen_address)
    health_servicer = grpc_health.HealthServicer(
        experimental_non_blocking=True,
        experimental_thread_pool=futures.ThreadPoolExecutor(
            max_workers=_THREAD_POOL_SIZE))

    health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)
    SERVICE_NAMES = (
        test_pb2.DESCRIPTOR.services_by_name["TestService"].full_name,
        health_pb2.DESCRIPTOR.services_by_name["Health"].full_name,
        channelz_pb2.DESCRIPTOR.services_by_name["Channelz"].full_name,
        reflection.SERVICE_NAME,
    )
    for service in SERVICE_NAMES:
        health_servicer.set(service, health_pb2.HealthCheckResponse.SERVING)
    reflection.enable_server_reflection(SERVICE_NAMES, server)
예제 #7
0
    def __finalize_shutdown(self, terminating_server: Server,
                            request: ShutdownRequest):
        # Wait for all pending requests to be terminated
        self.logger.debug("Waiting to terminate all requests")
        terminating_server.wait_for_termination()
        self.logger.debug(f"RPC server shut down on port {self.__port}")

        # Need to wait before real shutdown?
        # This may be useful to avoid being restarted by an orchestration manager (e.g. Docker Swarm), typically when doing a graceful shutdown before upgrade
        if request is not None and request.timeout >= 0:
            timeout = request.timeout if request.timeout > 0 else RpcStaticConfig.SHUTDOWN_TIMEOUT.int_val
            self.logger.warning(f"!!! Will shutdown in {timeout}s !!!")
            time.sleep(timeout)

        # Hack auto client to remove timeout
        self.client.srv.info.timeout = None

        # Just make sure that client calls are not working anymore with current instance
        # (Sometimes, it appears that the internal implementation is a bit lazy to close...)
        self.logger.debug(
            "Trying a last client call to make sure server socket is closed (following ERROR is normal)"
        )
        while True:
            try:
                # Try a client call
                self.client.srv.info(Filter())

                # Shouldn't get here; if so, wait a bit and retry
                time.sleep(0.2)  # pragma: no cover
            except Exception:
                # Ok, client is closed
                break

        # Removing all rotating loggers
        for descriptor in self.__real_descriptors:
            clean_rotating_handler(descriptor.manager.logger)

        # Remove rotating handler for current + root loggers
        clean_rotating_handler(logging.getLogger())
        self.__shutdown_event.set()
예제 #8
0
파일: server.py 프로젝트: yashykt/grpc
def _configure_maintenance_server(server: grpc.Server,
                                  maintenance_port: int) -> None:
    listen_address = f"{_LISTEN_HOST}:{maintenance_port}"
    server.add_insecure_port(listen_address)

    # Create a health check servicer. We use the non-blocking implementation
    # to avoid thread starvation.
    health_servicer = health.HealthServicer(
        experimental_non_blocking=True,
        experimental_thread_pool=futures.ThreadPoolExecutor(
            max_workers=_THREAD_POOL_SIZE))

    # Create a tuple of all of the services we want to export via reflection.
    services = tuple(
        service.full_name
        for service in helloworld_pb2.DESCRIPTOR.services_by_name.values()) + (
            reflection.SERVICE_NAME, health.SERVICE_NAME)

    # Mark all services as healthy.
    health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)
    for service in services:
        health_servicer.set(service, health_pb2.HealthCheckResponse.SERVING)
    reflection.enable_server_reflection(services, server)
예제 #9
0
def create_transaction_storage_server(server: grpc.Server, tx_storage: TransactionStorage,
                                      port: Optional[int] = None) -> Tuple[protos.TransactionStorageServicer, int]:
    """Create a GRPC servicer for the given storage, returns a (servicer, port) tuple.

    :param server: a GRPC server
    :type server: :py:class:`grpc.Server`

    :param tx_storage: an instance of TransactionStorage
    :type tx_storage: :py:class:`hathor.transaction.storage.TransactionStorage`

    :param port: optional listen port, if None a random port will be chosen (and returned)
    :type server: :py:class:`typing.Optional[int]`

    :rtype :py:class:`typing.Tuple[hathor.protos.TransactionStorageServicer, int]`
    """
    servicer = TransactionStorageServicer(tx_storage)
    protos.add_TransactionStorageServicer_to_server(servicer, server)
    port = server.add_insecure_port('127.0.0.1:0')
    assert port is not None
    return servicer, port
예제 #10
0
def start_collector(server: grpc.Server) -> None:
    print("Starting gRPC collector server")

    server.add_insecure_port("[::]:50051")
    server.start()