
    Z3re                        d Z ddlZddlZddlZddlZddlZddlZddlZddlZddlm	Z	 ddlm
Z
 ddlmZ ddlmZ ddlZ ej                  e      Zdj%                  ej&                        ZdZ ej,                  d      duZej0                  j2                  ej0                  j4                  ej0                  j6                  ej0                  j8                  ej0                  j:                  ej0                  j<                  fZej0                  j2                  ej0                  j4                  ej0                  j6                  ej0                  j8                  ej0                  j<                  fZ ej0                  j2                  ej0                  j8                  ej0                  j:                  ej0                  j<                  fZ!ej0                  j2                  ej0                  j8                  ej0                  j<                  fZ"d	Z#d
Z$dZ%d Z&d Z' G d de(      Z)d Z*d Z+d Z,d Z-d Z. G d dej^                  ej`                  ejb                        Z2 G d dej^                  ejf                        Z4 G d de4ej`                  ejb                        Z5 G d de4ej`                  ejb                        Z6d Z7d Z8d Z9d  Z:d! Z; G d" d#ejx                        Z= G d$ d%ej|                        Z? G d& d'ej|                        Z@ G d( d)ej                        ZB G d* d+ej                        ZD G d, d-eE      ZF G d. d/e(      ZGd0 ZHd1 ZI G d2 d3e(      ZJd4 ZKd5 ZLd6 ZMd7 ZNd8 ZOd9 ZPd: ZQd; ZR G d< d=ej                        ZSy)>z.Invocation-side implementation of gRPC Python.    N)_common)_compression)_grpcio_metadata)cygrpczgrpc-python/{}!GRPC_SINGLE_THREADED_UNARY_STREAMz0Exception calling channel subscription callback!z?<{} of RPC that terminated with:
	status = {}
	details = "{}"
>zZ<{} of RPC that terminated with:
	status = {}
	details = "{}"
	debug_error_string = "{}"
>c                 8    | d S t        j                          | z   S N)timetimeouts    -lib/python3.12/site-packages/grpc/_channel.py	_deadliner   T   s    ?4=		g(==    c                 &    dj                  | |      S )Nz,Server sent unknown code {} and details "{}")format)unknown_cygrpc_codedetailss     r   _unknown_code_detailsr   X   s    9@@W& &r   c                       e Zd Zd Zd Zy)	_RPCStatec                     t        j                         | _        t        |      | _        || _        d | _        || _        || _        || _	        d | _
        d| _        g | _        t        j                         | _        y NF)	threading	Condition	conditionsetdueinitial_metadataresponsetrailing_metadatacoder   debug_error_string	cancelled	callbacksr   get_fork_epoch
fork_epoch)selfr   r   r    r!   r   s         r   __init__z_RPCState.__init___   sn     #,,. s8 0!2	"&  //1r   c                 6    t        j                         | _        y r	   )r   r   r   r'   s    r   reset_postfork_childz_RPCState.reset_postfork_childz   s    ",,.r   N__name__
__module____qualname__r(   r+    r   r   r   r   ]   s    26/r   r   c                 n    | j                   )|| _         || _        | j                  d| _        d| _        y y Nr0   )r!   r   r   r    )stater!   r   s      r   _abortr4   ~   s;    zz
!!)%'E""$ r   c                    g }| j                   D ]  }|j                         }|j                  j                  |       |t        j
                  j                  k(  r|j                         |_        b|t        j
                  j                  k(  r[|j                         }|t        j                  ||      }|(d}t        |t        j                  j                  |       ||_        |t        j
                  j"                  k(  s|j%                         |_        |j&                  t        j(                  j+                  |j'                               }	|	?t        j                  j,                  |_        t/        |	|j1                               |_        n1|	|_        |j1                         |_        |j3                         |_        |j7                  |j8                         d |_         |S )Nz!Exception deserializing response!)batch_operationstyper   remover   OperationTypereceive_initial_metadatar   receive_messagemessager   deserializer4   grpc
StatusCodeINTERNALr   receive_status_on_clientr    r!   !CYGRPC_STATUS_CODE_TO_STATUS_CODEgetUNKNOWNr   r   error_stringr"   extendr$   )
eventr3   response_deserializerr$   batch_operationoperation_typeserialized_responser   r   r!   s
             r   _handle_eventrL      s   I 11 #(--/		(V11JJJ%4%E%E%GE"v33CCC"1"9"9";"."../B/DF#AG5$//":":GD%-ENv33LLL&5&G&G&IE#zz!@@DD#((*,<!%!8!8EJ$9o557%9EM "&EJ$3$;$;$=EM/>/K/K/ME,U__-"EO;#< r   c                       fd}|S )Nc           	         j                   5  t        |       }j                   j                          j                   }d d d        D ]
  }	  |         xr! j                  t        j                         k\  S # 1 sw Y   =xY w# t        $ r=}t        j                  dt        |j                        t        |             Y d }~d }~ww xY w)NzException in callback %s: %s)r   rL   
notify_allr   	Exceptionloggingerrorreprfuncr&   r   r%   )rG   r$   donecallbackerH   r3   s        r   handle_eventz$_event_handler.<locals>.handle_event   s    __ 	!%eU4IJIOO&&(yy=D	! " 	<H<
	< C((F,A,A,CCC	! 	!  < <"8==147< <<s#   5A?B?B	C3CCr0   )r3   rH   rX   s   `` r   _event_handlerrY      s    D r   c                       fd}t        j                  |      }|j                  d       |j                          y)z0Consume a request iterator supplied by the user.c            	      ,   	 d} 	 t        j                          t        
      }	 | st        j                          	 t        j                  |      }j                   5  j"                  mj$                  s`|Vt        j                  j&                  }d}j                  t        j                  |   |       t        ||       	 d d d        y j(                  j+                  t         j,                  j.                         t        j0                  |t2              f}j5                  |	      }|s=j(                  j7                  t         j,                  j.                         	 d d d        y fd}t        j8                  j                   j8                  |t;        j<                  t         j>                               j"                  	 d d d        y 	 d d d        y 	 d d d        # t        $ r Y | st        j                          nt
        $ r t        j                          d} t        j                  j                  }d}t        j                  |       j                  t        j                  |   |       t        ||       Y | st        j                          y y w xY w# | st        j                          w w xY w# 1 sw Y   xY wj                   5  j"                  j(                  j+                  t         j,                  j@                         t        jB                  t2              f}j5                  |	      }|s3j(                  j7                  t         j,                  j@                         d d d        y # 1 sw Y   y xY w)NTFzException iterating requests!Exception serializing request!c                  p     j                   d uxs& t        j                  j                   j                  vS r	   )r!   r   r9   send_messager   )r3   s   r   _donezJ_consume_request_iterator.<locals>.consume_request_iterator.<locals>._done   s4    $)JJd$: %5$*$8$8$E$E+099%56r   )spin_cb)"r   enter_user_request_generatornextStopIteration"return_from_user_request_generatorrP   r>   r?   rD   _LOGGER	exceptioncancelr   !STATUS_CODE_TO_CYGRPC_STATUS_CODEr4   	serializer   r!   r#   r@   r   addr9   r^   SendMessageOperation_EMPTY_FLAGSoperater8   wait	functoolspartialblock_if_fork_in_progresssend_close_from_clientSendCloseFromClientOperation)*return_from_user_request_generator_invokedrequestr!   r   serialized_request
operations	operatingr_   callevent_handlerrequest_iteratorrequest_serializerr3   s           r   consume_request_iteratorz;_consume_request_iterator.<locals>.consume_request_iterator   s    9>6@335/0 B==?!(!2!27<N!O  ::%eoo)1#77"B#EEdK#% udG4    		f&:&:&G&GH&,&A&A.'> &@
$(LL]$K	(!II,,V-A-A-N-NO"#   &6
  U__%9%9%*-6->->)/)I)I)..01
 !::1"=   @ A   : 2; -  !  B==?  	99;=A:..9!!'*GEEdK#%udG,A==? B	 B==? B   B __ 	Ezz!		f22IIJ77EH
 LL]C	 II$$,,CCE	E 	E 	Es^   G/ A*KBK1A KK/	J)8J, A?J)J, (J))J, ,KK B!N

NtargetTNr   ForkManagedThread	setDaemonstart)r{   r3   ry   r|   rz   r}   consumption_threads   `````  r   _consume_request_iteratorr      sB    BE BEH  11')  &r   c                    |j                   5  |j                  dj                  |       cddd       S |j                  t        j                  j
                  u r4t        j                  | |j                  |j                        cddd       S t        j                  | |j                  |j                  |j                        cddd       S # 1 sw Y   yxY w)z Calculates error string for RPC.Nz<{} object>)
r   r!   r   r>   r?   OK_OK_RENDEZVOUS_REPR_FORMATr   _NON_OK_RENDEZVOUS_REPR_FORMATr"   )
class_name	rpc_states     r   _rpc_state_stringr     s    			 	.>>! ''
3	. 	. ^^t111-44Z5>5F5FH		. 	. 288INNI,=,=,,.	. 	. 	.s   CAC5CCc                   ~    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd ZddZddZddZddZy)_InactiveRpcErrorzAn RPC error not tied to the execution of a particular RPC.

    The RPC represented by the state object must not be in-progress or
    cancelled.

    Attributes:
      _state: An instance of _RPCState.
    c           
         |j                   5  t        dt        j                  |j                        t        j                  |j
                        |j                  t        j                  |j                              | _        t        j                  |j                        | j                  _	        t        j                  |j                        | j                  _
        d d d        y # 1 sw Y   y xY wr2   )r   r   copydeepcopyr   r    r!   r   _stater   r"   r'   r3   s     r   r(   z_InactiveRpcError.__init__%  s    __ 	Q#Be6L6L(M$(MM%2I2I$J$)JJemm0LNDK $(99U^^#<DKK -1YYu7O7O-PDKK*	Q 	Q 	Qs   CC((C1c                 .    | j                   j                  S r	   r   r   r*   s    r   r   z"_InactiveRpcError.initial_metadata-  s    {{+++r   c                 .    | j                   j                  S r	   r   r    r*   s    r   r    z#_InactiveRpcError.trailing_metadata0  s    {{,,,r   c                 .    | j                   j                  S r	   r   r!   r*   s    r   r!   z_InactiveRpcError.code3  s    {{r   c                 T    t        j                  | j                  j                        S r	   )r   decoder   r   r*   s    r   r   z_InactiveRpcError.details6  s    ~~dkk1122r   c                 T    t        j                  | j                  j                        S r	   )r   r   r   r"   r*   s    r   r"   z$_InactiveRpcError.debug_error_string9  s    ~~dkk<<==r   c                 V    t        | j                  j                  | j                        S r	   r   	__class__r-   r   r*   s    r   _reprz_InactiveRpcError._repr<       !8!8$++FFr   c                 "    | j                         S r	   r   r*   s    r   __repr__z_InactiveRpcError.__repr__?      zz|r   c                 "    | j                         S r	   r   r*   s    r   __str__z_InactiveRpcError.__str__B  r   r   c                      y)zSee grpc.Future.cancel.Fr0   r*   s    r   rg   z_InactiveRpcError.cancelE      r   c                      y)zSee grpc.Future.cancelled.Fr0   r*   s    r   r#   z_InactiveRpcError.cancelledI  r   r   c                      y)zSee grpc.Future.running.Fr0   r*   s    r   runningz_InactiveRpcError.runningM  r   r   c                      y)zSee grpc.Future.done.Tr0   r*   s    r   rU   z_InactiveRpcError.doneQ  s    r   Nc                     | )zSee grpc.Future.result.r0   r'   r   s     r   resultz_InactiveRpcError.resultU  s    
r   c                     | S )zSee grpc.Future.exception.r0   r   s     r   rf   z_InactiveRpcError.exceptionY  s    r   c                 h    	 | # t         j                  $ r t        j                         d   cY S w xY w)zSee grpc.Future.traceback.   )r>   RpcErrorsysexc_infor   s     r   	tracebackz_InactiveRpcError.traceback]  s0    	%J}} 	%<<>!$$	%s    *11c                      ||        y)z"See grpc.Future.add_done_callback.Nr0   )r'   fnr   s      r   add_done_callbackz#_InactiveRpcError.add_done_callbackd  s	    
4r   r	   )r-   r.   r/   __doc__r(   r   r    r!   r   r"   r   r   r   rg   r#   r   rU   r   rf   r   r   r0   r   r   r   r     s^    Q,- 3>G%r   r   c                   p     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Z xZS )_Rendezvousa  An RPC iterator.

    Attributes:
      _state: An instance of _RPCState.
      _call: An instance of SegregatedCall or IntegratedCall.
        In either case, the _call object is expected to have operate, cancel,
        and next_event methods.
      _response_deserializer: A callable taking bytes and return a Python
        object.
      _deadline: A float representing the deadline of the RPC in seconds. Or
        possibly None, to represent an RPC with no deadline at all.
    c                 b    t         t        |           || _        || _        || _        || _        y r	   )superr   r(   r   _call_response_deserializerr   )r'   r3   ry   rH   deadliner   s        r   r(   z_Rendezvous.__init__w  s-    k4)+
&;#!r   c                     | j                   j                  5  | j                   j                  du cddd       S # 1 sw Y   yxY w)zSee grpc.RpcContext.is_activeNr   r   r!   r*   s    r   	is_activez_Rendezvous.is_active~  s6    [["" 	,;;##t+	, 	, 	,	   9Ac                     | j                   j                  5  | j                  
	 ddd       yt        | j                  t	        j                         z
  d      cddd       S # 1 sw Y   yxY w)z"See grpc.RpcContext.time_remainingNr   )r   r   r   maxr
   r*   s    r   time_remainingz_Rendezvous.time_remaining  sV    [["" 	<~~%	< 	< 4>>DIIK7;		< 	< 	<s   A"*A""A+c                    | j                   j                  5  | j                   j                  t        j                  j
                  }d}| j                  j                  t        j                  |   |       d| j                   _
        t        | j                   ||       | j                   j                  j                          	 ddd       y	 ddd       y# 1 sw Y   yxY w)zSee grpc.RpcContext.cancelNz!Locally cancelled by application!TF)r   r   r!   r>   r?   	CANCELLEDr   rg   r   rh   r#   r4   rO   )r'   r!   r   s      r   rg   z_Rendezvous.cancel  s    [["" 	{{'00=

!!==dCWN(,%t{{D'2%%002	 	 	 	 	s   B-CCC c                     | j                   j                  5  | j                   j                  
	 ddd       y| j                   j                  j                  |       	 ddd       y# 1 sw Y   yxY w)z See grpc.RpcContext.add_callbackNFT)r   r   r$   appendr'   rV   s     r   add_callbackz_Rendezvous.add_callback  s^    [["" 	{{$$,	 	 %%,,X6	 	 	s   A'&A''A0c                     | S r	   r0   r*   s    r   __iter__z_Rendezvous.__iter__      r   c                 "    | j                         S r	   _nextr*   s    r   rb   z_Rendezvous.next  r   r   c                 "    | j                         S r	   r   r*   s    r   __next__z_Rendezvous.__next__  r   r   c                     t               r	   NotImplementedErrorr*   s    r   r   z_Rendezvous._next      !##r   c                     t               r	   r   r*   s    r   r"   z_Rendezvous.debug_error_string  r   r   c                 V    t        | j                  j                  | j                        S r	   r   r*   s    r   r   z_Rendezvous._repr  r   r   c                 "    | j                         S r	   r   r*   s    r   r   z_Rendezvous.__repr__  r   r   c                 "    | j                         S r	   r   r*   s    r   r   z_Rendezvous.__str__  r   r   c                    | j                   j                  5  | j                   j                  t        j                  j
                  | j                   _        d| j                   _        d| j                   _        | j                  j                  t        j                  | j                   j                     | j                   j                         | j                   j                  j                          d d d        y # 1 sw Y   y xY w)Nz"Cancelled upon garbage collection!T)r   r   r!   r>   r?   r   r   r#   r   rg   r   rh   rO   r*   s    r   __del__z_Rendezvous.__del__  s    [["" 	3{{'#'??#<#< &J#(,%

!!==dkk>N>NOKK'') %%002	3 	3 	3s   CC;;D)r-   r.   r/   r   r(   r   r   rg   r   r   rb   r   r   r"   r   r   r   r   __classcell__r   s   @r   r   r   i  sO    ",
<$$G	3r   r   c                   v    e Zd ZdZd Zd Zd Zd ZddZddZ	dd	Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zy)_SingleThreadedRendezvousaN  An RPC iterator operating entirely on a single thread.

    The __next__ method of _SingleThreadedRendezvous does not depend on the
    existence of any other thread, including the "channel spin thread".
    However, this means that its interface is entirely synchronous. So this
    class cannot completely fulfill the grpc.Future interface. The result,
    exception, and traceback methods will never block and will instead raise
    an exception if calling the method would result in blocking.

    This means that these methods are safe to call from add_done_callback
    handlers.
    c                 2    | j                   j                  d uS r	   r   r*   s    r   _is_completez&_SingleThreadedRendezvous._is_complete      {{t++r   c                     | j                   j                  5  | j                   j                  cd d d        S # 1 sw Y   y xY wr	   r   r   r#   r*   s    r   r#   z#_SingleThreadedRendezvous.cancelled  1    [["" 	);;((	) 	) 	)	   7A c                     | j                   j                  5  | j                   j                  d u cd d d        S # 1 sw Y   y xY wr	   r   r*   s    r   r   z!_SingleThreadedRendezvous.running  6    [["" 	,;;##t+	, 	, 	,r   c                     | j                   j                  5  | j                   j                  d ucd d d        S # 1 sw Y   y xY wr	   r   r*   s    r   rU   z_SingleThreadedRendezvous.done  6    [["" 	0;;##4/	0 	0 	0r   Nc                    ~| j                   j                  5  | j                         st        j                  j                  d      | j                   j                  t        j                  j                  u r| j                   j                  cddd       S | j                   j                  rt        j                         | # 1 sw Y   yxY w)a9  Returns the result of the computation or raises its exception.

        This method will never block. Instead, it will raise an exception
        if calling this method would otherwise result in blocking.

        Since this method will never block, any `timeout` argument passed will
        be ignored.
        zJ_SingleThreadedRendezvous only supports result() when the RPC is complete.N)r   r   r   r>   experimental
UsageErrorr!   r?   r   r   r#   FutureCancelledErrorr   s     r   r   z _SingleThreadedRendezvous.result  s     [["" 
	$$&''22`  {{4??#5#55{{++
	 
	 &&//11

	 
	s   A5C,CCc                    ~| j                   j                  5  | j                         st        j                  j                  d      | j                   j                  t        j                  j                  u r
	 ddd       y| j                   j                  rt        j                         | cddd       S # 1 sw Y   yxY w)a*  Return the exception raised by the computation.

        This method will never block. Instead, it will raise an exception
        if calling this method would otherwise result in blocking.

        Since this method will never block, any `timeout` argument passed will
        be ignored.
        zM_SingleThreadedRendezvous only supports exception() when the RPC is complete.N)r   r   r   r>   r   r   r!   r?   r   r#   r   r   s     r   rf   z#_SingleThreadedRendezvous.exception  s     [["" 
	$$&''22c  {{4??#5#55
	 
	 &&//11
	 
	 
	s   A!B7+B77C c                    ~| j                   j                  5  | j                         st        j                  j                  d      | j                   j                  t        j                  j                  u r
	 ddd       y| j                   j                  rt        j                         	 | # t        j                  $ r# t        j                         d   cY cddd       S w xY w# 1 sw Y   yxY w)a;  Access the traceback of the exception raised by the computation.

        This method will never block. Instead, it will raise an exception
        if calling this method would otherwise result in blocking.

        Since this method will never block, any `timeout` argument passed will
        be ignored.
        zM_SingleThreadedRendezvous only supports traceback() when the RPC is complete.Nr   )r   r   r   r>   r   r   r!   r?   r   r#   r   r   r   r   r   s     r   r   z#_SingleThreadedRendezvous.traceback  s     [["" 	-$$&''22c  {{4??#5#55	- 	- &&//11-J}} -<<>!,,	- 	--	- 	-s0   A!C(*C(-B//*C%C($C%%C((C1c                    | j                   j                  5  | j                   j                  C| j                   j                  j	                  t        j                  ||              	 d d d        y 	 d d d         ||        y # 1 sw Y   xY wr	   r   r   r!   r$   r   ro   rp   r'   r   s     r   r   z+_SingleThreadedRendezvous.add_done_callback)  r    [["" 	{{'%%,,Y->->r4-HI	 	'	
 	4	 	   ABBc                     | j                   j                  5  | j                   j                  '| j                          | j                   j                  '| j                   j                  cddd       S # 1 sw Y   yxY w)See grpc.Call.initial_metadataN)r   r   r   _consume_next_eventr*   s    r   r   z*_SingleThreadedRendezvous.initial_metadata1  sa    [["" 	0 ++..6((* ++..6;;//	0 	0 	0s   =A4A44A=c                     | j                   j                  5  | j                   j                  t        j                  j                  d      | j                   j                  cddd       S # 1 sw Y   yxY w)See grpc.Call.trailing_metadataNz4Cannot get trailing metadata until RPC is completed.)r   r   r    r>   r   r   r*   s    r   r    z+_SingleThreadedRendezvous.trailing_metadata:  s_    [["" 	1{{,,4''22JL L;;00		1 	1 	1   AA,,A5c                     | j                   j                  5  | j                   j                  t        j                  j                  d      | j                   j                  cddd       S # 1 sw Y   yxY w)See grpc.Call.codeNz'Cannot get code until RPC is completed.)r   r   r!   r>   r   r   r*   s    r   r!   z_SingleThreadedRendezvous.codeB  s]    [["" 	${{'''22=? ?;;##		$ 	$ 	$r  c                    | j                   j                  5  | j                   j                  t        j                  j                  d      t        j                  | j                   j                        cddd       S # 1 sw Y   yxY w)See grpc.Call.detailsNz*Cannot get details until RPC is completed.)r   r   r   r>   r   r   r   r   r*   s    r   r   z!_SingleThreadedRendezvous.detailsJ  sh    [["" 	7{{""*''22@B B>>$++"5"56		7 	7 	7   AA??Bc                     | j                   j                         }| j                  j                  5  t	        || j                  | j
                        }|D ]	  } |         	 d d d        |S # 1 sw Y   |S xY wr	   )r   
next_eventr   r   rL   r   )r'   rG   r$   rV   s       r   r   z-_SingleThreadedRendezvous._consume_next_eventR  sq    

%%'[["" 	%eT[[&*&A&ACI%  
	 	 s   0A,,A6c                    	 | j                          | j                  j                  5  | j                  j                  2| j                  j                  }d | j                  _        |cd d d        S t        j
                  j                  | j                  j                  vrR| j                  j                  t        j                  j                  u r
t               | j                  j                  | d d d        # 1 sw Y   
xY wr	   )r   r   r   r   r   r9   r;   r   r!   r>   r?   r   rc   )r'   r   s     r   _next_responsez(_SingleThreadedRendezvous._next_response]  s    $$&&& 	#;;''3#{{33H+/DKK(#		# 	#
 ))99P{{''4??+=+==+o-))5"
	# 	# 	#s   ?C<1BC<<Dc                    | j                   j                  5  | j                   j                  | j                   j                  j	                  t
        j                  j                         | j                  j                  t        j                  t              fd       }|sz| j                   j                  j                  t
        j                  j                         n<| j                   j                  t        j                  j                  u r
t!               | d d d        | j#                         S # 1 sw Y   | j#                         S xY wr	   )r   r   r!   r   rj   r   r9   r;   r   rm   ReceiveMessageOperationrl   r8   r>   r?   r   rc   r
  )r'   rx   s     r   r   z_SingleThreadedRendezvous._nextk  s    [["" 	{{' ##F$8$8$H$HI JJ..33LACTK	 KKOO**6+?+?+O+OP!!T__%7%77#o%
+	, ""$$-	, ""$$s   DD33Ec                    | j                   j                  5  | j                   j                  t        j                  j                  d      t        j                  | j                   j                        cd d d        S # 1 sw Y   y xY w)Nz5Cannot get debug error string until RPC is completed.)r   r   r"   r>   r   r   r   r   r*   s    r   r"   z,_SingleThreadedRendezvous.debug_error_string  sl    [["" 	B{{--5''22KM M>>$++"@"@A		B 	B 	Br  r	   )r-   r.   r/   r   r   r#   r   rU   r   rf   r   r   r   r    r!   r   r   r
  r   r"   r0   r   r   r   r     sX    ,),0,,-201$7	#%2Br   r   c                   j    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 ZddZddZddZd Zd Zy)_MultiThreadedRendezvousa  An RPC iterator that depends on a channel spin thread.

    This iterator relies upon a per-channel thread running in the background,
    dequeueing events from the completion queue, and notifying threads waiting
    on the threading.Condition object in the _RPCState object.

    This extra thread allows _MultiThreadedRendezvous to fulfill the grpc.Future interface
    and to mediate a bidirection streaming RPC.
    c                       j                   j                  5   fd}t        j                   j                   j                  j                  |        j                   j                  cddd       S # 1 sw Y   yxY w)r   c                  4     j                   j                  d uS r	   r   r*   s   r   r_   z8_MultiThreadedRendezvous.initial_metadata.<locals>._done  s    {{334??r   N)r   r   r   rn   r   r'   r_   s   ` r   r   z)_MultiThreadedRendezvous.initial_metadata  sX    [["" 	0@ LL..33U;;;//	0 	0 	0   AA11A:c                       j                   j                  5   fd}t        j                   j                   j                  j                  |        j                   j                  cddd       S # 1 sw Y   yxY w)r   c                  4     j                   j                  d uS r	   r   r*   s   r   r_   z9_MultiThreadedRendezvous.trailing_metadata.<locals>._done  s    {{44D@@r   N)r   r   r   rn   r    r  s   ` r   r    z*_MultiThreadedRendezvous.trailing_metadata  sX    [["" 	1A LL..33U;;;00	1 	1 	1r  c                       j                   j                  5   fd}t        j                   j                   j                  j                  |        j                   j                  cddd       S # 1 sw Y   yxY w)r  c                  4     j                   j                  d uS r	   r   r*   s   r   r_   z,_MultiThreadedRendezvous.code.<locals>._done  s    {{''t33r   N)r   r   r   rn   r!   r  s   ` r   r!   z_MultiThreadedRendezvous.code  sW    [["" 	$4 LL..33U;;;##	$ 	$ 	$r  c                       j                   j                  5   fd}t        j                   j                   j                  j                  |       t        j                   j                   j
                        cddd       S # 1 sw Y   yxY w)r  c                  4     j                   j                  d uS r	   )r   r   r*   s   r   r_   z/_MultiThreadedRendezvous.details.<locals>._done  s    {{**$66r   N)r   r   r   rn   r   r   r  s   ` r   r   z _MultiThreadedRendezvous.details  s`    [["" 	77 LL..33U;>>$++"5"56	7 	7 	7   A"BBc                       j                   j                  5   fd}t        j                   j                   j                  j                  |       t        j                   j                   j
                        cd d d        S # 1 sw Y   y xY w)Nc                  4     j                   j                  d uS r	   )r   r"   r*   s   r   r_   z:_MultiThreadedRendezvous.debug_error_string.<locals>._done  s    {{55TAAr   )r   r   r   rn   r   r"   r  s   ` r   r"   z+_MultiThreadedRendezvous.debug_error_string  se    [["" 	BB LL..33U;>>$++"@"@A	B 	B 	Br  c                     | j                   j                  5  | j                   j                  cd d d        S # 1 sw Y   y xY wr	   r   r*   s    r   r#   z"_MultiThreadedRendezvous.cancelled  r   r   c                     | j                   j                  5  | j                   j                  d u cd d d        S # 1 sw Y   y xY wr	   r   r*   s    r   r   z _MultiThreadedRendezvous.running  r   r   c                     | j                   j                  5  | j                   j                  d ucd d d        S # 1 sw Y   y xY wr	   r   r*   s    r   rU   z_MultiThreadedRendezvous.done  r   r   c                 2    | j                   j                  d uS r	   r   r*   s    r   r   z%_MultiThreadedRendezvous._is_complete  r   r   Nc                    | j                   j                  5  t        j                  | j                   j                  j                  | j                  |      }|rt        j                         | j                   j                  t
        j                  j                  u r| j                   j                  cddd       S | j                   j                  rt        j                         | # 1 sw Y   yxY w)zReturns the result of the computation or raises its exception.

        See grpc.Future.result for the full API contract.
        r   N)r   r   r   rn   r   r>   FutureTimeoutErrorr!   r?   r   r   r#   r   r'   r   	timed_outs      r   r   z_MultiThreadedRendezvous.result  s    
 [["" 	T[[%:%:%?%?%)%6%6-46I --//;;##t'9'99;;//	 	 [[**3355J	 	s   BC)=,C))C2c                    | j                   j                  5  t        j                  | j                   j                  j                  | j                  |      }|rt        j                         | j                   j                  t
        j                  j                  u r
	 ddd       y| j                   j                  rt        j                         | cddd       S # 1 sw Y   yxY w)zvReturn the exception raised by the computation.

        See grpc.Future.exception for the full API contract.
        r   N)r   r   r   rn   r   r>   r"  r!   r?   r   r#   r   r#  s      r   rf   z"_MultiThreadedRendezvous.exception  s    
 [["" 	 T[[%:%:%?%?%)%6%6-46I --//;;##t'9'99	  	  [[**3355	  	  	 s   BC(+CC&c                 4   | j                   j                  5  t        j                  | j                   j                  j                  | j                  |      }|rt        j                         | j                   j                  t
        j                  j                  u r
	 ddd       y| j                   j                  rt        j                         	 | # t
        j                  $ r# t        j                         d   cY cddd       S w xY w# 1 sw Y   yxY w)zAccess the traceback of the exception raised by the computation.

        See grpc.future.traceback for the full API contract.
        r   Nr   )r   r   r   rn   r   r>   r"  r!   r?   r   r#   r   r   r   r   r#  s      r   r   z"_MultiThreadedRendezvous.traceback  s    
 [["" 	1T[[%:%:%?%?%)%6%6-46I --//;;##t'9'99	1 	1 [[**33551"
== 1"||~a00	1 	11	1 	1s0   BD(*DC*D?D
DDDc                    | j                   j                  5  | j                   j                  C| j                   j                  j	                  t        j                  ||              	 d d d        y 	 d d d         ||        y # 1 sw Y   xY wr	   r   r   s     r   r   z*_MultiThreadedRendezvous.add_done_callback  r   r   c                      j                   j                  5   j                   j                  t         j                    j                        } j                   j
                  j                  t        j                  j                          j                  j                  t        j                  t              f|      }|sz j                   j
                  j                  t        j                  j                         n< j                   j                  t        j                   j"                  u r
t%                 fd}t'        j(                   j                   j                  j(                  |        j                   j*                  2 j                   j*                  }d  j                   _        |cd d d        S t        j                  j                   j                   j
                  vrR j                   j                  t        j                   j"                  u r
t%                j                   j                   d d d        y # 1 sw Y   y xY w)Nc                       j                   j                  d uxsJ t        j                  j                   j                   j
                  vxr  j                   j                  d uS r	   )r   r   r   r9   r;   r   r!   r*   s   r   _response_readyz7_MultiThreadedRendezvous._next.<locals>._response_ready+  sU    ,,D8 7--== $0 6))58r   )r   r   r!   rY   r   r   rj   r   r9   r;   r   rm   r  rl   r8   r>   r?   r   rc   r   rn   r   )r'   rz   rx   r*  r   s   `    r   r   z_MultiThreadedRendezvous._next  s   [["" 	{{' .t{{/3/J/J!L##F$8$8$H$HI JJ..33LAC!#	 !KKOO**6+?+?+O+OP!!T__%7%77#o%
8 LL..33_E{{##/;;//'+$3	 	4 %%55T[[__L;;##t'9'99'/)[[%%1J=	 	 	s   FI=BIIr	   )r-   r.   r/   r   r   r    r!   r   r"   r#   r   rU   r   r   rf   r   r   r   r0   r   r   r  r    sN    01$7B),0,& &1,r   r  c                     t        |      }t        j                  | |      }|7t        dddt        j
                  j                  d      }t        |      }|d |fS ||d fS )Nr0   r\   )r   r   ri   r   r>   r?   r@   r   )ru   r   r|   r   rv   r3   rR   s          r   _start_unary_requestr,  =  sj    !H **74FG!"b"doo&>&>:<!%(u$$+T11r   c                     | j                   t        j                  j                  u r*|rt	        | |d |      }| j
                  |fS | j
                  S t        |       r	   )r!   r>   r?   r   r  r   r   )r3   ry   	with_callr   
rendezvouss        r   _end_unary_response_blockingr0  I  sO    zzT__'''1%tXNJ>>:-->>!&&r   c                     t        j                  | |      t        j                  t              t        j                  t              ft        j
                  t              ffS r	   )r   SendInitialMetadataOperationr  rl   ReceiveStatusOnClientOperationReceiveInitialMetadataOperationmetadatainitial_metadata_flagss     r   %_stream_unary_invocation_operationsesr8  T  sV     //0FH**<811,?		
 
	/	/	=? r   c                 :    t        d t        | |      D              S )Nc              3   $   K   | ]  }|d f 
 y wr	   r0   ).0rw   s     r   	<genexpr>zA_stream_unary_invocation_operationses_and_tags.<locals>.<genexpr>b  s"      +  	 +   )tupler8  r5  s     r   ._stream_unary_invocation_operationses_and_tagsr?  `  s)     + >(*+ + +r   c                 d    t        j                         }|| y || |S | || S t        ||       S r	   )r   get_deadline_from_contextmin)user_deadlineparent_deadlines     r   _determine_deadlinerE  i  sM    668O=#8		$)>		"'>?M22r   c                   T    e Zd Zd Zd Zd Z	 	 	 	 	 ddZ	 	 	 	 	 ddZ	 	 	 	 	 ddZy)	_UnaryUnaryMultiCallablec                 |    || _         || _        || _        || _        || _        t        j                         | _        y r	   _channel_managed_call_method_request_serializerr   r   build_census_context_contextr'   channelmanaged_callmethodr|   rH   s         r   r(   z!_UnaryUnaryMultiCallable.__init__x  8    )#5 &;#335r   c                    t        ||| j                        \  }}}t               j                  |      }	t	        j
                  ||      }
|d d d |fS t        t        d d d d       }t        j                  |
|	      t        j                  |t              t        j                  t              t        j                  t              t        j                  t              t        j                  t              f}|||d fS r	   )r,  rM  _InitialMetadataFlagswith_wait_for_readyr   augment_metadatar   _UNARY_UNARY_INITIAL_DUEr   r2  rk   rl   rs   r4  r  r3  )r'   ru   r   r6  wait_for_readycompressionr   rv   r/  r7  augmented_metadatar3   rw   s                r   _preparez!_UnaryUnaryMultiCallable._prepare  s    3GWd66480$j!6!8!L!L")::k#%tZ//6dD$OE334F4JL++,>M33LA66|D..|<55lCJ *h44r   c           
      d   | j                  |||||      \  }}}	}
||
| j                  j                  t        j                  j
                  | j                  d t        |	      ||d n|j                  |d ff| j                        }|j                         }t        ||| j                         ||fS r	   )r]  rJ  segregated_callr   PropagationConstantsGRPC_PROPAGATE_DEFAULTSrL  rE  _credentialsrO  r  rL   r   )r'   ru   r   r6  credentialsrZ  r[  r3   rw   r   r/  ry   rG   s                r   	_blockingz"_UnaryUnaryMultiCallable._blocking  s    26--Wh3E/z8Z===00++CCd$7$A8#+1I1IM L ]]$D OO%E%(C(CD$;r   Nc                 P    | j                  ||||||      \  }}t        ||dd       S r   rd  r0  	r'   ru   r   r6  rc  rZ  r[  r3   ry   s	            r   __call__z!_UnaryUnaryMultiCallable.__call__  s5     ~~gw+&4kCt+E4EEr   c                 P    | j                  ||||||      \  }}t        ||dd       S NTrf  rg  s	            r   r.  z"_UnaryUnaryMultiCallable.with_call  s5     ~~gw+&4kCt+E4tDDr   c                 B   | j                  |||||      \  }}}	}
||
t        || j                        }| j                  t        j
                  j                  | j                  d |	||d n|j                  |f|| j                  	      }t        ||| j                  |	      S r	   )r]  rY   r   rK  r   r`  ra  rL  rb  rO  r  )r'   ru   r   r6  rc  rZ  r[  r3   rw   r   r/  rz   ry   s                r   futurez_UnaryUnaryMultiCallable.future  s     37--Wh3E/z8Z=*5$2M2MNM%%++CCdHh#+1I1I}dmm	=D
 ,E4,0,G,G,46 6r   NNNNN)	r-   r.   r/   r(   r]  rd  rh  r.  rl  r0   r   r   rG  rG  u  sU    65,( ! $!	F "!%"	E "6r   rG  c                   $    e Zd Zd Z	 	 	 	 	 ddZy)'_SingleThreadedUnaryStreamMultiCallablec                 n    || _         || _        || _        || _        t	        j
                         | _        y r	   )rJ  rL  rM  r   r   rN  rO  )r'   rQ  rS  r|   rH   s        r   r(   z0_SingleThreadedUnaryStreamMultiCallable.__init__  s0    #5 &;#335r   Nc           
      \   t        |      }t        j                  || j                        }|2t	        dddt
        j                  j                  d      }	t        |	      t	        t        d d d d       }	|d n|j                  }
t               j                  |      }t        j                  ||      }t        j                   ||      t        j"                  |t$              t        j&                  t$              ft        j(                  t$              ft        j*                  t$              ff}t-        d |D              }| j.                  j1                  t        j2                  j4                  | j6                  d t9        |      ||
|| j:                        }t=        |	|| j>                  |      S )Nr0   r\   c              3   $   K   | ]  }|d f 
 y wr	   r0   )r;  opss     r   r<  zC_SingleThreadedUnaryStreamMultiCallable.__call__.<locals>.<genexpr>  s     #FCS$K#Fr=  ) r   r   ri   rM  r   r>   r?   r@   r   _UNARY_STREAM_INITIAL_DUErb  rV  rW  r   rX  r   r2  rk   rl   rs   r3  r4  r>  rJ  r_  r`  ra  rL  rE  rO  r   r   )r'   ru   r   r6  rc  rZ  r[  r   rv   r3   call_credentialsr7  r\  rw   operations_and_tagsry   s                   r   rh  z0_SingleThreadedUnaryStreamMultiCallable.__call__  s    W%$..w/3/G/GI%b"b$//*B*B>@E#E**3T4tL#.#64K<T<T!6!8!L!L")::k# 001C1GI(();\J00>@ 22<@B33LAC

 $#F:#FF}},,''??%h/;K0 ))-)D)DhP 	Pr   rm  r-   r.   r/   r(   rh  r0   r   r   ro  ro    s    6 $Pr   ro  c                   $    e Zd Zd Z	 	 	 	 	 ddZy)_UnaryStreamMultiCallablec                 |    || _         || _        || _        || _        || _        t        j                         | _        y r	   rI  rP  s         r   r(   z"_UnaryStreamMultiCallable.__init__  rT  r   Nc                    t        ||| j                        \  }}}	t               j                  |      }
||	t	        j
                  ||      }t        t        d d d d       }t        j                  ||
      t        j                  |t              t        j                  t              t        j                  t              ft        j                  t              ff}| j                  t        j                   j"                  | j$                  d t'        |      ||d n|j(                  |t+        || j,                        | j.                  	      }t1        ||| j,                  |      S r	   )r,  rM  rV  rW  r   rX  r   rt  r   r2  rk   rl   rs   r3  r4  rK  r`  ra  rL  rE  rb  rY   r   rO  r  )r'   ru   r   r6  rc  rZ  r[  r   rv   r/  r7  r\  r3   operationsesry   s                  r   rh  z"_UnaryStreamMultiCallable.__call__  sV    4HWd66480$j!6!8!L!L"%!-!>!>+"'7tT4PE 778J8NP//0B0<>77E99,G 77EG
L %%++CCd$7$A8#+1I1InU-1-H-HJD ,E4,0,G,G,46 6r   rm  rw  r0   r   r   ry  ry    s    6 &6r   ry  c                   N    e Zd Zd Zd Z	 	 	 	 	 ddZ	 	 	 	 	 ddZ	 	 	 	 	 ddZy)_StreamUnaryMultiCallablec                 |    || _         || _        || _        || _        || _        t        j                         | _        y r	   rI  rP  s         r   r(   z"_StreamUnaryMultiCallable.__init__@  rT  r   c                    t        |      }t        t        d d d d       }t               j	                  |      }	t        j                  ||      }
| j                  j                  t        j                  j                  | j                  d t        |      |
|d n|j                  t        |
|	      | j                         }t#        |||| j$                  d        	 |j'                         }|j(                  5  t+        ||| j,                         |j(                  j/                          |j0                  s	 d d d        ||fS 	 d d d        q# 1 sw Y   
xY wr	   )r   r   _STREAM_UNARY_INITIAL_DUErV  rW  r   rX  rJ  r_  r   r`  ra  rL  rE  rb  r?  rO  r   rM  r  r   rL   r   rO   r   )r'   r{   r   r6  rc  rZ  r[  r   r3   r7  r\  ry   rG   s                r   rd  z#_StreamUnaryMultiCallable._blockingI  s8   W%3T4tL!6!8!L!L")::k#}},,''??%h/1C'D[-E-E:"$:<=A]]L 	""2E4"&":":D	BOO%E eUD,G,GH**,yy	
 d{ !  s   3?EENc                 P    | j                  ||||||      \  }}t        ||dd       S r   rf  	r'   r{   r   r6  rc  rZ  r[  r3   ry   s	            r   rh  z"_StreamUnaryMultiCallable.__call__b  s6     ~~&6&1>;Pt+E4EEr   c                 P    | j                  ||||||      \  }}t        ||dd       S rj  rf  r  s	            r   r.  z#_StreamUnaryMultiCallable.with_callm  s6     ~~&6&1>;Pt+E4tDDr   c                    t        |      }t        t        d d d d       }t        || j                        }	t               j                  |      }
t        j                  ||      }| j                  t        j                  j                  | j                  d |||d n|j                  t        ||
      |	| j                   	      }t#        |||| j$                  |	       t'        ||| j                  |      S r	   )r   r   r  rY   r   rV  rW  r   rX  rK  r   r`  ra  rL  rb  r8  rO  r   rM  r  )r'   r{   r   r6  rc  rZ  r[  r   r3   rz   r7  r\  ry   s                r   rl  z _StreamUnaryMultiCallable.futurex  s     W%3T4tL&ud.I.IJ!6!8!L!L")::k#!!''??(.'D[-E-E1(2HJ4==* 	""2E4"&":":M	K't(,(C(CXO 	Or   rm  )r-   r.   r/   r(   rd  rh  r.  rl  r0   r   r   r~  r~  =  sQ    66 ! $!	F "!%"	E "Or   r~  c                   $    e Zd Zd Z	 	 	 	 	 ddZy)_StreamStreamMultiCallablec                 |    || _         || _        || _        || _        || _        t        j                         | _        y r	   rI  rP  s         r   r(   z#_StreamStreamMultiCallable.__init__  rT  r   Nc                 t   t        |      }t        t        d d d d       }t               j	                  |      }	t        j                  ||      }
t        j                  |
|	      t        j                  t              ft        j                  t              ff}t        || j                        }| j                  t        j                  j                   | j"                  d t%        |      |
|d n|j&                  ||| j(                  	      }t+        |||| j,                  |       t/        ||| j                  |      S r	   )r   r   _STREAM_STREAM_INITIAL_DUErV  rW  r   rX  r   r2  r3  rl   r4  rY   r   rK  r`  ra  rL  rE  rb  rO  r   rM  r  )r'   r{   r   r6  rc  rZ  r[  r   r3   r7  r\  r|  rz   ry   s                 r   rh  z#_StreamStreamMultiCallable.__call__  s/    W%4dD$M!6!8!L!L")::k# 334F4JL55lC
 33LAC
 'ud.I.IJ!!''??%h/1C'D[-E-E-	8
 	""2E4"&":":M	K't(,(C(CXO 	Or   rm  rw  r0   r   r   r  r    s    6 ! $!Or   r  c                   ,     e Zd ZdZef fd	Zd Z xZS )rV  z'Stores immutable initial metadata flagsc                 f    |t         j                  j                  z  }t        t        |   | |      S r	   )r   InitialMetadataFlags	used_maskr   rV  __new__)clsvaluer   s     r   r  z_InitialMetadataFlags.__new__  s.    ,,666*C8eDDr   c                 0   ||rG| j                  | t        j                  j                  z  t        j                  j                  z        S |sH| j                  | t        j                  j                   z  t        j                  j                  z        S | S r	   )r   r   r  rZ  wait_for_ready_explicitly_set)r'   rZ  s     r   rW  z)_InitialMetadataFlags.with_wait_for_ready  s    %~~dV-H-H-W-W&W//MM'N O O#~~df.I.I.X.X-X&X//MM'N O Or   )r-   r.   r/   r   rl   r  rW  r   r   s   @r   rV  rV    s    1' Er   rV  c                       e Zd Zd Zd Zd Zy)_ChannelCallStatec                 `    t        j                         | _        || _        d| _        d| _         y )Nr   F)r   LocklockrQ  managed_callsr'   rQ  s     r   r(   z_ChannelCallState.__init__  s&    NN$	r   c                     d| _         y )Nr   )r  r*   s    r   r+   z&_ChannelCallState.reset_postfork_child  s
    r   c                     	 | j                   j                  t        j                  j                  d       y # t
        t        f$ r Y y w xY w)NzChannel deallocated!)rQ  closer   r?   r#   	TypeErrorAttributeErrorr*   s    r   r   z_ChannelCallState.__del__  sA    	LLv00::57>* 		s   47 A	A	N)r-   r.   r/   r(   r+   r   r0   r   r   r  r    s    r   r  c                 ~      fd}t        j                  |      }|j                  d       |j                          y )Nc                  |   	 t        j                         j                  j                         } | j                  t         j
                  j                  k(  rX| j                  |       }|rDj                  5  xj                  dz  c_	        j                  dk(  r
	 d d d        y 	 d d d        # 1 sw Y   
xY w)N   r   )
r   rq   rQ  next_call_eventcompletion_typeCompletionTypequeue_timeouttagr  r  )rG   call_completedr3   s     r   channel_spinz._run_channel_spin_thread.<locals>.channel_spin  s    ,,U3MM113E$$(=(=(K(KK"YYu-NZZ ''1,'**a/ /  s   9&B22B;r~   Tr   )r3   r  channel_spin_threads   `  r   _run_channel_spin_threadr    s5     !22,G!!$'r   c                       fd}|S )Nc	                 (   t        fd|D              }	j                  5  j                  j                  | ||||||	|      }
j                  dk(  rd_        t               nxj                  dz  c_        |
cddd       S # 1 sw Y   yxY w)a'  Creates a cygrpc.IntegratedCall.

        Args:
          flags: An integer bitfield of call flags.
          method: The RPC method.
          host: A host string for the created call.
          deadline: A float to be the deadline of the created call or None if
            the call is to have an infinite deadline.
          metadata: The metadata for the call or None.
          credentials: A cygrpc.CallCredentials or None.
          operationses: An iterable of iterables of cygrpc.Operations to be
            started on the call.
          event_handler: A behavior to call to handle the events resultant from
            the operations on the call.
          context: Context object for distributed tracing.
        Returns:
          A cygrpc.IntegratedCall with which to conduct an RPC.
        c              3   &   K   | ]  }|f 
 y wr	   r0   )r;  rw   rz   s     r   r<  zC_channel_managed_call_management.<locals>.create.<locals>.<genexpr>  s#      &*  '
 &*s   r   r  N)r>  r  rQ  integrated_callr  r  )flagsrS  hostr   r6  rc  r|  rz   contextoperationses_and_tagsry   r3   s          `   r   createz0_channel_managed_call_management.<locals>.create  s    ( !& &* )&* !* ZZ 		==00h19;1FQD ""a'&'#(/##q(#		 		 		s   ABBr0   )r3   r  s   ` r    _channel_managed_call_managementr    s    !F Mr   c                       e Zd Zd Zd Zy)_ChannelConnectivityStatec                     t        j                         | _        || _        d| _        d | _        d| _        g | _        d| _        y r   )	r   RLockr  rQ  pollingconnectivitytry_to_connectcallbacks_and_connectivities
deliveringr  s     r   r(   z"_ChannelConnectivityState.__init__$  s=    OO%	 #,.)r   c                 J    d| _         d | _        d| _        g | _        d| _        y r   )r  r  r  r  r  r*   s    r   r+   z._ChannelConnectivityState.reset_postfork_child-  s(     #,.)r   Nr,   r0   r   r   r  r  "  s      r   r  c                     g }| j                   D ]6  }|\  }}|| j                  us|j                  |       | j                  |d<   8 |S )Nr  )r  r  r   )r3   callbacks_needing_updatecallback_and_connectivityrV   callback_connectivitys        r   _deliveriesr  5  s^    !%*%G%G >!+D(' (:(::$++H5+0+=+=%a(	>
 $#r   c                 B   |}|}	 |D ]   }t        j                  |        	  ||       " | j                  5  t        |       }|r| j                  }nd| _	        	 d d d        y 	 d d d        g# t        $ r t        j	                  t
               Y w xY w# 1 sw Y   2xY wr   )
r   rq   rP   re   rf   0_CHANNEL_SUBSCRIPTION_CALLBACK_ERROR_LOG_MESSAGEr  r  r  r  )r3   initial_connectivityinitial_callbacksr  r$   rV   s         r   _deliverr  ?  s    'L!I
! 	FH,,U3F&	F ZZ 	#E*I$11#( 	 	 	 
  F!!DFF	 	s   A-#B-"BBBc                     t        j                  t        | | j                  |f      }|j	                  d       |j                          d| _        y N)r   argsT)r   r   r  r  r   r   r  )r3   r$   delivering_threads      r   _spawn_deliveryr  S  sM    005:5:5G5G5>734 %Er   c                 p   |}|j                  |      }| j                  5  t        j                  |   | _        t        d | j                  D              }| j                  D ]  }| j                  |d<    |rt        | |       d d d        	 |j                  |t        j                         dz         }t        j                  |        | j                  5  | j                  s$| j                  sd| _        d | _        	 d d d        y | j                  }d| _        d d d        |j                  s|rc|j                  |      }| j                  5  t        j                  |   | _        | j                  st!        |       }|rt        | |       d d d        # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   $xY w)Nc              3   &   K   | ]	  \  }}|  y wr	   r0   )r;  rV   (unused_but_known_to_be_none_connectivitys      r   r<  z%_poll_connectivity.<locals>.<genexpr>g  s      0K#KH0s   r  g?F)check_connectivity_stater  r   1CYGRPC_CONNECTIVITY_STATE_TO_CHANNEL_CONNECTIVITYr  r>  r  r  watch_connectivity_stater
   r   rq   r  r  successr  r  )r3   rQ  initial_try_to_connectr  r  r$   r  rG   s           r   _poll_connectivityr  `  s   +N33NCL	 
.==lL 	  0..0 0	 */)K)K 	>%+0+=+=%a(	>E9-
. 0015s1BD((/ZZ 	)55e>R>R %%)"		) 	)
 #11N#(E 	) ==N";;NKL :MM$& " '' +E 2I 'y9: 
. 
.	) 	): :s*   A#F(F F >F,F F),F5c           	         | j                   5  | j                  s| j                  swt        j                  t
        | | j                  t        |      f      }|j                  d       |j                          d| _        | j                  j                  |d g       n| j                  s_| j                  St        | |f       | xj                  t        |      z  c_        | j                  j                  || j                  g       n;| xj                  t        |      z  c_        | j                  j                  |d g       d d d        y # 1 sw Y   y xY wr  )r  r  r  r   r   r  rQ  boolr   r   r   r  r  r  r  )r3   rV   r  polling_threads       r   
_subscriber    s   	 H11%--#55)U]]D,@ACN $$T*  " EM..55x6FG!!e&8&8&DEH;/  D$88 ..555--.0   D$88 ..55x6FG!H H Hs   D6EEc                     | j                   5  t        | j                        D ]*  \  }\  }}||k(  s| j                  j                  |        n d d d        y # 1 sw Y   y xY wr	   )r  	enumerater  pop)r3   rV   indexsubscribed_callbackunused_connectivitys        r   _unsubscriber    sh    	 AJ22B4 	=E=')<..2266u=		  s   $AAA"c                     t        j                  |      }t        |       |z   t        j                  j
                  t        ffz   S r	   )r   create_channel_optionr>  r   ChannelArgKeyprimary_user_agent_string_USER_AGENT)base_optionsr[  compression_options      r   _augment_optionsr    sG    %;;KH!33668 7  r   c                     g }g }| D ]O  }|d   t         j                  j                  j                  k(  r|j	                  |       ?|j	                  |       Q ||fS )z;Separates core channel options from Python channel options.r   )r>   r   ChannelOptionsSingleThreadedUnaryStreamr   )optionscore_optionspython_optionspairs       r   _separate_channel_optionsr    sb    LN &7d''66PPP!!$'%	&
 <''r   c                       e Zd ZdZd Zd ZddZd Z	 	 ddZ	 	 ddZ		 	 dd	Z
	 	 dd
Zd Zd Zd Zd Zd Zd Zd Zy)Channelz7A cygrpc.Channel-backed implementation of grpc.Channel.c                    t        |      \  }}t        | _        | j                  |       t	        j
                  t        j                  |      t        ||      |      | _	        t        | j                        | _        t        | j                        | _        t	        j                  |        t        j                  rt	        j                           yy)aP  Constructor.

        Args:
          target: The target to which to connect.
          options: Configuration options for the channel.
          credentials: A cygrpc.ChannelCredentials or None.
          compression: An optional value indicating the compression method to be
            used over the lifetime of the channel.
        N)r  %_DEFAULT_SINGLE_THREADED_UNARY_STREAM_single_threaded_unary_stream_process_python_optionsr   r  r   encoder  rJ  r  _call_stater  _connectivity_statefork_register_channelg_gevent_activatedgevent_increment_channel_count)r'   r   r  rc  r[  r  r  s          r   r(   zChannel.__init__  s     (A'I$-R*$$^4NN6"$4\;$O -T]];#<T]]#K $$T*$$113 %r   c                 v    |D ]4  }|d   t         j                  j                  j                  k(  s.d| _        6 y)zASets channel attributes according to python-only channel options.r   TN)r>   r   r  r  r  )r'   r  r  s      r   r  zChannel._process_python_options  s7    " 	:DAw$++::TTT592	:r   Nc                 2    t        | j                  ||       y r	   )r  r  )r'   rV   r  s      r   	subscribezChannel.subscribe  s    4++X~Fr   c                 0    t        | j                  |       y r	   )r  r  r   s     r   unsubscribezChannel.unsubscribe  s    T--x8r   c                     t        | j                  t        | j                        t	        j
                  |      ||      S r	   )rG  rJ  r  r  r   r  r'   rS  r|   rH   s       r   unary_unaryzChannel.unary_unary  s:     (MM;D<L<LMNN6"$68MO 	Or   c                     | j                   r+t        | j                  t        j                  |      ||      S t        | j                  t        | j                        t        j                  |      ||      S r	   )r  ro  rJ  r   r  ry  r  r  r  s       r   unary_streamzChannel.unary_stream  sh     --:w~~f57I%' ' -01A1ABv&(:%	' 'r   c                     t        | j                  t        | j                        t	        j
                  |      ||      S r	   )r~  rJ  r  r  r   r  r  s       r   stream_unaryzChannel.stream_unary  s:     )MM;D<L<LMNN6"$68MO 	Or   c                     t        | j                  t        | j                        t	        j
                  |      ||      S r	   )r  rJ  r  r  r   r  r  s       r   stream_streamzChannel.stream_stream  s:     *MM;D<L<LMNN6"$68MO 	Or   c                     | j                   }|r%|j                  5  |j                  d d = d d d        y y # 1 sw Y   y xY wr	   )r  r  r  r   s     r   _unsubscribe_allzChannel._unsubscribe_all  sD    (( :66q9: : : :s   5>c                     | j                          | j                  j                  t        j                  j
                  d       t        j                  |        t        j                  rt        j                          y y )NzChannel closed!)	r  rJ  r  r   r?   r#   fork_unregister_channelr  gevent_decrement_channel_countr*   s    r   _closezChannel._close  sW    F--779JK&&t,$$113 %r   c                     | j                          | j                  j                  t        j                  j
                  d       y )NzChannel closed due to fork)r  rJ  close_on_forkr   r?   r#   r*   s    r   _close_on_forkzChannel._close_on_fork  s2    ##F$5$5$?$?$@	Br   c                     | S r	   r0   r*   s    r   	__enter__zChannel.__enter__  r   r   c                 $    | j                          yr   r  )r'   exc_typeexc_valexc_tbs       r   __exit__zChannel.__exit__  s    r   c                 $    | j                          y r	   r  r*   s    r   r  zChannel.close  s    r   c                 4    	 | j                          y #  Y y xY wr	   )r  r*   s    r   r   zChannel.__del__"  s    	!!#	 s    r	   )NN)r-   r.   r/   r   r(   r  r  r  r  r  r
  r  r  r  r  r  r  r  r   r0   r   r   r  r    su    A4,:G9
 (,*.O )-+/'* )-+/O *.,0O:4B
r   r  )Tr   r   ro   rQ   osr   r   r
   r>   r   r   r   grpc._cythonr   grpc.experimental	getLoggerr-   re   r   __version__r  rl   getenvr  r9   send_initial_metadatar^   rr   r:   r;   rA   rY  rt  r  r  r  r   r   r   r   objectr   r4   rL   rY   r   r   r   CallFuturer   
RpcContextr   r   r  r,  r0  r8  r?  rE  UnaryUnaryMultiCallablerG  UnaryStreamMultiCallablero  ry  StreamUnaryMultiCallabler~  StreamStreamMultiCallabler  intrV  r  r  r  r  r  r  r  r  r  r  r  r  r  r0   r   r   <module>r0     s]   5    	 
      !  
'

H
%%%&6&B&BC )2		'))04)5 % ..
%%
//
11
((
11  ..
%%
//
11
11  ..
11
((
11	  ..
11
11  7 1" 
#& >&
/ /B% F*K\.Ktyy$++ K\[3$-- [3|BBTYY BBJn{DIIt{{ nb	2'	+	3^6t;; ^6B/Pd.K.K /Pd26 = = 26jSO = = SOl*O!?!? *OZC $ & (&R   &$(	":JH(	({dll {r   