|
|
@ -23,6 +23,7 @@ import six |
|
|
|
|
|
|
|
|
|
|
|
import grpc |
|
|
|
import grpc |
|
|
|
from grpc import _common |
|
|
|
from grpc import _common |
|
|
|
|
|
|
|
from grpc import _interceptor |
|
|
|
from grpc._cython import cygrpc |
|
|
|
from grpc._cython import cygrpc |
|
|
|
from grpc.framework.foundation import callable_util |
|
|
|
from grpc.framework.foundation import callable_util |
|
|
|
|
|
|
|
|
|
|
@ -541,17 +542,25 @@ def _handle_stream_stream(rpc_event, state, method_handler, thread_pool): |
|
|
|
method_handler.request_deserializer, method_handler.response_serializer) |
|
|
|
method_handler.request_deserializer, method_handler.response_serializer) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _find_method_handler(rpc_event, generic_handlers): |
|
|
|
def _find_method_handler(rpc_event, generic_handlers, interceptor_pipeline): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def query_handlers(handler_call_details): |
|
|
|
for generic_handler in generic_handlers: |
|
|
|
for generic_handler in generic_handlers: |
|
|
|
method_handler = generic_handler.service( |
|
|
|
method_handler = generic_handler.service(handler_call_details) |
|
|
|
_HandlerCallDetails( |
|
|
|
|
|
|
|
_common.decode(rpc_event.request_call_details.method), |
|
|
|
|
|
|
|
rpc_event.request_metadata)) |
|
|
|
|
|
|
|
if method_handler is not None: |
|
|
|
if method_handler is not None: |
|
|
|
return method_handler |
|
|
|
return method_handler |
|
|
|
else: |
|
|
|
|
|
|
|
return None |
|
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handler_call_details = _HandlerCallDetails( |
|
|
|
|
|
|
|
_common.decode(rpc_event.request_call_details.method), |
|
|
|
|
|
|
|
rpc_event.request_metadata) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if interceptor_pipeline is not None: |
|
|
|
|
|
|
|
return interceptor_pipeline.execute(query_handlers, |
|
|
|
|
|
|
|
handler_call_details) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
return query_handlers(handler_call_details) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _reject_rpc(rpc_event, status, details): |
|
|
|
def _reject_rpc(rpc_event, status, details): |
|
|
|
operations = (cygrpc.operation_send_initial_metadata((), _EMPTY_FLAGS), |
|
|
|
operations = (cygrpc.operation_send_initial_metadata((), _EMPTY_FLAGS), |
|
|
@ -587,13 +596,14 @@ def _handle_with_method_handler(rpc_event, method_handler, thread_pool): |
|
|
|
method_handler, thread_pool) |
|
|
|
method_handler, thread_pool) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _handle_call(rpc_event, generic_handlers, thread_pool, |
|
|
|
def _handle_call(rpc_event, generic_handlers, interceptor_pipeline, thread_pool, |
|
|
|
concurrency_exceeded): |
|
|
|
concurrency_exceeded): |
|
|
|
if not rpc_event.success: |
|
|
|
if not rpc_event.success: |
|
|
|
return None, None |
|
|
|
return None, None |
|
|
|
if rpc_event.request_call_details.method is not None: |
|
|
|
if rpc_event.request_call_details.method is not None: |
|
|
|
try: |
|
|
|
try: |
|
|
|
method_handler = _find_method_handler(rpc_event, generic_handlers) |
|
|
|
method_handler = _find_method_handler(rpc_event, generic_handlers, |
|
|
|
|
|
|
|
interceptor_pipeline) |
|
|
|
except Exception as exception: # pylint: disable=broad-except |
|
|
|
except Exception as exception: # pylint: disable=broad-except |
|
|
|
details = 'Exception servicing handler: {}'.format(exception) |
|
|
|
details = 'Exception servicing handler: {}'.format(exception) |
|
|
|
logging.exception(details) |
|
|
|
logging.exception(details) |
|
|
@ -621,12 +631,14 @@ class _ServerStage(enum.Enum): |
|
|
|
|
|
|
|
|
|
|
|
class _ServerState(object): |
|
|
|
class _ServerState(object): |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, completion_queue, server, generic_handlers, thread_pool, |
|
|
|
# pylint: disable=too-many-arguments |
|
|
|
maximum_concurrent_rpcs): |
|
|
|
def __init__(self, completion_queue, server, generic_handlers, |
|
|
|
|
|
|
|
interceptor_pipeline, thread_pool, maximum_concurrent_rpcs): |
|
|
|
self.lock = threading.Lock() |
|
|
|
self.lock = threading.Lock() |
|
|
|
self.completion_queue = completion_queue |
|
|
|
self.completion_queue = completion_queue |
|
|
|
self.server = server |
|
|
|
self.server = server |
|
|
|
self.generic_handlers = list(generic_handlers) |
|
|
|
self.generic_handlers = list(generic_handlers) |
|
|
|
|
|
|
|
self.interceptor_pipeline = interceptor_pipeline |
|
|
|
self.thread_pool = thread_pool |
|
|
|
self.thread_pool = thread_pool |
|
|
|
self.stage = _ServerStage.STOPPED |
|
|
|
self.stage = _ServerStage.STOPPED |
|
|
|
self.shutdown_events = None |
|
|
|
self.shutdown_events = None |
|
|
@ -691,8 +703,8 @@ def _serve(state): |
|
|
|
state.maximum_concurrent_rpcs is not None and |
|
|
|
state.maximum_concurrent_rpcs is not None and |
|
|
|
state.active_rpc_count >= state.maximum_concurrent_rpcs) |
|
|
|
state.active_rpc_count >= state.maximum_concurrent_rpcs) |
|
|
|
rpc_state, rpc_future = _handle_call( |
|
|
|
rpc_state, rpc_future = _handle_call( |
|
|
|
event, state.generic_handlers, state.thread_pool, |
|
|
|
event, state.generic_handlers, state.interceptor_pipeline, |
|
|
|
concurrency_exceeded) |
|
|
|
state.thread_pool, concurrency_exceeded) |
|
|
|
if rpc_state is not None: |
|
|
|
if rpc_state is not None: |
|
|
|
state.rpc_states.add(rpc_state) |
|
|
|
state.rpc_states.add(rpc_state) |
|
|
|
if rpc_future is not None: |
|
|
|
if rpc_future is not None: |
|
|
@ -780,12 +792,14 @@ def _start(state): |
|
|
|
|
|
|
|
|
|
|
|
class Server(grpc.Server): |
|
|
|
class Server(grpc.Server): |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, thread_pool, generic_handlers, options, |
|
|
|
# pylint: disable=too-many-arguments |
|
|
|
|
|
|
|
def __init__(self, thread_pool, generic_handlers, interceptors, options, |
|
|
|
maximum_concurrent_rpcs): |
|
|
|
maximum_concurrent_rpcs): |
|
|
|
completion_queue = cygrpc.CompletionQueue() |
|
|
|
completion_queue = cygrpc.CompletionQueue() |
|
|
|
server = cygrpc.Server(_common.channel_args(options)) |
|
|
|
server = cygrpc.Server(_common.channel_args(options)) |
|
|
|
server.register_completion_queue(completion_queue) |
|
|
|
server.register_completion_queue(completion_queue) |
|
|
|
self._state = _ServerState(completion_queue, server, generic_handlers, |
|
|
|
self._state = _ServerState(completion_queue, server, generic_handlers, |
|
|
|
|
|
|
|
_interceptor.service_pipeline(interceptors), |
|
|
|
thread_pool, maximum_concurrent_rpcs) |
|
|
|
thread_pool, maximum_concurrent_rpcs) |
|
|
|
|
|
|
|
|
|
|
|
def add_generic_rpc_handlers(self, generic_rpc_handlers): |
|
|
|
def add_generic_rpc_handlers(self, generic_rpc_handlers): |
|
|
|