
                             d dl mZmZmZmZmZmZmZ d dlZd dl	m
Z
mZ d dlmZmZmZmZ d dlmZ d dlmZ  ed      Z G d d	eeef   ee   e      Z G d
 deeef         Zy)    )castAsyncIteratorTypeVarOptionalCallableAsyncIterable	AwaitableN)GoogleAPICallErrorFailedPrecondition)
ConnectionRequestResponseConnectionFactory)WorkItem)PermanentFailableTc                        e Zd ZU dZded<   eee      ed<    fdZdee   fdZ	de
d	d
fdZd	efdZd ZddZd	e
fdZd	ee   fdZ xZS )GapicConnectionzCA Connection wrapping a gapic AsyncIterator[Request/Response] pair.z&asyncio.Queue[WorkItem[Request, None]]_write_queue_response_itc                 X    t         |           t        j                  d      | _        y )N   )maxsize)super__init__asyncioQueuer   )self	__class__s    Ilib/third_party/google/cloud/pubsublite/internal/wire/gapic_connection.pyr   zGapicConnection.__init__0   s    #MM!4    response_itc                     || _         y N)r   )r   r"   s     r    set_response_itzGapicConnection.set_response_it4   s
    'r!   requestreturnNc                    K   t        |      }| j                  | j                  j                  |             d {    | j                  |j                         d {    y 7 (7 wr$   )r   await_unless_failedr   putresponse_future)r   r&   items      r    writezGapicConnection.write7   sY      &&t'8'8'<'<T'BCCC&&t';';<<< 	D<s!   9A(A$"A(A&A(&A(c                   K   | j                   *| j                  t        d             | j                         	 t	        t
        t           | j                         }| j                  |j                                d {   S 7 # t        $ r, | j                  t        d             Y | j                         t        $ r*}| j                  |       Y d }~| j                         d }~ww xY ww)Nz GapicConnection not initialized.z"Server sent unprompted half close.)r   failr   errorr   r   r   r)   	__anext__StopAsyncIterationr
   )r   r"   es      r    readzGapicConnection.read<   s     $II()KLM**,	}X68I8IJK11+2G2G2IJJJJ! 	PII()MNO jjl " 	IIaLLjjl	sM   7C-AB =B>B C-B #C*'C-8C* C%C-%C**C-c                    K   | S wr$    r   s    r    
__aenter__zGapicConnection.__aenter__I   s     s   c                    K   y wr$   r6   )r   exc_type	exc_value	tracebacks       r    	__aexit__zGapicConnection.__aexit__L   s	     s   c                    K   | j                  | j                  j                                d {   }|j                  j	                  d        |j
                  S 7 +wr$   )r)   r   getr+   
set_resultr&   )r   r,   s     r    r1   zGapicConnection.__anext__O   sR     .2.F.F!!#/
 )
 	''-||	)
s   -AA,Ac                     | S r$   r6   r7   s    r    	__aiter__zGapicConnection.__aiter__V   s    r!   )r'   N)__name__
__module____qualname____doc____annotations__r   r   r   r   r%   r   r-   r4   r8   r=   r1   rB   __classcell__)r   s   @r    r   r   (   s{     N::=2335(=+B (=7 =t =
H  =2 r!   r   c                   r    e Zd ZdZeee   geee	      f   Z
deee   geee	      f   fdZdeee	f   fdZy)GapicConnectionFactoryz3A ConnectionFactory that produces GapicConnections.producerc                     || _         y r$   )	_producer)r   rK   s     r    r   zGapicConnectionFactory.__init___   s     "r!   r'   c                    K   t        t        t        f          }| j                  |      }| d {   }|j	                  |j                                |S 7 %wr$   )r   r   r   rM   r%   rB   )r   connresponse_futresponse_iterables       r    newzGapicConnectionFactory.newg   sQ     w013~~d+"...88:; /s   .AA&AN)rC   rD   rE   rF   r   r   r   r	   r   r   rM   r   r   rR   r6   r!   r    rJ   rJ   Z   sh    =-019]8=T3UUVI"7#$ih0G&HH
":gx&78 r!   rJ   )typingr   r   r   r   r   r   r	   r   google.api_core.exceptionsr
   r   0google.cloud.pubsublite.internal.wire.connectionr   r   r   r   /google.cloud.pubsublite.internal.wire.work_itemr   8google.cloud.pubsublite.internal.wire.permanent_failabler   r   r   rJ   r6   r!   r    <module>rX      sq       M  E VCL/w !=#9;L/d.w/@A r!   