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)
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)
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)
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)
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
def start_collector(server: grpc.Server) -> None: print("Starting gRPC collector server") server.add_insecure_port("[::]:50051") server.start()