_interceptor.py 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. # Copyright 2019 gRPC authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Interceptors implementation of gRPC Asyncio Python."""
  15. from abc import ABCMeta
  16. from abc import abstractmethod
  17. import asyncio
  18. import collections
  19. import functools
  20. from typing import (AsyncIterable, Awaitable, Callable, Iterator, Optional,
  21. Sequence, Union)
  22. import grpc
  23. from grpc._cython import cygrpc
  24. from . import _base_call
  25. from ._call import AioRpcError
  26. from ._call import StreamStreamCall
  27. from ._call import StreamUnaryCall
  28. from ._call import UnaryStreamCall
  29. from ._call import UnaryUnaryCall
  30. from ._call import _API_STYLE_ERROR
  31. from ._call import _RPC_ALREADY_FINISHED_DETAILS
  32. from ._call import _RPC_HALF_CLOSED_DETAILS
  33. from ._metadata import Metadata
  34. from ._typing import DeserializingFunction
  35. from ._typing import DoneCallbackType
  36. from ._typing import RequestIterableType
  37. from ._typing import RequestType
  38. from ._typing import ResponseIterableType
  39. from ._typing import ResponseType
  40. from ._typing import SerializingFunction
  41. from ._utils import _timeout_to_deadline
  42. _LOCAL_CANCELLATION_DETAILS = 'Locally cancelled by application!'
  43. class ServerInterceptor(metaclass=ABCMeta):
  44. """Affords intercepting incoming RPCs on the service-side.
  45. This is an EXPERIMENTAL API.
  46. """
  47. @abstractmethod
  48. async def intercept_service(
  49. self, continuation: Callable[[grpc.HandlerCallDetails],
  50. Awaitable[grpc.RpcMethodHandler]],
  51. handler_call_details: grpc.HandlerCallDetails
  52. ) -> grpc.RpcMethodHandler:
  53. """Intercepts incoming RPCs before handing them over to a handler.
  54. Args:
  55. continuation: A function that takes a HandlerCallDetails and
  56. proceeds to invoke the next interceptor in the chain, if any,
  57. or the RPC handler lookup logic, with the call details passed
  58. as an argument, and returns an RpcMethodHandler instance if
  59. the RPC is considered serviced, or None otherwise.
  60. handler_call_details: A HandlerCallDetails describing the RPC.
  61. Returns:
  62. An RpcMethodHandler with which the RPC may be serviced if the
  63. interceptor chooses to service this RPC, or None otherwise.
  64. """
  65. class ClientCallDetails(
  66. collections.namedtuple(
  67. 'ClientCallDetails',
  68. ('method', 'timeout', 'metadata', 'credentials', 'wait_for_ready')),
  69. grpc.ClientCallDetails):
  70. """Describes an RPC to be invoked.
  71. This is an EXPERIMENTAL API.
  72. Args:
  73. method: The method name of the RPC.
  74. timeout: An optional duration of time in seconds to allow for the RPC.
  75. metadata: Optional metadata to be transmitted to the service-side of
  76. the RPC.
  77. credentials: An optional CallCredentials for the RPC.
  78. wait_for_ready: This is an EXPERIMENTAL argument. An optional
  79. flag to enable :term:`wait_for_ready` mechanism.
  80. """
  81. method: str
  82. timeout: Optional[float]
  83. metadata: Optional[Metadata]
  84. credentials: Optional[grpc.CallCredentials]
  85. wait_for_ready: Optional[bool]
  86. class ClientInterceptor(metaclass=ABCMeta):
  87. """Base class used for all Aio Client Interceptor classes"""
  88. class UnaryUnaryClientInterceptor(ClientInterceptor, metaclass=ABCMeta):
  89. """Affords intercepting unary-unary invocations."""
  90. @abstractmethod
  91. async def intercept_unary_unary(
  92. self, continuation: Callable[[ClientCallDetails, RequestType],
  93. UnaryUnaryCall],
  94. client_call_details: ClientCallDetails,
  95. request: RequestType) -> Union[UnaryUnaryCall, ResponseType]:
  96. """Intercepts a unary-unary invocation asynchronously.
  97. Args:
  98. continuation: A coroutine that proceeds with the invocation by
  99. executing the next interceptor in the chain or invoking the
  100. actual RPC on the underlying Channel. It is the interceptor's
  101. responsibility to call it if it decides to move the RPC forward.
  102. The interceptor can use
  103. `call = await continuation(client_call_details, request)`
  104. to continue with the RPC. `continuation` returns the call to the
  105. RPC.
  106. client_call_details: A ClientCallDetails object describing the
  107. outgoing RPC.
  108. request: The request value for the RPC.
  109. Returns:
  110. An object with the RPC response.
  111. Raises:
  112. AioRpcError: Indicating that the RPC terminated with non-OK status.
  113. asyncio.CancelledError: Indicating that the RPC was canceled.
  114. """
  115. class UnaryStreamClientInterceptor(ClientInterceptor, metaclass=ABCMeta):
  116. """Affords intercepting unary-stream invocations."""
  117. @abstractmethod
  118. async def intercept_unary_stream(
  119. self, continuation: Callable[[ClientCallDetails, RequestType],
  120. UnaryStreamCall],
  121. client_call_details: ClientCallDetails, request: RequestType
  122. ) -> Union[ResponseIterableType, UnaryStreamCall]:
  123. """Intercepts a unary-stream invocation asynchronously.
  124. The function could return the call object or an asynchronous
  125. iterator, in case of being an asyncrhonous iterator this will
  126. become the source of the reads done by the caller.
  127. Args:
  128. continuation: A coroutine that proceeds with the invocation by
  129. executing the next interceptor in the chain or invoking the
  130. actual RPC on the underlying Channel. It is the interceptor's
  131. responsibility to call it if it decides to move the RPC forward.
  132. The interceptor can use
  133. `call = await continuation(client_call_details, request)`
  134. to continue with the RPC. `continuation` returns the call to the
  135. RPC.
  136. client_call_details: A ClientCallDetails object describing the
  137. outgoing RPC.
  138. request: The request value for the RPC.
  139. Returns:
  140. The RPC Call or an asynchronous iterator.
  141. Raises:
  142. AioRpcError: Indicating that the RPC terminated with non-OK status.
  143. asyncio.CancelledError: Indicating that the RPC was canceled.
  144. """
  145. class StreamUnaryClientInterceptor(ClientInterceptor, metaclass=ABCMeta):
  146. """Affords intercepting stream-unary invocations."""
  147. @abstractmethod
  148. async def intercept_stream_unary(
  149. self,
  150. continuation: Callable[[ClientCallDetails, RequestType],
  151. StreamUnaryCall],
  152. client_call_details: ClientCallDetails,
  153. request_iterator: RequestIterableType,
  154. ) -> StreamUnaryCall:
  155. """Intercepts a stream-unary invocation asynchronously.
  156. Within the interceptor the usage of the call methods like `write` or
  157. even awaiting the call should be done carefully, since the caller
  158. could be expecting an untouched call, for example for start writing
  159. messages to it.
  160. Args:
  161. continuation: A coroutine that proceeds with the invocation by
  162. executing the next interceptor in the chain or invoking the
  163. actual RPC on the underlying Channel. It is the interceptor's
  164. responsibility to call it if it decides to move the RPC forward.
  165. The interceptor can use
  166. `call = await continuation(client_call_details, request_iterator)`
  167. to continue with the RPC. `continuation` returns the call to the
  168. RPC.
  169. client_call_details: A ClientCallDetails object describing the
  170. outgoing RPC.
  171. request_iterator: The request iterator that will produce requests
  172. for the RPC.
  173. Returns:
  174. The RPC Call.
  175. Raises:
  176. AioRpcError: Indicating that the RPC terminated with non-OK status.
  177. asyncio.CancelledError: Indicating that the RPC was canceled.
  178. """
  179. class StreamStreamClientInterceptor(ClientInterceptor, metaclass=ABCMeta):
  180. """Affords intercepting stream-stream invocations."""
  181. @abstractmethod
  182. async def intercept_stream_stream(
  183. self,
  184. continuation: Callable[[ClientCallDetails, RequestType],
  185. StreamStreamCall],
  186. client_call_details: ClientCallDetails,
  187. request_iterator: RequestIterableType,
  188. ) -> Union[ResponseIterableType, StreamStreamCall]:
  189. """Intercepts a stream-stream invocation asynchronously.
  190. Within the interceptor the usage of the call methods like `write` or
  191. even awaiting the call should be done carefully, since the caller
  192. could be expecting an untouched call, for example for start writing
  193. messages to it.
  194. The function could return the call object or an asynchronous
  195. iterator, in case of being an asyncrhonous iterator this will
  196. become the source of the reads done by the caller.
  197. Args:
  198. continuation: A coroutine that proceeds with the invocation by
  199. executing the next interceptor in the chain or invoking the
  200. actual RPC on the underlying Channel. It is the interceptor's
  201. responsibility to call it if it decides to move the RPC forward.
  202. The interceptor can use
  203. `call = await continuation(client_call_details, request_iterator)`
  204. to continue with the RPC. `continuation` returns the call to the
  205. RPC.
  206. client_call_details: A ClientCallDetails object describing the
  207. outgoing RPC.
  208. request_iterator: The request iterator that will produce requests
  209. for the RPC.
  210. Returns:
  211. The RPC Call or an asynchronous iterator.
  212. Raises:
  213. AioRpcError: Indicating that the RPC terminated with non-OK status.
  214. asyncio.CancelledError: Indicating that the RPC was canceled.
  215. """
  216. class InterceptedCall:
  217. """Base implementation for all intercepted call arities.
  218. Interceptors might have some work to do before the RPC invocation with
  219. the capacity of changing the invocation parameters, and some work to do
  220. after the RPC invocation with the capacity for accessing to the wrapped
  221. `UnaryUnaryCall`.
  222. It handles also early and later cancellations, when the RPC has not even
  223. started and the execution is still held by the interceptors or when the
  224. RPC has finished but again the execution is still held by the interceptors.
  225. Once the RPC is finally executed, all methods are finally done against the
  226. intercepted call, being at the same time the same call returned to the
  227. interceptors.
  228. As a base class for all of the interceptors implements the logic around
  229. final status, metadata and cancellation.
  230. """
  231. _interceptors_task: asyncio.Task
  232. _pending_add_done_callbacks: Sequence[DoneCallbackType]
  233. def __init__(self, interceptors_task: asyncio.Task) -> None:
  234. self._interceptors_task = interceptors_task
  235. self._pending_add_done_callbacks = []
  236. self._interceptors_task.add_done_callback(
  237. self._fire_or_add_pending_done_callbacks)
  238. def __del__(self):
  239. self.cancel()
  240. def _fire_or_add_pending_done_callbacks(
  241. self, interceptors_task: asyncio.Task) -> None:
  242. if not self._pending_add_done_callbacks:
  243. return
  244. call_completed = False
  245. try:
  246. call = interceptors_task.result()
  247. if call.done():
  248. call_completed = True
  249. except (AioRpcError, asyncio.CancelledError):
  250. call_completed = True
  251. if call_completed:
  252. for callback in self._pending_add_done_callbacks:
  253. callback(self)
  254. else:
  255. for callback in self._pending_add_done_callbacks:
  256. callback = functools.partial(self._wrap_add_done_callback,
  257. callback)
  258. call.add_done_callback(callback)
  259. self._pending_add_done_callbacks = []
  260. def _wrap_add_done_callback(self, callback: DoneCallbackType,
  261. unused_call: _base_call.Call) -> None:
  262. callback(self)
  263. def cancel(self) -> bool:
  264. if not self._interceptors_task.done():
  265. # There is no yet the intercepted call available,
  266. # Trying to cancel it by using the generic Asyncio
  267. # cancellation method.
  268. return self._interceptors_task.cancel()
  269. try:
  270. call = self._interceptors_task.result()
  271. except AioRpcError:
  272. return False
  273. except asyncio.CancelledError:
  274. return False
  275. return call.cancel()
  276. def cancelled(self) -> bool:
  277. if not self._interceptors_task.done():
  278. return False
  279. try:
  280. call = self._interceptors_task.result()
  281. except AioRpcError as err:
  282. return err.code() == grpc.StatusCode.CANCELLED
  283. except asyncio.CancelledError:
  284. return True
  285. return call.cancelled()
  286. def done(self) -> bool:
  287. if not self._interceptors_task.done():
  288. return False
  289. try:
  290. call = self._interceptors_task.result()
  291. except (AioRpcError, asyncio.CancelledError):
  292. return True
  293. return call.done()
  294. def add_done_callback(self, callback: DoneCallbackType) -> None:
  295. if not self._interceptors_task.done():
  296. self._pending_add_done_callbacks.append(callback)
  297. return
  298. try:
  299. call = self._interceptors_task.result()
  300. except (AioRpcError, asyncio.CancelledError):
  301. callback(self)
  302. return
  303. if call.done():
  304. callback(self)
  305. else:
  306. callback = functools.partial(self._wrap_add_done_callback, callback)
  307. call.add_done_callback(callback)
  308. def time_remaining(self) -> Optional[float]:
  309. raise NotImplementedError()
  310. async def initial_metadata(self) -> Optional[Metadata]:
  311. try:
  312. call = await self._interceptors_task
  313. except AioRpcError as err:
  314. return err.initial_metadata()
  315. except asyncio.CancelledError:
  316. return None
  317. return await call.initial_metadata()
  318. async def trailing_metadata(self) -> Optional[Metadata]:
  319. try:
  320. call = await self._interceptors_task
  321. except AioRpcError as err:
  322. return err.trailing_metadata()
  323. except asyncio.CancelledError:
  324. return None
  325. return await call.trailing_metadata()
  326. async def code(self) -> grpc.StatusCode:
  327. try:
  328. call = await self._interceptors_task
  329. except AioRpcError as err:
  330. return err.code()
  331. except asyncio.CancelledError:
  332. return grpc.StatusCode.CANCELLED
  333. return await call.code()
  334. async def details(self) -> str:
  335. try:
  336. call = await self._interceptors_task
  337. except AioRpcError as err:
  338. return err.details()
  339. except asyncio.CancelledError:
  340. return _LOCAL_CANCELLATION_DETAILS
  341. return await call.details()
  342. async def debug_error_string(self) -> Optional[str]:
  343. try:
  344. call = await self._interceptors_task
  345. except AioRpcError as err:
  346. return err.debug_error_string()
  347. except asyncio.CancelledError:
  348. return ''
  349. return await call.debug_error_string()
  350. async def wait_for_connection(self) -> None:
  351. call = await self._interceptors_task
  352. return await call.wait_for_connection()
  353. class _InterceptedUnaryResponseMixin:
  354. def __await__(self):
  355. call = yield from self._interceptors_task.__await__()
  356. response = yield from call.__await__()
  357. return response
  358. class _InterceptedStreamResponseMixin:
  359. _response_aiter: Optional[AsyncIterable[ResponseType]]
  360. def _init_stream_response_mixin(self) -> None:
  361. # Is initalized later, otherwise if the iterator is not finnally
  362. # consumed a logging warning is emmited by Asyncio.
  363. self._response_aiter = None
  364. async def _wait_for_interceptor_task_response_iterator(
  365. self) -> ResponseType:
  366. call = await self._interceptors_task
  367. async for response in call:
  368. yield response
  369. def __aiter__(self) -> AsyncIterable[ResponseType]:
  370. if self._response_aiter is None:
  371. self._response_aiter = self._wait_for_interceptor_task_response_iterator(
  372. )
  373. return self._response_aiter
  374. async def read(self) -> ResponseType:
  375. if self._response_aiter is None:
  376. self._response_aiter = self._wait_for_interceptor_task_response_iterator(
  377. )
  378. return await self._response_aiter.asend(None)
  379. class _InterceptedStreamRequestMixin:
  380. _write_to_iterator_async_gen: Optional[AsyncIterable[RequestType]]
  381. _write_to_iterator_queue: Optional[asyncio.Queue]
  382. _status_code_task: Optional[asyncio.Task]
  383. _FINISH_ITERATOR_SENTINEL = object()
  384. def _init_stream_request_mixin(
  385. self, request_iterator: Optional[RequestIterableType]
  386. ) -> RequestIterableType:
  387. if request_iterator is None:
  388. # We provide our own request iterator which is a proxy
  389. # of the futures writes that will be done by the caller.
  390. self._write_to_iterator_queue = asyncio.Queue(maxsize=1)
  391. self._write_to_iterator_async_gen = self._proxy_writes_as_request_iterator(
  392. )
  393. self._status_code_task = None
  394. request_iterator = self._write_to_iterator_async_gen
  395. else:
  396. self._write_to_iterator_queue = None
  397. return request_iterator
  398. async def _proxy_writes_as_request_iterator(self):
  399. await self._interceptors_task
  400. while True:
  401. value = await self._write_to_iterator_queue.get()
  402. if value is _InterceptedStreamRequestMixin._FINISH_ITERATOR_SENTINEL:
  403. break
  404. yield value
  405. async def _write_to_iterator_queue_interruptible(self, request: RequestType,
  406. call: InterceptedCall):
  407. # Write the specified 'request' to the request iterator queue using the
  408. # specified 'call' to allow for interruption of the write in the case
  409. # of abrupt termination of the call.
  410. if self._status_code_task is None:
  411. self._status_code_task = self._loop.create_task(call.code())
  412. await asyncio.wait(
  413. (self._loop.create_task(self._write_to_iterator_queue.put(request)),
  414. self._status_code_task),
  415. return_when=asyncio.FIRST_COMPLETED)
  416. async def write(self, request: RequestType) -> None:
  417. # If no queue was created it means that requests
  418. # should be expected through an iterators provided
  419. # by the caller.
  420. if self._write_to_iterator_queue is None:
  421. raise cygrpc.UsageError(_API_STYLE_ERROR)
  422. try:
  423. call = await self._interceptors_task
  424. except (asyncio.CancelledError, AioRpcError):
  425. raise asyncio.InvalidStateError(_RPC_ALREADY_FINISHED_DETAILS)
  426. if call.done():
  427. raise asyncio.InvalidStateError(_RPC_ALREADY_FINISHED_DETAILS)
  428. elif call._done_writing_flag:
  429. raise asyncio.InvalidStateError(_RPC_HALF_CLOSED_DETAILS)
  430. await self._write_to_iterator_queue_interruptible(request, call)
  431. if call.done():
  432. raise asyncio.InvalidStateError(_RPC_ALREADY_FINISHED_DETAILS)
  433. async def done_writing(self) -> None:
  434. """Signal peer that client is done writing.
  435. This method is idempotent.
  436. """
  437. # If no queue was created it means that requests
  438. # should be expected through an iterators provided
  439. # by the caller.
  440. if self._write_to_iterator_queue is None:
  441. raise cygrpc.UsageError(_API_STYLE_ERROR)
  442. try:
  443. call = await self._interceptors_task
  444. except asyncio.CancelledError:
  445. raise asyncio.InvalidStateError(_RPC_ALREADY_FINISHED_DETAILS)
  446. await self._write_to_iterator_queue_interruptible(
  447. _InterceptedStreamRequestMixin._FINISH_ITERATOR_SENTINEL, call)
  448. class InterceptedUnaryUnaryCall(_InterceptedUnaryResponseMixin, InterceptedCall,
  449. _base_call.UnaryUnaryCall):
  450. """Used for running a `UnaryUnaryCall` wrapped by interceptors.
  451. For the `__await__` method is it is proxied to the intercepted call only when
  452. the interceptor task is finished.
  453. """
  454. _loop: asyncio.AbstractEventLoop
  455. _channel: cygrpc.AioChannel
  456. # pylint: disable=too-many-arguments
  457. def __init__(self, interceptors: Sequence[UnaryUnaryClientInterceptor],
  458. request: RequestType, timeout: Optional[float],
  459. metadata: Metadata,
  460. credentials: Optional[grpc.CallCredentials],
  461. wait_for_ready: Optional[bool], channel: cygrpc.AioChannel,
  462. method: bytes, request_serializer: SerializingFunction,
  463. response_deserializer: DeserializingFunction,
  464. loop: asyncio.AbstractEventLoop) -> None:
  465. self._loop = loop
  466. self._channel = channel
  467. interceptors_task = loop.create_task(
  468. self._invoke(interceptors, method, timeout, metadata, credentials,
  469. wait_for_ready, request, request_serializer,
  470. response_deserializer))
  471. super().__init__(interceptors_task)
  472. # pylint: disable=too-many-arguments
  473. async def _invoke(
  474. self, interceptors: Sequence[UnaryUnaryClientInterceptor],
  475. method: bytes, timeout: Optional[float],
  476. metadata: Optional[Metadata],
  477. credentials: Optional[grpc.CallCredentials],
  478. wait_for_ready: Optional[bool], request: RequestType,
  479. request_serializer: SerializingFunction,
  480. response_deserializer: DeserializingFunction) -> UnaryUnaryCall:
  481. """Run the RPC call wrapped in interceptors"""
  482. async def _run_interceptor(
  483. interceptors: Iterator[UnaryUnaryClientInterceptor],
  484. client_call_details: ClientCallDetails,
  485. request: RequestType) -> _base_call.UnaryUnaryCall:
  486. interceptor = next(interceptors, None)
  487. if interceptor:
  488. continuation = functools.partial(_run_interceptor, interceptors)
  489. call_or_response = await interceptor.intercept_unary_unary(
  490. continuation, client_call_details, request)
  491. if isinstance(call_or_response, _base_call.UnaryUnaryCall):
  492. return call_or_response
  493. else:
  494. return UnaryUnaryCallResponse(call_or_response)
  495. else:
  496. return UnaryUnaryCall(
  497. request, _timeout_to_deadline(client_call_details.timeout),
  498. client_call_details.metadata,
  499. client_call_details.credentials,
  500. client_call_details.wait_for_ready, self._channel,
  501. client_call_details.method, request_serializer,
  502. response_deserializer, self._loop)
  503. client_call_details = ClientCallDetails(method, timeout, metadata,
  504. credentials, wait_for_ready)
  505. return await _run_interceptor(iter(interceptors), client_call_details,
  506. request)
  507. def time_remaining(self) -> Optional[float]:
  508. raise NotImplementedError()
  509. class InterceptedUnaryStreamCall(_InterceptedStreamResponseMixin,
  510. InterceptedCall, _base_call.UnaryStreamCall):
  511. """Used for running a `UnaryStreamCall` wrapped by interceptors."""
  512. _loop: asyncio.AbstractEventLoop
  513. _channel: cygrpc.AioChannel
  514. _last_returned_call_from_interceptors = Optional[_base_call.UnaryStreamCall]
  515. # pylint: disable=too-many-arguments
  516. def __init__(self, interceptors: Sequence[UnaryStreamClientInterceptor],
  517. request: RequestType, timeout: Optional[float],
  518. metadata: Metadata,
  519. credentials: Optional[grpc.CallCredentials],
  520. wait_for_ready: Optional[bool], channel: cygrpc.AioChannel,
  521. method: bytes, request_serializer: SerializingFunction,
  522. response_deserializer: DeserializingFunction,
  523. loop: asyncio.AbstractEventLoop) -> None:
  524. self._loop = loop
  525. self._channel = channel
  526. self._init_stream_response_mixin()
  527. self._last_returned_call_from_interceptors = None
  528. interceptors_task = loop.create_task(
  529. self._invoke(interceptors, method, timeout, metadata, credentials,
  530. wait_for_ready, request, request_serializer,
  531. response_deserializer))
  532. super().__init__(interceptors_task)
  533. # pylint: disable=too-many-arguments
  534. async def _invoke(
  535. self, interceptors: Sequence[UnaryUnaryClientInterceptor],
  536. method: bytes, timeout: Optional[float],
  537. metadata: Optional[Metadata],
  538. credentials: Optional[grpc.CallCredentials],
  539. wait_for_ready: Optional[bool], request: RequestType,
  540. request_serializer: SerializingFunction,
  541. response_deserializer: DeserializingFunction) -> UnaryStreamCall:
  542. """Run the RPC call wrapped in interceptors"""
  543. async def _run_interceptor(
  544. interceptors: Iterator[UnaryStreamClientInterceptor],
  545. client_call_details: ClientCallDetails,
  546. request: RequestType,
  547. ) -> _base_call.UnaryUnaryCall:
  548. interceptor = next(interceptors, None)
  549. if interceptor:
  550. continuation = functools.partial(_run_interceptor, interceptors)
  551. call_or_response_iterator = await interceptor.intercept_unary_stream(
  552. continuation, client_call_details, request)
  553. if isinstance(call_or_response_iterator,
  554. _base_call.UnaryStreamCall):
  555. self._last_returned_call_from_interceptors = call_or_response_iterator
  556. else:
  557. self._last_returned_call_from_interceptors = UnaryStreamCallResponseIterator(
  558. self._last_returned_call_from_interceptors,
  559. call_or_response_iterator)
  560. return self._last_returned_call_from_interceptors
  561. else:
  562. self._last_returned_call_from_interceptors = UnaryStreamCall(
  563. request, _timeout_to_deadline(client_call_details.timeout),
  564. client_call_details.metadata,
  565. client_call_details.credentials,
  566. client_call_details.wait_for_ready, self._channel,
  567. client_call_details.method, request_serializer,
  568. response_deserializer, self._loop)
  569. return self._last_returned_call_from_interceptors
  570. client_call_details = ClientCallDetails(method, timeout, metadata,
  571. credentials, wait_for_ready)
  572. return await _run_interceptor(iter(interceptors), client_call_details,
  573. request)
  574. def time_remaining(self) -> Optional[float]:
  575. raise NotImplementedError()
  576. class InterceptedStreamUnaryCall(_InterceptedUnaryResponseMixin,
  577. _InterceptedStreamRequestMixin,
  578. InterceptedCall, _base_call.StreamUnaryCall):
  579. """Used for running a `StreamUnaryCall` wrapped by interceptors.
  580. For the `__await__` method is it is proxied to the intercepted call only when
  581. the interceptor task is finished.
  582. """
  583. _loop: asyncio.AbstractEventLoop
  584. _channel: cygrpc.AioChannel
  585. # pylint: disable=too-many-arguments
  586. def __init__(self, interceptors: Sequence[StreamUnaryClientInterceptor],
  587. request_iterator: Optional[RequestIterableType],
  588. timeout: Optional[float], metadata: Metadata,
  589. credentials: Optional[grpc.CallCredentials],
  590. wait_for_ready: Optional[bool], channel: cygrpc.AioChannel,
  591. method: bytes, request_serializer: SerializingFunction,
  592. response_deserializer: DeserializingFunction,
  593. loop: asyncio.AbstractEventLoop) -> None:
  594. self._loop = loop
  595. self._channel = channel
  596. request_iterator = self._init_stream_request_mixin(request_iterator)
  597. interceptors_task = loop.create_task(
  598. self._invoke(interceptors, method, timeout, metadata, credentials,
  599. wait_for_ready, request_iterator, request_serializer,
  600. response_deserializer))
  601. super().__init__(interceptors_task)
  602. # pylint: disable=too-many-arguments
  603. async def _invoke(
  604. self, interceptors: Sequence[StreamUnaryClientInterceptor],
  605. method: bytes, timeout: Optional[float],
  606. metadata: Optional[Metadata],
  607. credentials: Optional[grpc.CallCredentials],
  608. wait_for_ready: Optional[bool],
  609. request_iterator: RequestIterableType,
  610. request_serializer: SerializingFunction,
  611. response_deserializer: DeserializingFunction) -> StreamUnaryCall:
  612. """Run the RPC call wrapped in interceptors"""
  613. async def _run_interceptor(
  614. interceptors: Iterator[UnaryUnaryClientInterceptor],
  615. client_call_details: ClientCallDetails,
  616. request_iterator: RequestIterableType
  617. ) -> _base_call.StreamUnaryCall:
  618. interceptor = next(interceptors, None)
  619. if interceptor:
  620. continuation = functools.partial(_run_interceptor, interceptors)
  621. return await interceptor.intercept_stream_unary(
  622. continuation, client_call_details, request_iterator)
  623. else:
  624. return StreamUnaryCall(
  625. request_iterator,
  626. _timeout_to_deadline(client_call_details.timeout),
  627. client_call_details.metadata,
  628. client_call_details.credentials,
  629. client_call_details.wait_for_ready, self._channel,
  630. client_call_details.method, request_serializer,
  631. response_deserializer, self._loop)
  632. client_call_details = ClientCallDetails(method, timeout, metadata,
  633. credentials, wait_for_ready)
  634. return await _run_interceptor(iter(interceptors), client_call_details,
  635. request_iterator)
  636. def time_remaining(self) -> Optional[float]:
  637. raise NotImplementedError()
  638. class InterceptedStreamStreamCall(_InterceptedStreamResponseMixin,
  639. _InterceptedStreamRequestMixin,
  640. InterceptedCall, _base_call.StreamStreamCall):
  641. """Used for running a `StreamStreamCall` wrapped by interceptors."""
  642. _loop: asyncio.AbstractEventLoop
  643. _channel: cygrpc.AioChannel
  644. _last_returned_call_from_interceptors = Optional[_base_call.UnaryStreamCall]
  645. # pylint: disable=too-many-arguments
  646. def __init__(self, interceptors: Sequence[StreamStreamClientInterceptor],
  647. request_iterator: Optional[RequestIterableType],
  648. timeout: Optional[float], metadata: Metadata,
  649. credentials: Optional[grpc.CallCredentials],
  650. wait_for_ready: Optional[bool], channel: cygrpc.AioChannel,
  651. method: bytes, request_serializer: SerializingFunction,
  652. response_deserializer: DeserializingFunction,
  653. loop: asyncio.AbstractEventLoop) -> None:
  654. self._loop = loop
  655. self._channel = channel
  656. self._init_stream_response_mixin()
  657. request_iterator = self._init_stream_request_mixin(request_iterator)
  658. self._last_returned_call_from_interceptors = None
  659. interceptors_task = loop.create_task(
  660. self._invoke(interceptors, method, timeout, metadata, credentials,
  661. wait_for_ready, request_iterator, request_serializer,
  662. response_deserializer))
  663. super().__init__(interceptors_task)
  664. # pylint: disable=too-many-arguments
  665. async def _invoke(
  666. self, interceptors: Sequence[StreamStreamClientInterceptor],
  667. method: bytes, timeout: Optional[float],
  668. metadata: Optional[Metadata],
  669. credentials: Optional[grpc.CallCredentials],
  670. wait_for_ready: Optional[bool],
  671. request_iterator: RequestIterableType,
  672. request_serializer: SerializingFunction,
  673. response_deserializer: DeserializingFunction) -> StreamStreamCall:
  674. """Run the RPC call wrapped in interceptors"""
  675. async def _run_interceptor(
  676. interceptors: Iterator[StreamStreamClientInterceptor],
  677. client_call_details: ClientCallDetails,
  678. request_iterator: RequestIterableType
  679. ) -> _base_call.StreamStreamCall:
  680. interceptor = next(interceptors, None)
  681. if interceptor:
  682. continuation = functools.partial(_run_interceptor, interceptors)
  683. call_or_response_iterator = await interceptor.intercept_stream_stream(
  684. continuation, client_call_details, request_iterator)
  685. if isinstance(call_or_response_iterator,
  686. _base_call.StreamStreamCall):
  687. self._last_returned_call_from_interceptors = call_or_response_iterator
  688. else:
  689. self._last_returned_call_from_interceptors = StreamStreamCallResponseIterator(
  690. self._last_returned_call_from_interceptors,
  691. call_or_response_iterator)
  692. return self._last_returned_call_from_interceptors
  693. else:
  694. self._last_returned_call_from_interceptors = StreamStreamCall(
  695. request_iterator,
  696. _timeout_to_deadline(client_call_details.timeout),
  697. client_call_details.metadata,
  698. client_call_details.credentials,
  699. client_call_details.wait_for_ready, self._channel,
  700. client_call_details.method, request_serializer,
  701. response_deserializer, self._loop)
  702. return self._last_returned_call_from_interceptors
  703. client_call_details = ClientCallDetails(method, timeout, metadata,
  704. credentials, wait_for_ready)
  705. return await _run_interceptor(iter(interceptors), client_call_details,
  706. request_iterator)
  707. def time_remaining(self) -> Optional[float]:
  708. raise NotImplementedError()
  709. class UnaryUnaryCallResponse(_base_call.UnaryUnaryCall):
  710. """Final UnaryUnaryCall class finished with a response."""
  711. _response: ResponseType
  712. def __init__(self, response: ResponseType) -> None:
  713. self._response = response
  714. def cancel(self) -> bool:
  715. return False
  716. def cancelled(self) -> bool:
  717. return False
  718. def done(self) -> bool:
  719. return True
  720. def add_done_callback(self, unused_callback) -> None:
  721. raise NotImplementedError()
  722. def time_remaining(self) -> Optional[float]:
  723. raise NotImplementedError()
  724. async def initial_metadata(self) -> Optional[Metadata]:
  725. return None
  726. async def trailing_metadata(self) -> Optional[Metadata]:
  727. return None
  728. async def code(self) -> grpc.StatusCode:
  729. return grpc.StatusCode.OK
  730. async def details(self) -> str:
  731. return ''
  732. async def debug_error_string(self) -> Optional[str]:
  733. return None
  734. def __await__(self):
  735. if False: # pylint: disable=using-constant-test
  736. # This code path is never used, but a yield statement is needed
  737. # for telling the interpreter that __await__ is a generator.
  738. yield None
  739. return self._response
  740. async def wait_for_connection(self) -> None:
  741. pass
  742. class _StreamCallResponseIterator:
  743. _call: Union[_base_call.UnaryStreamCall, _base_call.StreamStreamCall]
  744. _response_iterator: AsyncIterable[ResponseType]
  745. def __init__(self, call: Union[_base_call.UnaryStreamCall,
  746. _base_call.StreamStreamCall],
  747. response_iterator: AsyncIterable[ResponseType]) -> None:
  748. self._response_iterator = response_iterator
  749. self._call = call
  750. def cancel(self) -> bool:
  751. return self._call.cancel()
  752. def cancelled(self) -> bool:
  753. return self._call.cancelled()
  754. def done(self) -> bool:
  755. return self._call.done()
  756. def add_done_callback(self, callback) -> None:
  757. self._call.add_done_callback(callback)
  758. def time_remaining(self) -> Optional[float]:
  759. return self._call.time_remaining()
  760. async def initial_metadata(self) -> Optional[Metadata]:
  761. return await self._call.initial_metadata()
  762. async def trailing_metadata(self) -> Optional[Metadata]:
  763. return await self._call.trailing_metadata()
  764. async def code(self) -> grpc.StatusCode:
  765. return await self._call.code()
  766. async def details(self) -> str:
  767. return await self._call.details()
  768. async def debug_error_string(self) -> Optional[str]:
  769. return await self._call.debug_error_string()
  770. def __aiter__(self):
  771. return self._response_iterator.__aiter__()
  772. async def wait_for_connection(self) -> None:
  773. return await self._call.wait_for_connection()
  774. class UnaryStreamCallResponseIterator(_StreamCallResponseIterator,
  775. _base_call.UnaryStreamCall):
  776. """UnaryStreamCall class wich uses an alternative response iterator."""
  777. async def read(self) -> ResponseType:
  778. # Behind the scenes everyting goes through the
  779. # async iterator. So this path should not be reached.
  780. raise NotImplementedError()
  781. class StreamStreamCallResponseIterator(_StreamCallResponseIterator,
  782. _base_call.StreamStreamCall):
  783. """StreamStreamCall class wich uses an alternative response iterator."""
  784. async def read(self) -> ResponseType:
  785. # Behind the scenes everyting goes through the
  786. # async iterator. So this path should not be reached.
  787. raise NotImplementedError()
  788. async def write(self, request: RequestType) -> None:
  789. # Behind the scenes everyting goes through the
  790. # async iterator provided by the InterceptedStreamStreamCall.
  791. # So this path should not be reached.
  792. raise NotImplementedError()
  793. async def done_writing(self) -> None:
  794. # Behind the scenes everyting goes through the
  795. # async iterator provided by the InterceptedStreamStreamCall.
  796. # So this path should not be reached.
  797. raise NotImplementedError()
  798. @property
  799. def _done_writing_flag(self) -> bool:
  800. return self._call._done_writing_flag