Example #1
0
    def run(
        cls,
        config: Optional[DefaultConfig] = None,
        host: Optional[str] = None,
        port: Optional[int] = None,
        server_credentials: Optional[grpc.ServerCredentials] = None,
        block: Optional[bool] = None,
    ):
        if config is None:
            config = DefaultConfig()
        host = host or config.GRPC_SERVER_HOST
        port = port or config.GRPC_SERVER_PORT

        socket_bind_test(host, port)

        cls.as_view()
        for bp_cls in cls.get_blueprints():
            bp_cls.as_view()

        generate_proto_file(
            template_path_root=config.PROTO_TEMPLATE_ROOT,
            template_path=config.PROTO_TEMPLATE_PATH,
        )

        if config.GRPC_SERVER_PROCESS_COUNT > 1:
            address_family = select_address_family(host)
            server_address = get_sockaddr(host, port, address_family)
            with socket.socket(address_family, socket.SOCK_STREAM) as sock:
                sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
                if sock.getsockopt(socket.SOL_SOCKET,
                                   socket.SO_REUSEPORT) == 0:
                    raise RuntimeError("Failed to set SO_REUSEPORT.")
                sock.bind(server_address)
                config.GRPC_SERVER_OPTIONS.append(("grpc.so_reuseport", 1))
                for _ in range(config.GRPC_SERVER_PROCESS_COUNT):
                    # NOTE: It is imperative that the worker subprocesses be forked before
                    # any gRPC servers start up. See
                    # https://github.com/grpc/grpc/issues/16001 for more details.
                    worker = multiprocessing.Process(
                        target=cls._run,
                        kwargs=dict(
                            config=config,
                            target=f"{host}:{port}",
                            server_credentials=server_credentials,
                            block=True,
                        ),
                    )
                    worker.start()
                    cls.workers.append(worker)
                if block:
                    for worker in cls.workers:
                        worker.join()
        else:
            return cls._run(
                config=config,
                target=f"{host}:{port}",
                server_credentials=server_credentials,
                block=block,
            )
Example #2
0
 def test_socket_bind_test_INET6(self):
     host = "::"
     address_family = select_address_family(host)
     with socket.socket(address_family, socket.SOCK_STREAM) as s:
         s.bind(("", 0))
         port = s.getsockname()[1]
         with self.assertRaises(OSError):
             socket_bind_test(host, port)
Example #3
0
 def test_socket_bind_test(self):
     host = "0.0.0.0"
     port = 50051
     address_family = select_address_family(host)
     server_address = get_sockaddr(host, port, address_family)
     with socket.socket(address_family, socket.SOCK_STREAM) as s:
         s.bind(server_address)
         with self.assertRaises(OSError):
             socket_bind_test(host, port)
Example #4
0
 def test_socket_bind_test_UNIX(self):
     host = "unix://test"
     address_family = select_address_family(host)
     with socket.socket(address_family, socket.SOCK_STREAM) as s:
         s.bind("test")
         port = s.getsockname()[1]
         server_address = get_sockaddr(host, port, address_family)
         with self.assertRaises(OSError):
             socket_bind_test(host, port)
         if os.path.exists(server_address):
             os.remove(server_address)
Example #5
0
 def test_socket_bind_test(self):
     unix_socket = "0.0.0.0:50051"
     for host in ["0.0.0.0", "::", f"unix://{unix_socket}"]:
         port = 50051
         address_family = select_address_family(host)
         server_address = get_sockaddr(host, port, address_family)
         with self.subTest(f"host: {host}"):
             with socket.socket(address_family, socket.SOCK_STREAM) as s:
                 s.bind(server_address)
                 with self.assertRaises(OSError):
                     socket_bind_test(host, port)
     if os.path.exists(unix_socket):
         os.remove(unix_socket)