_interceptor.py 40 KB

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