
    s                         d dl Z d dlmZmZmZmZ d dlmZmZ d dl	m
Z
  ed      ZdedefdZ G d	 d
      Z G d d      Zy)    N)	AwaitableTypeVarOptionalCallable)GoogleAPICallErrorUnknown)wait_ignore_errorsTereturnc                 B    t        | t              r| S t        d| g      S )NzHad an unknown error)errors)
isinstancer   r   )r   s    Klib/third_party/google/cloud/pubsublite/internal/wire/permanent_failable.pyadapt_errorr      s"    !'()1#66    c                   $    e Zd ZdefdZd Zd Zy)_TaskWithCleanupac                 8    t        j                  |      | _        y N)asyncioensure_future_task)selfr   s     r   __init__z_TaskWithCleanup.__init__    s    **1-
r   c                 "   K   | j                   S wr   )r   r   s    r   
__aenter__z_TaskWithCleanup.__aenter__#   s     zzs   c                    K   | j                   j                         s8| j                   j                          t        | j                          d {    y y 7 wr   )r   donecancelr	   )r   exc_typeexc_valexc_tbs       r   	__aexit__z_TaskWithCleanup.__aexit__&   s>     zz JJ$TZZ000 !0s   AAAAN)__name__
__module____qualname__r   r   r   r&    r   r   r   r      s    .) .1r   r   c                       e Zd ZU dZeej                     ed<   d Ze	dej                  fd       Z
dee   defdZdeg ed	   f   fd
ZdefdZdee   fdZy	)PermanentFailablezdA class that can experience permanent failures, with helpers for forwarding these to client actions._maybe_failure_taskc                     d | _         y r   )r-   r   s    r   r   zPermanentFailable.__init__1   s
    #' r   r   c                 d    | j                   t        j                         | _         | j                   S )zaGet the failure task, initializing it lazily, since it needs to be initialized in the event loop.)r-   r   Futurer   s    r   _failure_taskzPermanentFailable._failure_task4   s+     ##+'.~~'7D$'''r   	awaitablec                   K   t        |      4 d{   }| j                  j                         r| j                  j                         t	        j
                  || j                  gt        j                         d{   \  }}||v r| d{   cddd      d{    S | j                  j                         7 7 B7 37 %# 1 d{  7  sw Y   yxY ww)z
        Await the awaitable, unless fail() is called first.
        Args:
          awaitable: An awaitable

        Returns: The result of the awaitable
        Raises: The permanent error if fail() is called or the awaitable raises one.
        N)return_when)r   r1   r!   	exceptionr   waitFIRST_COMPLETED)r   r2   taskr!   _s        r   await_unless_failedz%PermanentFailable.await_unless_failed;   s      $I..$!!&&(((2244#LLt))*8O8O GD! t|!z /.. $$..00 / " /...su   CC CA)C?C CCCC C!C&C CCCCCCCCpoll_actionNc                    K   	 	 | j                   |              d{    7 # t        j                  $ r Y yt        $ r$}| j	                  t        |             Y d}~yd}~ww xY ww)z
        Run a polling loop, which runs poll_action forever unless this is failed.
        Args:
          poll_action: A callable returning an awaitable to run in a loop. Note that async functions which return once
          satisfy this.
        N)r:   r   CancelledError	Exceptionfailr   )r   r;   r   s      r   
run_pollerzPermanentFailable.run_pollerN   s_     	&..{}=== =%% 	 	&IIk!n%%	&s8   A*& $& A'A*A'A"A*"A''A*errc                     | j                   j                         s6| j                   j                  |       | j                   j                          y y r   )r1   r!   set_exceptionr5   )r   rA   s     r   r?   zPermanentFailable.fail]   sA    !!&&(,,S1 ((*	 )r   c                 l    | j                   j                         sy | j                   j                         S r   )r1   r!   r5   r   s    r   errorzPermanentFailable.errord   s,    !!&&(!!++--r   )r'   r(   r)   __doc__r   r   r0   __annotations__r   propertyr1   r   r
   r:   r   r@   r   r?   rE   r*   r   r   r,   r,   ,   s    n!'..11( (w~~ ( (19Q< 1A 1&&HR45H,I &+* +.x 23 .r   r,   )r   typingr   r   r   r   google.api_core.exceptionsr   r   6google.cloud.pubsublite.internal.wait_ignore_cancelledr	   r
   r>   r   r   r,   r*   r   r   <module>rL      sG     9 9 B UCL79 7!3 7
1 
1;. ;.r   