
    0j              	          d Z ddlZddlZddlZddlmZmZmZmZm	Z	  e	d          Z
 ej        e          Zed         Z G d d          Zdded	ed
ee         defdZddeg ee
         f         dee         de
fdZdS )z[
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License.
    N)	AwaitableCallableLiteralOptionalTypeVarT)nonefullequaldecorrelatedc                   H    e Zd Z	 	 	 	 	 	 ddeded	ed
edee         defdZdS )RetryOptions         ?      >@r
   N   max_attemptsdelay	max_delayjitter_typeprevious_delayattempt_numberc                 Z    || _         || _        || _        || _        || _        || _        d S Nr   r   r   r   r   r   )selfr   r   r   r   r   r   s          h/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/microsoft_teams/apps/utils/retry.py__init__zRetryOptions.__init__   s8     )
"'2,,    )r   r   r   r
   Nr   )__name__
__module____qualname__intfloat
JitterTyper   r    r   r   r   r      s         "(*.- -- - 	-
  - !- - - - - - -r   r   r   r   r   returnc                     |dk    r| S |dk    rt          j        d|           S |dk    rt          j        | dz  |           S |dk    r/| dz  }||}t          j        |t          | d	|z                      S | S )
z>Apply jitter to the delay to prevent thundering herd problems.r	   r
   r   r      r      N   )randomuniformmin)r   r   r   
base_delays       r   _apply_jitterr0   %   s    f			~a'''			~eai///		&	&QY
!'N~j#eQ5G*H*HIIIr   factoryoptionsc                   K   |pt                      }|j        }|j        }|j        }|j        }|j        }|j        }	  |              d {V S # t          j        t          f$ r t                              d            t          $ r}|dk    r|d|dz
  z  z  }	t          |	|          }
t          |
||          }t                              d|dd| d           t          j        |           d {V  t                              d           t!          | t          |dz
  |||||dz   	                     d {V cY d }~S t                              d
|            d }~ww xY w)Nz0Operation cancelled or interrupted, not retryingr   r)   z	Delaying z.2fzs before retry (attempt z)...zRetrying...r   zFinal attempt failed.)exc_info)r   r   r   r   r   r   r   asyncioCancelledErrorKeyboardInterruptloggerdebug	Exceptionr.   r0   sleepretryerror)r1   r2   r   r/   r   r   r   r   errexponential_delaycapped_delayjittered_delays               r   r<   r<   9   s     'G'LJ!I%1K+N+N!WYY"$56   GHHH   ! *aNQ4F.G H 0)<<L +<nUUNLLe^eeeQ_eeefff-/////////LL'''!-!1$' +#1#1A#5  
 
 
 
 
 
 
 
 
 
 
 
 
 
 	,s;;;7s$   A :E&B6E!>E&E!!E&r   )__doc__r5   loggingr,   typingr   r   r   r   r   r   	getLoggerr    r8   r%   r   r$   r0   r<   r&   r   r   <module>rF      s1   
    B B B B B B B B B B B B B BGCLL		8	$	$ <=
- - - - - - - -$  Z RW di    (* *"il"23 *h|>T *`a * * * * * *r   