diff --git a/Pipfile b/Pipfile index 00ac860..a0c4354 100644 --- a/Pipfile +++ b/Pipfile @@ -26,7 +26,7 @@ grpcio-reflection = "*" grpcio-health-checking = "*" [requires] -python_version = "3.7" +python_version = "3.8" [scripts] init-pre-commit = "pre-commit install" diff --git a/grpcalchemy/blueprint.py b/grpcalchemy/blueprint.py index d86f64e..647f6ac 100644 --- a/grpcalchemy/blueprint.py +++ b/grpcalchemy/blueprint.py @@ -16,8 +16,9 @@ ) from google.protobuf.message import Message as GeneratedProtocolMessageType -from grpc._server import _Context as Context from grpc import ServicerContext +from grpc._server import _Context as Context + from .meta import ServiceMeta, __meta__ from .orm import Message diff --git a/grpcalchemy/config.py b/grpcalchemy/config.py index 75bbe2b..35a5613 100644 --- a/grpcalchemy/config.py +++ b/grpcalchemy/config.py @@ -44,5 +44,8 @@ class DefaultConfig(BaseConfig): #: Server Reflection GRPC_SEVER_REFLECTION_ENABLE = False - # :gRPC Service Third-Part Package Support + #: gRPC Service Third-Part Package Support GRPC_THIRD_PART_PACKAGES: List[str] = [] + #: If set to true, retrieves server configuration via xDS. This is an + #: EXPERIMENTAL option. Only for grpcio >= 1.36.0 + GRPC_XDS_SUPPORT = False diff --git a/grpcalchemy/server.py b/grpcalchemy/server.py index 925dd8d..e3a78e6 100644 --- a/grpcalchemy/server.py +++ b/grpcalchemy/server.py @@ -1,16 +1,15 @@ import logging import multiprocessing -import os.path import socket import sys import time from concurrent import futures -from importlib import import_module from threading import Event from typing import Callable, Dict, Optional, Tuple, Type, ContextManager, List import grpc from grpc import GenericRpcHandler +from grpc import __version__ as GRPC_VERSION from grpc._cython import cygrpc from grpc._server import ( _add_generic_handlers, @@ -73,7 +72,12 @@ def __init__(self, config: DefaultConfig): ) completion_queue = cygrpc.CompletionQueue() self.logger.info(f"server options: {self.config.GRPC_SERVER_OPTIONS}") - server = cygrpc.Server(tuple(self.config.GRPC_SERVER_OPTIONS)) + if tuple(map(int, GRPC_VERSION.split("."))) >= (1, 36, 0): + server = cygrpc.Server( + tuple(self.config.GRPC_SERVER_OPTIONS), self.config.GRPC_XDS_SUPPORT + ) + else: + server = cygrpc.Server(tuple(self.config.GRPC_SERVER_OPTIONS)) server.register_completion_queue(completion_queue) #: gRPC Server State diff --git a/grpcalchemy/utils.py b/grpcalchemy/utils.py index b7df2a2..dcf6121 100644 --- a/grpcalchemy/utils.py +++ b/grpcalchemy/utils.py @@ -1,12 +1,14 @@ import socket +import sys from importlib import import_module, reload from os import walk, path, mkdir from os.path import abspath, dirname, exists, join from typing import Union, Optional, TYPE_CHECKING, Tuple -import sys + import grpc_tools.protoc import pkg_resources from jinja2 import Environment, FileSystemLoader + from grpcalchemy.config import DefaultConfig if TYPE_CHECKING: