
    G                        d Z ddlZddlZddlmZ ddlZddlZej                  Z	dZ
dZej                  rRddlZej                  Zeej                  j                   j"                  ej                  j                   j$                  gZn@ej                  j                   j"                  ej                  j                   j$                  gZd Z G d de      Z G d	 d
ej.                        Zy)zHThis package facilitates retries for HTTP/REST requests to the registry.    N)nested   g      ?c                 6    t         D ]  }t        | |      s y y)NTF)RETRYABLE_EXCEPTION_TYPES
isinstance)errexception_types     5lib/third_party/containerregistry/transport/retry_.pyShouldRetryr   *   s    1n#~& 2 
    c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	Factoryz'A factory for creating RetryTransports.c                 (    i | _         t        | _        y N)kwargs!DEFAULT_SOURCE_TRANSPORT_CALLABLEsource_transport_callableselfs    r
   __init__zFactory.__init__5   s    DK%FD"r   c                     || _         | S r   )r   )r   r   s     r
   WithSourceTransportCallablez#Factory.WithSourceTransportCallable9   s    %>D"Kr   c                 $    || j                   d<   | S )Nmax_retriesr   )r   r   s     r
   WithMaxRetrieszFactory.WithMaxRetries=   s    !,DKKKr   c                 $    || j                   d<   | S )Nbackoff_factorr   )r   r   s     r
   WithBackoffFactorzFactory.WithBackoffFactorA   s    $2DKK !Kr   c                 $    || j                   d<   | S )Nshould_retry_fnr   )r   r!   s     r
   WithShouldRetryFunctionzFactory.WithShouldRetryFunctionE   s    %4DKK!"Kr   c                 J    t        | j                         fi | j                  S )z;Returns a RetryTransport constructed with the given values.)RetryTransportr   r   r   s    r
   BuildzFactory.BuildI   s    $88:JdkkJJr   N)
__name__
__module____qualname____doc__r   r   r   r   r"   r%    r   r
   r   r   2   s%    /GKr   r   c                   0     e Zd ZdZeeef fd	Zd Z xZ	S )r$   zEA wrapper for the given transport which automatically retries errors.c                 V    t         t        |   |       || _        || _        || _        y r   )superr$   r   _max_retries_backoff_factor_should_retry)r   source_transportr   r   r!   	__class__s        r
   r   zRetryTransport.__init__Q   s-    
 
.$()9:#D)D(Dr   c                 0   d}	 	  | j                   j                  |i |S # t        $ rk}|| j                  k\  s| j	                  |      s t        j                  d|       |dz  }t        j                  | j                  d|z  z         Y d}~d}~ww xY w)a  Does the request, exponentially backing off and retrying as appropriate.

    Backoff is backoff_factor * (2 ^ (retry #)) seconds.
    Args:
      *args: The sequence of positional arguments to forward to the source
        transport.
      **kwargs: The keyword arguments to forward to the source transport.

    Returns:
      The response of the HTTP request, and its contents.
    r   zRetrying after exception %s.   r   N)
r1   request	Exceptionr.   r0   loggingerrortimesleepr/   )r   argsr   retriesr   s        r
   r5   zRetryTransport.request[   s     G
	,t$$,,d=f== d'''t/A/A#/F
4c:1

4''1g:67s   ! 	BA!BB)
r&   r'   r(   r)   DEFAULT_MAX_RETRIESDEFAULT_BACKOFF_FACTORr   r   r5   __classcell__)r2   s   @r
   r$   r$   N   s    M 1 6!,	)r   r$   )r)   r7   r9   containerregistry.transportr   httplib2six.moves.http_clientsixHttpr   r=   r>   PY3builtinsBrokenPipeErrormoveshttp_clientIncompleteReadResponseNotReadyr   r   objectr   NestedTransportr$   r*   r   r
   <module>rN      s    O   .  $,MM !  77,,/	ii**	ii,, 
ii**	ii,,Kf K8$V++ $r   