_channel.py 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. # Copyright 2016 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. """Invocation-side implementation of gRPC Python."""
  15. import copy
  16. import functools
  17. import logging
  18. import os
  19. import sys
  20. import threading
  21. import time
  22. import grpc
  23. from grpc import _common
  24. from grpc import _compression
  25. from grpc import _grpcio_metadata
  26. from grpc._cython import cygrpc
  27. import grpc.experimental
  28. _LOGGER = logging.getLogger(__name__)
  29. _USER_AGENT = 'grpc-python/{}'.format(_grpcio_metadata.__version__)
  30. _EMPTY_FLAGS = 0
  31. # NOTE(rbellevi): No guarantees are given about the maintenance of this
  32. # environment variable.
  33. _DEFAULT_SINGLE_THREADED_UNARY_STREAM = os.getenv(
  34. "GRPC_SINGLE_THREADED_UNARY_STREAM") is not None
  35. _UNARY_UNARY_INITIAL_DUE = (
  36. cygrpc.OperationType.send_initial_metadata,
  37. cygrpc.OperationType.send_message,
  38. cygrpc.OperationType.send_close_from_client,
  39. cygrpc.OperationType.receive_initial_metadata,
  40. cygrpc.OperationType.receive_message,
  41. cygrpc.OperationType.receive_status_on_client,
  42. )
  43. _UNARY_STREAM_INITIAL_DUE = (
  44. cygrpc.OperationType.send_initial_metadata,
  45. cygrpc.OperationType.send_message,
  46. cygrpc.OperationType.send_close_from_client,
  47. cygrpc.OperationType.receive_initial_metadata,
  48. cygrpc.OperationType.receive_status_on_client,
  49. )
  50. _STREAM_UNARY_INITIAL_DUE = (
  51. cygrpc.OperationType.send_initial_metadata,
  52. cygrpc.OperationType.receive_initial_metadata,
  53. cygrpc.OperationType.receive_message,
  54. cygrpc.OperationType.receive_status_on_client,
  55. )
  56. _STREAM_STREAM_INITIAL_DUE = (
  57. cygrpc.OperationType.send_initial_metadata,
  58. cygrpc.OperationType.receive_initial_metadata,
  59. cygrpc.OperationType.receive_status_on_client,
  60. )
  61. _CHANNEL_SUBSCRIPTION_CALLBACK_ERROR_LOG_MESSAGE = (
  62. 'Exception calling channel subscription callback!')
  63. _OK_RENDEZVOUS_REPR_FORMAT = ('<{} of RPC that terminated with:\n'
  64. '\tstatus = {}\n'
  65. '\tdetails = "{}"\n'
  66. '>')
  67. _NON_OK_RENDEZVOUS_REPR_FORMAT = ('<{} of RPC that terminated with:\n'
  68. '\tstatus = {}\n'
  69. '\tdetails = "{}"\n'
  70. '\tdebug_error_string = "{}"\n'
  71. '>')
  72. def _deadline(timeout):
  73. return None if timeout is None else time.time() + timeout
  74. def _unknown_code_details(unknown_cygrpc_code, details):
  75. return 'Server sent unknown code {} and details "{}"'.format(
  76. unknown_cygrpc_code, details)
  77. class _RPCState(object):
  78. def __init__(self, due, initial_metadata, trailing_metadata, code, details):
  79. # `condition` guards all members of _RPCState. `notify_all` is called on
  80. # `condition` when the state of the RPC has changed.
  81. self.condition = threading.Condition()
  82. # The cygrpc.OperationType objects representing events due from the RPC's
  83. # completion queue. If an operation is in `due`, it is guaranteed that
  84. # `operate()` has been called on a corresponding operation. But the
  85. # converse is not true. That is, in the case of failed `operate()`
  86. # calls, there may briefly be events in `due` that do not correspond to
  87. # operations submitted to Core.
  88. self.due = set(due)
  89. self.initial_metadata = initial_metadata
  90. self.response = None
  91. self.trailing_metadata = trailing_metadata
  92. self.code = code
  93. self.details = details
  94. self.debug_error_string = None
  95. # The semantics of grpc.Future.cancel and grpc.Future.cancelled are
  96. # slightly wonky, so they have to be tracked separately from the rest of the
  97. # result of the RPC. This field tracks whether cancellation was requested
  98. # prior to termination of the RPC.
  99. self.cancelled = False
  100. self.callbacks = []
  101. self.fork_epoch = cygrpc.get_fork_epoch()
  102. def reset_postfork_child(self):
  103. self.condition = threading.Condition()
  104. def _abort(state, code, details):
  105. if state.code is None:
  106. state.code = code
  107. state.details = details
  108. if state.initial_metadata is None:
  109. state.initial_metadata = ()
  110. state.trailing_metadata = ()
  111. def _handle_event(event, state, response_deserializer):
  112. callbacks = []
  113. for batch_operation in event.batch_operations:
  114. operation_type = batch_operation.type()
  115. state.due.remove(operation_type)
  116. if operation_type == cygrpc.OperationType.receive_initial_metadata:
  117. state.initial_metadata = batch_operation.initial_metadata()
  118. elif operation_type == cygrpc.OperationType.receive_message:
  119. serialized_response = batch_operation.message()
  120. if serialized_response is not None:
  121. response = _common.deserialize(serialized_response,
  122. response_deserializer)
  123. if response is None:
  124. details = 'Exception deserializing response!'
  125. _abort(state, grpc.StatusCode.INTERNAL, details)
  126. else:
  127. state.response = response
  128. elif operation_type == cygrpc.OperationType.receive_status_on_client:
  129. state.trailing_metadata = batch_operation.trailing_metadata()
  130. if state.code is None:
  131. code = _common.CYGRPC_STATUS_CODE_TO_STATUS_CODE.get(
  132. batch_operation.code())
  133. if code is None:
  134. state.code = grpc.StatusCode.UNKNOWN
  135. state.details = _unknown_code_details(
  136. code, batch_operation.details())
  137. else:
  138. state.code = code
  139. state.details = batch_operation.details()
  140. state.debug_error_string = batch_operation.error_string()
  141. callbacks.extend(state.callbacks)
  142. state.callbacks = None
  143. return callbacks
  144. def _event_handler(state, response_deserializer):
  145. def handle_event(event):
  146. with state.condition:
  147. callbacks = _handle_event(event, state, response_deserializer)
  148. state.condition.notify_all()
  149. done = not state.due
  150. for callback in callbacks:
  151. try:
  152. callback()
  153. except Exception as e: # pylint: disable=broad-except
  154. # NOTE(rbellevi): We suppress but log errors here so as not to
  155. # kill the channel spin thread.
  156. logging.error('Exception in callback %s: %s',
  157. repr(callback.func), repr(e))
  158. return done and state.fork_epoch >= cygrpc.get_fork_epoch()
  159. return handle_event
  160. #pylint: disable=too-many-statements
  161. def _consume_request_iterator(request_iterator, state, call, request_serializer,
  162. event_handler):
  163. """Consume a request iterator supplied by the user."""
  164. def consume_request_iterator(): # pylint: disable=too-many-branches
  165. # Iterate over the request iterator until it is exhausted or an error
  166. # condition is encountered.
  167. while True:
  168. return_from_user_request_generator_invoked = False
  169. try:
  170. # The thread may die in user-code. Do not block fork for this.
  171. cygrpc.enter_user_request_generator()
  172. request = next(request_iterator)
  173. except StopIteration:
  174. break
  175. except Exception: # pylint: disable=broad-except
  176. cygrpc.return_from_user_request_generator()
  177. return_from_user_request_generator_invoked = True
  178. code = grpc.StatusCode.UNKNOWN
  179. details = 'Exception iterating requests!'
  180. _LOGGER.exception(details)
  181. call.cancel(_common.STATUS_CODE_TO_CYGRPC_STATUS_CODE[code],
  182. details)
  183. _abort(state, code, details)
  184. return
  185. finally:
  186. if not return_from_user_request_generator_invoked:
  187. cygrpc.return_from_user_request_generator()
  188. serialized_request = _common.serialize(request, request_serializer)
  189. with state.condition:
  190. if state.code is None and not state.cancelled:
  191. if serialized_request is None:
  192. code = grpc.StatusCode.INTERNAL
  193. details = 'Exception serializing request!'
  194. call.cancel(
  195. _common.STATUS_CODE_TO_CYGRPC_STATUS_CODE[code],
  196. details)
  197. _abort(state, code, details)
  198. return
  199. else:
  200. state.due.add(cygrpc.OperationType.send_message)
  201. operations = (cygrpc.SendMessageOperation(
  202. serialized_request, _EMPTY_FLAGS),)
  203. operating = call.operate(operations, event_handler)
  204. if not operating:
  205. state.due.remove(cygrpc.OperationType.send_message)
  206. return
  207. def _done():
  208. return (state.code is not None or
  209. cygrpc.OperationType.send_message
  210. not in state.due)
  211. _common.wait(state.condition.wait,
  212. _done,
  213. spin_cb=functools.partial(
  214. cygrpc.block_if_fork_in_progress,
  215. state))
  216. if state.code is not None:
  217. return
  218. else:
  219. return
  220. with state.condition:
  221. if state.code is None:
  222. state.due.add(cygrpc.OperationType.send_close_from_client)
  223. operations = (
  224. cygrpc.SendCloseFromClientOperation(_EMPTY_FLAGS),)
  225. operating = call.operate(operations, event_handler)
  226. if not operating:
  227. state.due.remove(
  228. cygrpc.OperationType.send_close_from_client)
  229. consumption_thread = cygrpc.ForkManagedThread(
  230. target=consume_request_iterator)
  231. consumption_thread.setDaemon(True)
  232. consumption_thread.start()
  233. def _rpc_state_string(class_name, rpc_state):
  234. """Calculates error string for RPC."""
  235. with rpc_state.condition:
  236. if rpc_state.code is None:
  237. return '<{} object>'.format(class_name)
  238. elif rpc_state.code is grpc.StatusCode.OK:
  239. return _OK_RENDEZVOUS_REPR_FORMAT.format(class_name, rpc_state.code,
  240. rpc_state.details)
  241. else:
  242. return _NON_OK_RENDEZVOUS_REPR_FORMAT.format(
  243. class_name, rpc_state.code, rpc_state.details,
  244. rpc_state.debug_error_string)
  245. class _InactiveRpcError(grpc.RpcError, grpc.Call, grpc.Future):
  246. """An RPC error not tied to the execution of a particular RPC.
  247. The RPC represented by the state object must not be in-progress or
  248. cancelled.
  249. Attributes:
  250. _state: An instance of _RPCState.
  251. """
  252. def __init__(self, state):
  253. with state.condition:
  254. self._state = _RPCState((), copy.deepcopy(state.initial_metadata),
  255. copy.deepcopy(state.trailing_metadata),
  256. state.code, copy.deepcopy(state.details))
  257. self._state.response = copy.copy(state.response)
  258. self._state.debug_error_string = copy.copy(state.debug_error_string)
  259. def initial_metadata(self):
  260. return self._state.initial_metadata
  261. def trailing_metadata(self):
  262. return self._state.trailing_metadata
  263. def code(self):
  264. return self._state.code
  265. def details(self):
  266. return _common.decode(self._state.details)
  267. def debug_error_string(self):
  268. return _common.decode(self._state.debug_error_string)
  269. def _repr(self):
  270. return _rpc_state_string(self.__class__.__name__, self._state)
  271. def __repr__(self):
  272. return self._repr()
  273. def __str__(self):
  274. return self._repr()
  275. def cancel(self):
  276. """See grpc.Future.cancel."""
  277. return False
  278. def cancelled(self):
  279. """See grpc.Future.cancelled."""
  280. return False
  281. def running(self):
  282. """See grpc.Future.running."""
  283. return False
  284. def done(self):
  285. """See grpc.Future.done."""
  286. return True
  287. def result(self, timeout=None): # pylint: disable=unused-argument
  288. """See grpc.Future.result."""
  289. raise self
  290. def exception(self, timeout=None): # pylint: disable=unused-argument
  291. """See grpc.Future.exception."""
  292. return self
  293. def traceback(self, timeout=None): # pylint: disable=unused-argument
  294. """See grpc.Future.traceback."""
  295. try:
  296. raise self
  297. except grpc.RpcError:
  298. return sys.exc_info()[2]
  299. def add_done_callback(self, fn, timeout=None): # pylint: disable=unused-argument
  300. """See grpc.Future.add_done_callback."""
  301. fn(self)
  302. class _Rendezvous(grpc.RpcError, grpc.RpcContext):
  303. """An RPC iterator.
  304. Attributes:
  305. _state: An instance of _RPCState.
  306. _call: An instance of SegregatedCall or IntegratedCall.
  307. In either case, the _call object is expected to have operate, cancel,
  308. and next_event methods.
  309. _response_deserializer: A callable taking bytes and return a Python
  310. object.
  311. _deadline: A float representing the deadline of the RPC in seconds. Or
  312. possibly None, to represent an RPC with no deadline at all.
  313. """
  314. def __init__(self, state, call, response_deserializer, deadline):
  315. super(_Rendezvous, self).__init__()
  316. self._state = state
  317. self._call = call
  318. self._response_deserializer = response_deserializer
  319. self._deadline = deadline
  320. def is_active(self):
  321. """See grpc.RpcContext.is_active"""
  322. with self._state.condition:
  323. return self._state.code is None
  324. def time_remaining(self):
  325. """See grpc.RpcContext.time_remaining"""
  326. with self._state.condition:
  327. if self._deadline is None:
  328. return None
  329. else:
  330. return max(self._deadline - time.time(), 0)
  331. def cancel(self):
  332. """See grpc.RpcContext.cancel"""
  333. with self._state.condition:
  334. if self._state.code is None:
  335. code = grpc.StatusCode.CANCELLED
  336. details = 'Locally cancelled by application!'
  337. self._call.cancel(
  338. _common.STATUS_CODE_TO_CYGRPC_STATUS_CODE[code], details)
  339. self._state.cancelled = True
  340. _abort(self._state, code, details)
  341. self._state.condition.notify_all()
  342. return True
  343. else:
  344. return False
  345. def add_callback(self, callback):
  346. """See grpc.RpcContext.add_callback"""
  347. with self._state.condition:
  348. if self._state.callbacks is None:
  349. return False
  350. else:
  351. self._state.callbacks.append(callback)
  352. return True
  353. def __iter__(self):
  354. return self
  355. def next(self):
  356. return self._next()
  357. def __next__(self):
  358. return self._next()
  359. def _next(self):
  360. raise NotImplementedError()
  361. def debug_error_string(self):
  362. raise NotImplementedError()
  363. def _repr(self):
  364. return _rpc_state_string(self.__class__.__name__, self._state)
  365. def __repr__(self):
  366. return self._repr()
  367. def __str__(self):
  368. return self._repr()
  369. def __del__(self):
  370. with self._state.condition:
  371. if self._state.code is None:
  372. self._state.code = grpc.StatusCode.CANCELLED
  373. self._state.details = 'Cancelled upon garbage collection!'
  374. self._state.cancelled = True
  375. self._call.cancel(
  376. _common.STATUS_CODE_TO_CYGRPC_STATUS_CODE[self._state.code],
  377. self._state.details)
  378. self._state.condition.notify_all()
  379. class _SingleThreadedRendezvous(_Rendezvous, grpc.Call, grpc.Future): # pylint: disable=too-many-ancestors
  380. """An RPC iterator operating entirely on a single thread.
  381. The __next__ method of _SingleThreadedRendezvous does not depend on the
  382. existence of any other thread, including the "channel spin thread".
  383. However, this means that its interface is entirely synchronous. So this
  384. class cannot completely fulfill the grpc.Future interface. The result,
  385. exception, and traceback methods will never block and will instead raise
  386. an exception if calling the method would result in blocking.
  387. This means that these methods are safe to call from add_done_callback
  388. handlers.
  389. """
  390. def _is_complete(self):
  391. return self._state.code is not None
  392. def cancelled(self):
  393. with self._state.condition:
  394. return self._state.cancelled
  395. def running(self):
  396. with self._state.condition:
  397. return self._state.code is None
  398. def done(self):
  399. with self._state.condition:
  400. return self._state.code is not None
  401. def result(self, timeout=None):
  402. """Returns the result of the computation or raises its exception.
  403. This method will never block. Instead, it will raise an exception
  404. if calling this method would otherwise result in blocking.
  405. Since this method will never block, any `timeout` argument passed will
  406. be ignored.
  407. """
  408. del timeout
  409. with self._state.condition:
  410. if not self._is_complete():
  411. raise grpc.experimental.UsageError(
  412. "_SingleThreadedRendezvous only supports result() when the RPC is complete."
  413. )
  414. if self._state.code is grpc.StatusCode.OK:
  415. return self._state.response
  416. elif self._state.cancelled:
  417. raise grpc.FutureCancelledError()
  418. else:
  419. raise self
  420. def exception(self, timeout=None):
  421. """Return the exception raised by the computation.
  422. This method will never block. Instead, it will raise an exception
  423. if calling this method would otherwise result in blocking.
  424. Since this method will never block, any `timeout` argument passed will
  425. be ignored.
  426. """
  427. del timeout
  428. with self._state.condition:
  429. if not self._is_complete():
  430. raise grpc.experimental.UsageError(
  431. "_SingleThreadedRendezvous only supports exception() when the RPC is complete."
  432. )
  433. if self._state.code is grpc.StatusCode.OK:
  434. return None
  435. elif self._state.cancelled:
  436. raise grpc.FutureCancelledError()
  437. else:
  438. return self
  439. def traceback(self, timeout=None):
  440. """Access the traceback of the exception raised by the computation.
  441. This method will never block. Instead, it will raise an exception
  442. if calling this method would otherwise result in blocking.
  443. Since this method will never block, any `timeout` argument passed will
  444. be ignored.
  445. """
  446. del timeout
  447. with self._state.condition:
  448. if not self._is_complete():
  449. raise grpc.experimental.UsageError(
  450. "_SingleThreadedRendezvous only supports traceback() when the RPC is complete."
  451. )
  452. if self._state.code is grpc.StatusCode.OK:
  453. return None
  454. elif self._state.cancelled:
  455. raise grpc.FutureCancelledError()
  456. else:
  457. try:
  458. raise self
  459. except grpc.RpcError:
  460. return sys.exc_info()[2]
  461. def add_done_callback(self, fn):
  462. with self._state.condition:
  463. if self._state.code is None:
  464. self._state.callbacks.append(functools.partial(fn, self))
  465. return
  466. fn(self)
  467. def initial_metadata(self):
  468. """See grpc.Call.initial_metadata"""
  469. with self._state.condition:
  470. # NOTE(gnossen): Based on our initial call batch, we are guaranteed
  471. # to receive initial metadata before any messages.
  472. while self._state.initial_metadata is None:
  473. self._consume_next_event()
  474. return self._state.initial_metadata
  475. def trailing_metadata(self):
  476. """See grpc.Call.trailing_metadata"""
  477. with self._state.condition:
  478. if self._state.trailing_metadata is None:
  479. raise grpc.experimental.UsageError(
  480. "Cannot get trailing metadata until RPC is completed.")
  481. return self._state.trailing_metadata
  482. def code(self):
  483. """See grpc.Call.code"""
  484. with self._state.condition:
  485. if self._state.code is None:
  486. raise grpc.experimental.UsageError(
  487. "Cannot get code until RPC is completed.")
  488. return self._state.code
  489. def details(self):
  490. """See grpc.Call.details"""
  491. with self._state.condition:
  492. if self._state.details is None:
  493. raise grpc.experimental.UsageError(
  494. "Cannot get details until RPC is completed.")
  495. return _common.decode(self._state.details)
  496. def _consume_next_event(self):
  497. event = self._call.next_event()
  498. with self._state.condition:
  499. callbacks = _handle_event(event, self._state,
  500. self._response_deserializer)
  501. for callback in callbacks:
  502. # NOTE(gnossen): We intentionally allow exceptions to bubble up
  503. # to the user when running on a single thread.
  504. callback()
  505. return event
  506. def _next_response(self):
  507. while True:
  508. self._consume_next_event()
  509. with self._state.condition:
  510. if self._state.response is not None:
  511. response = self._state.response
  512. self._state.response = None
  513. return response
  514. elif cygrpc.OperationType.receive_message not in self._state.due:
  515. if self._state.code is grpc.StatusCode.OK:
  516. raise StopIteration()
  517. elif self._state.code is not None:
  518. raise self
  519. def _next(self):
  520. with self._state.condition:
  521. if self._state.code is None:
  522. # We tentatively add the operation as expected and remove
  523. # it if the enqueue operation fails. This allows us to guarantee that
  524. # if an event has been submitted to the core completion queue,
  525. # it is in `due`. If we waited until after a successful
  526. # enqueue operation then a signal could interrupt this
  527. # thread between the enqueue operation and the addition of the
  528. # operation to `due`. This would cause an exception on the
  529. # channel spin thread when the operation completes and no
  530. # corresponding operation would be present in state.due.
  531. # Note that, since `condition` is held through this block, there is
  532. # no data race on `due`.
  533. self._state.due.add(cygrpc.OperationType.receive_message)
  534. operating = self._call.operate(
  535. (cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS),), None)
  536. if not operating:
  537. self._state.due.remove(cygrpc.OperationType.receive_message)
  538. elif self._state.code is grpc.StatusCode.OK:
  539. raise StopIteration()
  540. else:
  541. raise self
  542. return self._next_response()
  543. def debug_error_string(self):
  544. with self._state.condition:
  545. if self._state.debug_error_string is None:
  546. raise grpc.experimental.UsageError(
  547. "Cannot get debug error string until RPC is completed.")
  548. return _common.decode(self._state.debug_error_string)
  549. class _MultiThreadedRendezvous(_Rendezvous, grpc.Call, grpc.Future): # pylint: disable=too-many-ancestors
  550. """An RPC iterator that depends on a channel spin thread.
  551. This iterator relies upon a per-channel thread running in the background,
  552. dequeueing events from the completion queue, and notifying threads waiting
  553. on the threading.Condition object in the _RPCState object.
  554. This extra thread allows _MultiThreadedRendezvous to fulfill the grpc.Future interface
  555. and to mediate a bidirection streaming RPC.
  556. """
  557. def initial_metadata(self):
  558. """See grpc.Call.initial_metadata"""
  559. with self._state.condition:
  560. def _done():
  561. return self._state.initial_metadata is not None
  562. _common.wait(self._state.condition.wait, _done)
  563. return self._state.initial_metadata
  564. def trailing_metadata(self):
  565. """See grpc.Call.trailing_metadata"""
  566. with self._state.condition:
  567. def _done():
  568. return self._state.trailing_metadata is not None
  569. _common.wait(self._state.condition.wait, _done)
  570. return self._state.trailing_metadata
  571. def code(self):
  572. """See grpc.Call.code"""
  573. with self._state.condition:
  574. def _done():
  575. return self._state.code is not None
  576. _common.wait(self._state.condition.wait, _done)
  577. return self._state.code
  578. def details(self):
  579. """See grpc.Call.details"""
  580. with self._state.condition:
  581. def _done():
  582. return self._state.details is not None
  583. _common.wait(self._state.condition.wait, _done)
  584. return _common.decode(self._state.details)
  585. def debug_error_string(self):
  586. with self._state.condition:
  587. def _done():
  588. return self._state.debug_error_string is not None
  589. _common.wait(self._state.condition.wait, _done)
  590. return _common.decode(self._state.debug_error_string)
  591. def cancelled(self):
  592. with self._state.condition:
  593. return self._state.cancelled
  594. def running(self):
  595. with self._state.condition:
  596. return self._state.code is None
  597. def done(self):
  598. with self._state.condition:
  599. return self._state.code is not None
  600. def _is_complete(self):
  601. return self._state.code is not None
  602. def result(self, timeout=None):
  603. """Returns the result of the computation or raises its exception.
  604. See grpc.Future.result for the full API contract.
  605. """
  606. with self._state.condition:
  607. timed_out = _common.wait(self._state.condition.wait,
  608. self._is_complete,
  609. timeout=timeout)
  610. if timed_out:
  611. raise grpc.FutureTimeoutError()
  612. else:
  613. if self._state.code is grpc.StatusCode.OK:
  614. return self._state.response
  615. elif self._state.cancelled:
  616. raise grpc.FutureCancelledError()
  617. else:
  618. raise self
  619. def exception(self, timeout=None):
  620. """Return the exception raised by the computation.
  621. See grpc.Future.exception for the full API contract.
  622. """
  623. with self._state.condition:
  624. timed_out = _common.wait(self._state.condition.wait,
  625. self._is_complete,
  626. timeout=timeout)
  627. if timed_out:
  628. raise grpc.FutureTimeoutError()
  629. else:
  630. if self._state.code is grpc.StatusCode.OK:
  631. return None
  632. elif self._state.cancelled:
  633. raise grpc.FutureCancelledError()
  634. else:
  635. return self
  636. def traceback(self, timeout=None):
  637. """Access the traceback of the exception raised by the computation.
  638. See grpc.future.traceback for the full API contract.
  639. """
  640. with self._state.condition:
  641. timed_out = _common.wait(self._state.condition.wait,
  642. self._is_complete,
  643. timeout=timeout)
  644. if timed_out:
  645. raise grpc.FutureTimeoutError()
  646. else:
  647. if self._state.code is grpc.StatusCode.OK:
  648. return None
  649. elif self._state.cancelled:
  650. raise grpc.FutureCancelledError()
  651. else:
  652. try:
  653. raise self
  654. except grpc.RpcError:
  655. return sys.exc_info()[2]
  656. def add_done_callback(self, fn):
  657. with self._state.condition:
  658. if self._state.code is None:
  659. self._state.callbacks.append(functools.partial(fn, self))
  660. return
  661. fn(self)
  662. def _next(self):
  663. with self._state.condition:
  664. if self._state.code is None:
  665. event_handler = _event_handler(self._state,
  666. self._response_deserializer)
  667. self._state.due.add(cygrpc.OperationType.receive_message)
  668. operating = self._call.operate(
  669. (cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS),),
  670. event_handler)
  671. if not operating:
  672. self._state.due.remove(cygrpc.OperationType.receive_message)
  673. elif self._state.code is grpc.StatusCode.OK:
  674. raise StopIteration()
  675. else:
  676. raise self
  677. def _response_ready():
  678. return (self._state.response is not None or
  679. (cygrpc.OperationType.receive_message
  680. not in self._state.due and
  681. self._state.code is not None))
  682. _common.wait(self._state.condition.wait, _response_ready)
  683. if self._state.response is not None:
  684. response = self._state.response
  685. self._state.response = None
  686. return response
  687. elif cygrpc.OperationType.receive_message not in self._state.due:
  688. if self._state.code is grpc.StatusCode.OK:
  689. raise StopIteration()
  690. elif self._state.code is not None:
  691. raise self
  692. def _start_unary_request(request, timeout, request_serializer):
  693. deadline = _deadline(timeout)
  694. serialized_request = _common.serialize(request, request_serializer)
  695. if serialized_request is None:
  696. state = _RPCState((), (), (), grpc.StatusCode.INTERNAL,
  697. 'Exception serializing request!')
  698. error = _InactiveRpcError(state)
  699. return deadline, None, error
  700. else:
  701. return deadline, serialized_request, None
  702. def _end_unary_response_blocking(state, call, with_call, deadline):
  703. if state.code is grpc.StatusCode.OK:
  704. if with_call:
  705. rendezvous = _MultiThreadedRendezvous(state, call, None, deadline)
  706. return state.response, rendezvous
  707. else:
  708. return state.response
  709. else:
  710. raise _InactiveRpcError(state)
  711. def _stream_unary_invocation_operationses(metadata, initial_metadata_flags):
  712. return (
  713. (
  714. cygrpc.SendInitialMetadataOperation(metadata,
  715. initial_metadata_flags),
  716. cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS),
  717. cygrpc.ReceiveStatusOnClientOperation(_EMPTY_FLAGS),
  718. ),
  719. (cygrpc.ReceiveInitialMetadataOperation(_EMPTY_FLAGS),),
  720. )
  721. def _stream_unary_invocation_operationses_and_tags(metadata,
  722. initial_metadata_flags):
  723. return tuple((
  724. operations,
  725. None,
  726. ) for operations in _stream_unary_invocation_operationses(
  727. metadata, initial_metadata_flags))
  728. def _determine_deadline(user_deadline):
  729. parent_deadline = cygrpc.get_deadline_from_context()
  730. if parent_deadline is None and user_deadline is None:
  731. return None
  732. elif parent_deadline is not None and user_deadline is None:
  733. return parent_deadline
  734. elif user_deadline is not None and parent_deadline is None:
  735. return user_deadline
  736. else:
  737. return min(parent_deadline, user_deadline)
  738. class _UnaryUnaryMultiCallable(grpc.UnaryUnaryMultiCallable):
  739. # pylint: disable=too-many-arguments
  740. def __init__(self, channel, managed_call, method, request_serializer,
  741. response_deserializer):
  742. self._channel = channel
  743. self._managed_call = managed_call
  744. self._method = method
  745. self._request_serializer = request_serializer
  746. self._response_deserializer = response_deserializer
  747. self._context = cygrpc.build_census_context()
  748. def _prepare(self, request, timeout, metadata, wait_for_ready, compression):
  749. deadline, serialized_request, rendezvous = _start_unary_request(
  750. request, timeout, self._request_serializer)
  751. initial_metadata_flags = _InitialMetadataFlags().with_wait_for_ready(
  752. wait_for_ready)
  753. augmented_metadata = _compression.augment_metadata(
  754. metadata, compression)
  755. if serialized_request is None:
  756. return None, None, None, rendezvous
  757. else:
  758. state = _RPCState(_UNARY_UNARY_INITIAL_DUE, None, None, None, None)
  759. operations = (
  760. cygrpc.SendInitialMetadataOperation(augmented_metadata,
  761. initial_metadata_flags),
  762. cygrpc.SendMessageOperation(serialized_request, _EMPTY_FLAGS),
  763. cygrpc.SendCloseFromClientOperation(_EMPTY_FLAGS),
  764. cygrpc.ReceiveInitialMetadataOperation(_EMPTY_FLAGS),
  765. cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS),
  766. cygrpc.ReceiveStatusOnClientOperation(_EMPTY_FLAGS),
  767. )
  768. return state, operations, deadline, None
  769. def _blocking(self, request, timeout, metadata, credentials, wait_for_ready,
  770. compression):
  771. state, operations, deadline, rendezvous = self._prepare(
  772. request, timeout, metadata, wait_for_ready, compression)
  773. if state is None:
  774. raise rendezvous # pylint: disable-msg=raising-bad-type
  775. else:
  776. call = self._channel.segregated_call(
  777. cygrpc.PropagationConstants.GRPC_PROPAGATE_DEFAULTS,
  778. self._method, None, _determine_deadline(deadline), metadata,
  779. None if credentials is None else credentials._credentials, ((
  780. operations,
  781. None,
  782. ),), self._context)
  783. event = call.next_event()
  784. _handle_event(event, state, self._response_deserializer)
  785. return state, call
  786. def __call__(self,
  787. request,
  788. timeout=None,
  789. metadata=None,
  790. credentials=None,
  791. wait_for_ready=None,
  792. compression=None):
  793. state, call, = self._blocking(request, timeout, metadata, credentials,
  794. wait_for_ready, compression)
  795. return _end_unary_response_blocking(state, call, False, None)
  796. def with_call(self,
  797. request,
  798. timeout=None,
  799. metadata=None,
  800. credentials=None,
  801. wait_for_ready=None,
  802. compression=None):
  803. state, call, = self._blocking(request, timeout, metadata, credentials,
  804. wait_for_ready, compression)
  805. return _end_unary_response_blocking(state, call, True, None)
  806. def future(self,
  807. request,
  808. timeout=None,
  809. metadata=None,
  810. credentials=None,
  811. wait_for_ready=None,
  812. compression=None):
  813. state, operations, deadline, rendezvous = self._prepare(
  814. request, timeout, metadata, wait_for_ready, compression)
  815. if state is None:
  816. raise rendezvous # pylint: disable-msg=raising-bad-type
  817. else:
  818. event_handler = _event_handler(state, self._response_deserializer)
  819. call = self._managed_call(
  820. cygrpc.PropagationConstants.GRPC_PROPAGATE_DEFAULTS,
  821. self._method, None, deadline, metadata,
  822. None if credentials is None else credentials._credentials,
  823. (operations,), event_handler, self._context)
  824. return _MultiThreadedRendezvous(state, call,
  825. self._response_deserializer,
  826. deadline)
  827. class _SingleThreadedUnaryStreamMultiCallable(grpc.UnaryStreamMultiCallable):
  828. # pylint: disable=too-many-arguments
  829. def __init__(self, channel, method, request_serializer,
  830. response_deserializer):
  831. self._channel = channel
  832. self._method = method
  833. self._request_serializer = request_serializer
  834. self._response_deserializer = response_deserializer
  835. self._context = cygrpc.build_census_context()
  836. def __call__( # pylint: disable=too-many-locals
  837. self,
  838. request,
  839. timeout=None,
  840. metadata=None,
  841. credentials=None,
  842. wait_for_ready=None,
  843. compression=None):
  844. deadline = _deadline(timeout)
  845. serialized_request = _common.serialize(request,
  846. self._request_serializer)
  847. if serialized_request is None:
  848. state = _RPCState((), (), (), grpc.StatusCode.INTERNAL,
  849. 'Exception serializing request!')
  850. raise _InactiveRpcError(state)
  851. state = _RPCState(_UNARY_STREAM_INITIAL_DUE, None, None, None, None)
  852. call_credentials = None if credentials is None else credentials._credentials
  853. initial_metadata_flags = _InitialMetadataFlags().with_wait_for_ready(
  854. wait_for_ready)
  855. augmented_metadata = _compression.augment_metadata(
  856. metadata, compression)
  857. operations = (
  858. (cygrpc.SendInitialMetadataOperation(augmented_metadata,
  859. initial_metadata_flags),
  860. cygrpc.SendMessageOperation(serialized_request, _EMPTY_FLAGS),
  861. cygrpc.SendCloseFromClientOperation(_EMPTY_FLAGS)),
  862. (cygrpc.ReceiveStatusOnClientOperation(_EMPTY_FLAGS),),
  863. (cygrpc.ReceiveInitialMetadataOperation(_EMPTY_FLAGS),),
  864. )
  865. operations_and_tags = tuple((ops, None) for ops in operations)
  866. call = self._channel.segregated_call(
  867. cygrpc.PropagationConstants.GRPC_PROPAGATE_DEFAULTS, self._method,
  868. None, _determine_deadline(deadline), metadata, call_credentials,
  869. operations_and_tags, self._context)
  870. return _SingleThreadedRendezvous(state, call,
  871. self._response_deserializer, deadline)
  872. class _UnaryStreamMultiCallable(grpc.UnaryStreamMultiCallable):
  873. # pylint: disable=too-many-arguments
  874. def __init__(self, channel, managed_call, method, request_serializer,
  875. response_deserializer):
  876. self._channel = channel
  877. self._managed_call = managed_call
  878. self._method = method
  879. self._request_serializer = request_serializer
  880. self._response_deserializer = response_deserializer
  881. self._context = cygrpc.build_census_context()
  882. def __call__( # pylint: disable=too-many-locals
  883. self,
  884. request,
  885. timeout=None,
  886. metadata=None,
  887. credentials=None,
  888. wait_for_ready=None,
  889. compression=None):
  890. deadline, serialized_request, rendezvous = _start_unary_request(
  891. request, timeout, self._request_serializer)
  892. initial_metadata_flags = _InitialMetadataFlags().with_wait_for_ready(
  893. wait_for_ready)
  894. if serialized_request is None:
  895. raise rendezvous # pylint: disable-msg=raising-bad-type
  896. else:
  897. augmented_metadata = _compression.augment_metadata(
  898. metadata, compression)
  899. state = _RPCState(_UNARY_STREAM_INITIAL_DUE, None, None, None, None)
  900. operationses = (
  901. (
  902. cygrpc.SendInitialMetadataOperation(augmented_metadata,
  903. initial_metadata_flags),
  904. cygrpc.SendMessageOperation(serialized_request,
  905. _EMPTY_FLAGS),
  906. cygrpc.SendCloseFromClientOperation(_EMPTY_FLAGS),
  907. cygrpc.ReceiveStatusOnClientOperation(_EMPTY_FLAGS),
  908. ),
  909. (cygrpc.ReceiveInitialMetadataOperation(_EMPTY_FLAGS),),
  910. )
  911. call = self._managed_call(
  912. cygrpc.PropagationConstants.GRPC_PROPAGATE_DEFAULTS,
  913. self._method, None, _determine_deadline(deadline), metadata,
  914. None if credentials is None else credentials._credentials,
  915. operationses, _event_handler(state,
  916. self._response_deserializer),
  917. self._context)
  918. return _MultiThreadedRendezvous(state, call,
  919. self._response_deserializer,
  920. deadline)
  921. class _StreamUnaryMultiCallable(grpc.StreamUnaryMultiCallable):
  922. # pylint: disable=too-many-arguments
  923. def __init__(self, channel, managed_call, method, request_serializer,
  924. response_deserializer):
  925. self._channel = channel
  926. self._managed_call = managed_call
  927. self._method = method
  928. self._request_serializer = request_serializer
  929. self._response_deserializer = response_deserializer
  930. self._context = cygrpc.build_census_context()
  931. def _blocking(self, request_iterator, timeout, metadata, credentials,
  932. wait_for_ready, compression):
  933. deadline = _deadline(timeout)
  934. state = _RPCState(_STREAM_UNARY_INITIAL_DUE, None, None, None, None)
  935. initial_metadata_flags = _InitialMetadataFlags().with_wait_for_ready(
  936. wait_for_ready)
  937. augmented_metadata = _compression.augment_metadata(
  938. metadata, compression)
  939. call = self._channel.segregated_call(
  940. cygrpc.PropagationConstants.GRPC_PROPAGATE_DEFAULTS, self._method,
  941. None, _determine_deadline(deadline), augmented_metadata,
  942. None if credentials is None else credentials._credentials,
  943. _stream_unary_invocation_operationses_and_tags(
  944. augmented_metadata, initial_metadata_flags), self._context)
  945. _consume_request_iterator(request_iterator, state, call,
  946. self._request_serializer, None)
  947. while True:
  948. event = call.next_event()
  949. with state.condition:
  950. _handle_event(event, state, self._response_deserializer)
  951. state.condition.notify_all()
  952. if not state.due:
  953. break
  954. return state, call
  955. def __call__(self,
  956. request_iterator,
  957. timeout=None,
  958. metadata=None,
  959. credentials=None,
  960. wait_for_ready=None,
  961. compression=None):
  962. state, call, = self._blocking(request_iterator, timeout, metadata,
  963. credentials, wait_for_ready, compression)
  964. return _end_unary_response_blocking(state, call, False, None)
  965. def with_call(self,
  966. request_iterator,
  967. timeout=None,
  968. metadata=None,
  969. credentials=None,
  970. wait_for_ready=None,
  971. compression=None):
  972. state, call, = self._blocking(request_iterator, timeout, metadata,
  973. credentials, wait_for_ready, compression)
  974. return _end_unary_response_blocking(state, call, True, None)
  975. def future(self,
  976. request_iterator,
  977. timeout=None,
  978. metadata=None,
  979. credentials=None,
  980. wait_for_ready=None,
  981. compression=None):
  982. deadline = _deadline(timeout)
  983. state = _RPCState(_STREAM_UNARY_INITIAL_DUE, None, None, None, None)
  984. event_handler = _event_handler(state, self._response_deserializer)
  985. initial_metadata_flags = _InitialMetadataFlags().with_wait_for_ready(
  986. wait_for_ready)
  987. augmented_metadata = _compression.augment_metadata(
  988. metadata, compression)
  989. call = self._managed_call(
  990. cygrpc.PropagationConstants.GRPC_PROPAGATE_DEFAULTS, self._method,
  991. None, deadline, augmented_metadata,
  992. None if credentials is None else credentials._credentials,
  993. _stream_unary_invocation_operationses(metadata,
  994. initial_metadata_flags),
  995. event_handler, self._context)
  996. _consume_request_iterator(request_iterator, state, call,
  997. self._request_serializer, event_handler)
  998. return _MultiThreadedRendezvous(state, call,
  999. self._response_deserializer, deadline)
  1000. class _StreamStreamMultiCallable(grpc.StreamStreamMultiCallable):
  1001. # pylint: disable=too-many-arguments
  1002. def __init__(self, channel, managed_call, method, request_serializer,
  1003. response_deserializer):
  1004. self._channel = channel
  1005. self._managed_call = managed_call
  1006. self._method = method
  1007. self._request_serializer = request_serializer
  1008. self._response_deserializer = response_deserializer
  1009. self._context = cygrpc.build_census_context()
  1010. def __call__(self,
  1011. request_iterator,
  1012. timeout=None,
  1013. metadata=None,
  1014. credentials=None,
  1015. wait_for_ready=None,
  1016. compression=None):
  1017. deadline = _deadline(timeout)
  1018. state = _RPCState(_STREAM_STREAM_INITIAL_DUE, None, None, None, None)
  1019. initial_metadata_flags = _InitialMetadataFlags().with_wait_for_ready(
  1020. wait_for_ready)
  1021. augmented_metadata = _compression.augment_metadata(
  1022. metadata, compression)
  1023. operationses = (
  1024. (
  1025. cygrpc.SendInitialMetadataOperation(augmented_metadata,
  1026. initial_metadata_flags),
  1027. cygrpc.ReceiveStatusOnClientOperation(_EMPTY_FLAGS),
  1028. ),
  1029. (cygrpc.ReceiveInitialMetadataOperation(_EMPTY_FLAGS),),
  1030. )
  1031. event_handler = _event_handler(state, self._response_deserializer)
  1032. call = self._managed_call(
  1033. cygrpc.PropagationConstants.GRPC_PROPAGATE_DEFAULTS, self._method,
  1034. None, _determine_deadline(deadline), augmented_metadata,
  1035. None if credentials is None else credentials._credentials,
  1036. operationses, event_handler, self._context)
  1037. _consume_request_iterator(request_iterator, state, call,
  1038. self._request_serializer, event_handler)
  1039. return _MultiThreadedRendezvous(state, call,
  1040. self._response_deserializer, deadline)
  1041. class _InitialMetadataFlags(int):
  1042. """Stores immutable initial metadata flags"""
  1043. def __new__(cls, value=_EMPTY_FLAGS):
  1044. value &= cygrpc.InitialMetadataFlags.used_mask
  1045. return super(_InitialMetadataFlags, cls).__new__(cls, value)
  1046. def with_wait_for_ready(self, wait_for_ready):
  1047. if wait_for_ready is not None:
  1048. if wait_for_ready:
  1049. return self.__class__(self | cygrpc.InitialMetadataFlags.wait_for_ready | \
  1050. cygrpc.InitialMetadataFlags.wait_for_ready_explicitly_set)
  1051. elif not wait_for_ready:
  1052. return self.__class__(self & ~cygrpc.InitialMetadataFlags.wait_for_ready | \
  1053. cygrpc.InitialMetadataFlags.wait_for_ready_explicitly_set)
  1054. return self
  1055. class _ChannelCallState(object):
  1056. def __init__(self, channel):
  1057. self.lock = threading.Lock()
  1058. self.channel = channel
  1059. self.managed_calls = 0
  1060. self.threading = False
  1061. def reset_postfork_child(self):
  1062. self.managed_calls = 0
  1063. def __del__(self):
  1064. try:
  1065. self.channel.close(cygrpc.StatusCode.cancelled,
  1066. 'Channel deallocated!')
  1067. except (TypeError, AttributeError):
  1068. pass
  1069. def _run_channel_spin_thread(state):
  1070. def channel_spin():
  1071. while True:
  1072. cygrpc.block_if_fork_in_progress(state)
  1073. event = state.channel.next_call_event()
  1074. if event.completion_type == cygrpc.CompletionType.queue_timeout:
  1075. continue
  1076. call_completed = event.tag(event)
  1077. if call_completed:
  1078. with state.lock:
  1079. state.managed_calls -= 1
  1080. if state.managed_calls == 0:
  1081. return
  1082. channel_spin_thread = cygrpc.ForkManagedThread(target=channel_spin)
  1083. channel_spin_thread.setDaemon(True)
  1084. channel_spin_thread.start()
  1085. def _channel_managed_call_management(state):
  1086. # pylint: disable=too-many-arguments
  1087. def create(flags, method, host, deadline, metadata, credentials,
  1088. operationses, event_handler, context):
  1089. """Creates a cygrpc.IntegratedCall.
  1090. Args:
  1091. flags: An integer bitfield of call flags.
  1092. method: The RPC method.
  1093. host: A host string for the created call.
  1094. deadline: A float to be the deadline of the created call or None if
  1095. the call is to have an infinite deadline.
  1096. metadata: The metadata for the call or None.
  1097. credentials: A cygrpc.CallCredentials or None.
  1098. operationses: An iterable of iterables of cygrpc.Operations to be
  1099. started on the call.
  1100. event_handler: A behavior to call to handle the events resultant from
  1101. the operations on the call.
  1102. context: Context object for distributed tracing.
  1103. Returns:
  1104. A cygrpc.IntegratedCall with which to conduct an RPC.
  1105. """
  1106. operationses_and_tags = tuple((
  1107. operations,
  1108. event_handler,
  1109. ) for operations in operationses)
  1110. with state.lock:
  1111. call = state.channel.integrated_call(flags, method, host, deadline,
  1112. metadata, credentials,
  1113. operationses_and_tags, context)
  1114. if state.managed_calls == 0:
  1115. state.managed_calls = 1
  1116. _run_channel_spin_thread(state)
  1117. else:
  1118. state.managed_calls += 1
  1119. return call
  1120. return create
  1121. class _ChannelConnectivityState(object):
  1122. def __init__(self, channel):
  1123. self.lock = threading.RLock()
  1124. self.channel = channel
  1125. self.polling = False
  1126. self.connectivity = None
  1127. self.try_to_connect = False
  1128. self.callbacks_and_connectivities = []
  1129. self.delivering = False
  1130. def reset_postfork_child(self):
  1131. self.polling = False
  1132. self.connectivity = None
  1133. self.try_to_connect = False
  1134. self.callbacks_and_connectivities = []
  1135. self.delivering = False
  1136. def _deliveries(state):
  1137. callbacks_needing_update = []
  1138. for callback_and_connectivity in state.callbacks_and_connectivities:
  1139. callback, callback_connectivity, = callback_and_connectivity
  1140. if callback_connectivity is not state.connectivity:
  1141. callbacks_needing_update.append(callback)
  1142. callback_and_connectivity[1] = state.connectivity
  1143. return callbacks_needing_update
  1144. def _deliver(state, initial_connectivity, initial_callbacks):
  1145. connectivity = initial_connectivity
  1146. callbacks = initial_callbacks
  1147. while True:
  1148. for callback in callbacks:
  1149. cygrpc.block_if_fork_in_progress(state)
  1150. try:
  1151. callback(connectivity)
  1152. except Exception: # pylint: disable=broad-except
  1153. _LOGGER.exception(
  1154. _CHANNEL_SUBSCRIPTION_CALLBACK_ERROR_LOG_MESSAGE)
  1155. with state.lock:
  1156. callbacks = _deliveries(state)
  1157. if callbacks:
  1158. connectivity = state.connectivity
  1159. else:
  1160. state.delivering = False
  1161. return
  1162. def _spawn_delivery(state, callbacks):
  1163. delivering_thread = cygrpc.ForkManagedThread(target=_deliver,
  1164. args=(
  1165. state,
  1166. state.connectivity,
  1167. callbacks,
  1168. ))
  1169. delivering_thread.setDaemon(True)
  1170. delivering_thread.start()
  1171. state.delivering = True
  1172. # NOTE(https://github.com/grpc/grpc/issues/3064): We'd rather not poll.
  1173. def _poll_connectivity(state, channel, initial_try_to_connect):
  1174. try_to_connect = initial_try_to_connect
  1175. connectivity = channel.check_connectivity_state(try_to_connect)
  1176. with state.lock:
  1177. state.connectivity = (
  1178. _common.
  1179. CYGRPC_CONNECTIVITY_STATE_TO_CHANNEL_CONNECTIVITY[connectivity])
  1180. callbacks = tuple(
  1181. callback for callback, unused_but_known_to_be_none_connectivity in
  1182. state.callbacks_and_connectivities)
  1183. for callback_and_connectivity in state.callbacks_and_connectivities:
  1184. callback_and_connectivity[1] = state.connectivity
  1185. if callbacks:
  1186. _spawn_delivery(state, callbacks)
  1187. while True:
  1188. event = channel.watch_connectivity_state(connectivity,
  1189. time.time() + 0.2)
  1190. cygrpc.block_if_fork_in_progress(state)
  1191. with state.lock:
  1192. if not state.callbacks_and_connectivities and not state.try_to_connect:
  1193. state.polling = False
  1194. state.connectivity = None
  1195. break
  1196. try_to_connect = state.try_to_connect
  1197. state.try_to_connect = False
  1198. if event.success or try_to_connect:
  1199. connectivity = channel.check_connectivity_state(try_to_connect)
  1200. with state.lock:
  1201. state.connectivity = (
  1202. _common.CYGRPC_CONNECTIVITY_STATE_TO_CHANNEL_CONNECTIVITY[
  1203. connectivity])
  1204. if not state.delivering:
  1205. callbacks = _deliveries(state)
  1206. if callbacks:
  1207. _spawn_delivery(state, callbacks)
  1208. def _subscribe(state, callback, try_to_connect):
  1209. with state.lock:
  1210. if not state.callbacks_and_connectivities and not state.polling:
  1211. polling_thread = cygrpc.ForkManagedThread(
  1212. target=_poll_connectivity,
  1213. args=(state, state.channel, bool(try_to_connect)))
  1214. polling_thread.setDaemon(True)
  1215. polling_thread.start()
  1216. state.polling = True
  1217. state.callbacks_and_connectivities.append([callback, None])
  1218. elif not state.delivering and state.connectivity is not None:
  1219. _spawn_delivery(state, (callback,))
  1220. state.try_to_connect |= bool(try_to_connect)
  1221. state.callbacks_and_connectivities.append(
  1222. [callback, state.connectivity])
  1223. else:
  1224. state.try_to_connect |= bool(try_to_connect)
  1225. state.callbacks_and_connectivities.append([callback, None])
  1226. def _unsubscribe(state, callback):
  1227. with state.lock:
  1228. for index, (subscribed_callback, unused_connectivity) in enumerate(
  1229. state.callbacks_and_connectivities):
  1230. if callback == subscribed_callback:
  1231. state.callbacks_and_connectivities.pop(index)
  1232. break
  1233. def _augment_options(base_options, compression):
  1234. compression_option = _compression.create_channel_option(compression)
  1235. return tuple(base_options) + compression_option + ((
  1236. cygrpc.ChannelArgKey.primary_user_agent_string,
  1237. _USER_AGENT,
  1238. ),)
  1239. def _separate_channel_options(options):
  1240. """Separates core channel options from Python channel options."""
  1241. core_options = []
  1242. python_options = []
  1243. for pair in options:
  1244. if pair[0] == grpc.experimental.ChannelOptions.SingleThreadedUnaryStream:
  1245. python_options.append(pair)
  1246. else:
  1247. core_options.append(pair)
  1248. return python_options, core_options
  1249. class Channel(grpc.Channel):
  1250. """A cygrpc.Channel-backed implementation of grpc.Channel."""
  1251. def __init__(self, target, options, credentials, compression):
  1252. """Constructor.
  1253. Args:
  1254. target: The target to which to connect.
  1255. options: Configuration options for the channel.
  1256. credentials: A cygrpc.ChannelCredentials or None.
  1257. compression: An optional value indicating the compression method to be
  1258. used over the lifetime of the channel.
  1259. """
  1260. python_options, core_options = _separate_channel_options(options)
  1261. self._single_threaded_unary_stream = _DEFAULT_SINGLE_THREADED_UNARY_STREAM
  1262. self._process_python_options(python_options)
  1263. self._channel = cygrpc.Channel(
  1264. _common.encode(target), _augment_options(core_options, compression),
  1265. credentials)
  1266. self._call_state = _ChannelCallState(self._channel)
  1267. self._connectivity_state = _ChannelConnectivityState(self._channel)
  1268. cygrpc.fork_register_channel(self)
  1269. if cygrpc.g_gevent_activated:
  1270. cygrpc.gevent_increment_channel_count()
  1271. def _process_python_options(self, python_options):
  1272. """Sets channel attributes according to python-only channel options."""
  1273. for pair in python_options:
  1274. if pair[0] == grpc.experimental.ChannelOptions.SingleThreadedUnaryStream:
  1275. self._single_threaded_unary_stream = True
  1276. def subscribe(self, callback, try_to_connect=None):
  1277. _subscribe(self._connectivity_state, callback, try_to_connect)
  1278. def unsubscribe(self, callback):
  1279. _unsubscribe(self._connectivity_state, callback)
  1280. def unary_unary(self,
  1281. method,
  1282. request_serializer=None,
  1283. response_deserializer=None):
  1284. return _UnaryUnaryMultiCallable(
  1285. self._channel, _channel_managed_call_management(self._call_state),
  1286. _common.encode(method), request_serializer, response_deserializer)
  1287. def unary_stream(self,
  1288. method,
  1289. request_serializer=None,
  1290. response_deserializer=None):
  1291. # NOTE(rbellevi): Benchmarks have shown that running a unary-stream RPC
  1292. # on a single Python thread results in an appreciable speed-up. However,
  1293. # due to slight differences in capability, the multi-threaded variant
  1294. # remains the default.
  1295. if self._single_threaded_unary_stream:
  1296. return _SingleThreadedUnaryStreamMultiCallable(
  1297. self._channel, _common.encode(method), request_serializer,
  1298. response_deserializer)
  1299. else:
  1300. return _UnaryStreamMultiCallable(
  1301. self._channel,
  1302. _channel_managed_call_management(self._call_state),
  1303. _common.encode(method), request_serializer,
  1304. response_deserializer)
  1305. def stream_unary(self,
  1306. method,
  1307. request_serializer=None,
  1308. response_deserializer=None):
  1309. return _StreamUnaryMultiCallable(
  1310. self._channel, _channel_managed_call_management(self._call_state),
  1311. _common.encode(method), request_serializer, response_deserializer)
  1312. def stream_stream(self,
  1313. method,
  1314. request_serializer=None,
  1315. response_deserializer=None):
  1316. return _StreamStreamMultiCallable(
  1317. self._channel, _channel_managed_call_management(self._call_state),
  1318. _common.encode(method), request_serializer, response_deserializer)
  1319. def _unsubscribe_all(self):
  1320. state = self._connectivity_state
  1321. if state:
  1322. with state.lock:
  1323. del state.callbacks_and_connectivities[:]
  1324. def _close(self):
  1325. self._unsubscribe_all()
  1326. self._channel.close(cygrpc.StatusCode.cancelled, 'Channel closed!')
  1327. cygrpc.fork_unregister_channel(self)
  1328. if cygrpc.g_gevent_activated:
  1329. cygrpc.gevent_decrement_channel_count()
  1330. def _close_on_fork(self):
  1331. self._unsubscribe_all()
  1332. self._channel.close_on_fork(cygrpc.StatusCode.cancelled,
  1333. 'Channel closed due to fork')
  1334. def __enter__(self):
  1335. return self
  1336. def __exit__(self, exc_type, exc_val, exc_tb):
  1337. self._close()
  1338. return False
  1339. def close(self):
  1340. self._close()
  1341. def __del__(self):
  1342. # TODO(https://github.com/grpc/grpc/issues/12531): Several releases
  1343. # after 1.12 (1.16 or thereabouts?) add a "self._channel.close" call
  1344. # here (or more likely, call self._close() here). We don't do this today
  1345. # because many valid use cases today allow the channel to be deleted
  1346. # immediately after stubs are created. After a sufficient period of time
  1347. # has passed for all users to be trusted to freeze out to their channels
  1348. # for as long as they are in use and to close them after using them,
  1349. # then deletion of this grpc._channel.Channel instance can be made to
  1350. # effect closure of the underlying cygrpc.Channel instance.
  1351. try:
  1352. self._unsubscribe_all()
  1353. except: # pylint: disable=bare-except
  1354. # Exceptions in __del__ are ignored by Python anyway, but they can
  1355. # keep spamming logs. Just silence them.
  1356. pass