U
    N8cN                     @   s<  d Z ddlZ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mZ dZeeZefddZG d	d
 d
ZG dd dZG dd dZG dd dZG dd deZG dd deZG dd deZG dd deZG dd deZG dd dZG dd dZG dd  d eZG d!d" d"eZG d#d$ d$Z dS )%aC  Standard retry behavior.

This contains the default standard retry behavior.
It provides consistent behavior with other AWS SDKs.

The key base classes uses for retries:

    * ``BaseRetryableChecker`` - Use to check a specific condition that
    indicates a retry should happen.  This can include things like
    max attempts, HTTP status code checks, error code checks etc.
    * ``RetryBackoff`` - Use to determine how long we should backoff until
    we retry a request.  This is the class that will implement delay such
    as exponential backoff.
    * ``RetryPolicy`` - Main class that determines if a retry should
    happen.  It can combine data from a various BaseRetryableCheckers
    to make a final call as to whether or not a retry should happen.
    It then uses a ``BaseRetryBackoff`` to determine how long to delay.
    * ``RetryHandler`` - The bridge between botocore's event system
    used by endpoint.py to manage retries and the interfaces defined
    in this module.

This allows us to define an API that has minimal coupling to the event
based API used by botocore.

    N)ConnectionErrorConnectTimeoutErrorHTTPClientErrorReadTimeoutError)quotaspecial)BaseRetryableCheckerBaseRetryBackoff   c                 C   s|   t t }| jjj}| }| jjd| |j	 t
tt|dt dt |d}d| }| jjjd| |j|d |S )Nzafter-call.)max_attempts)retry_checkerretry_backoff)retry_policyretry_event_adapterretry_quotazretry-config-%szneeds-retry.%s)	unique_id)RetryQuotaCheckerr   Z
RetryQuotametaZservice_model
service_idZ	hyphenizeeventsregisterrelease_retry_quotaRetryHandlerRetryPolicyStandardRetryConditionsExponentialBackoffRetryEventAdapterneeds_retry)clientr   r   r   Zservice_event_namehandlerr    r    =/tmp/pip-unpacked-wheel-ozje0y8b/botocore/retries/standard.pyregister_retry_handler*   s,    
 	r"   c                   @   s    e Zd ZdZdd Zdd ZdS )r   zBridge between botocore's event system and this module.

    This class is intended to be hooked to botocore's event system
    as an event handler.
    c                 C   s   || _ || _|| _d S N)_retry_policy_retry_event_adapter_retry_quota)selfr   r   r   r    r    r!   __init__L   s    zRetryHandler.__init__c                 K   sj   d}| j jf |}| j|rP| j|rD| j|}td| qZtd n
td | j 	| |S )z.Connect as a handler to the needs-retry event.Nz1Retry needed, retrying request after delay of: %sz;Retry needed but retry quota reached, not retrying request.zNot retrying request.)
r%   create_retry_contextr$   should_retryr&   acquire_retry_quotacompute_retry_delayloggerdebug!adapt_retry_response_from_context)r'   kwargsZretry_delaycontextr    r    r!   r   Q   s    
zRetryHandler.needs_retryN)__name__
__module____qualname____doc__r(   r   r    r    r    r!   r   E   s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )r   a{  Adapter to existing retry interface used in the endpoints layer.

    This existing interface for determining if a retry needs to happen
    is event based and used in ``botocore.endpoint``.  The interface has
    grown organically over the years and could use some cleanup.  This
    adapter converts that interface into the interface used by the
    new retry strategies.

    c                 K   sN   |d }|dkrd}d}n|\}}t |d |d |||d |d d d}|S )	z+Create context based on needs-retry kwargs.responseNattemptsZ	operationcaught_exceptionZrequest_dictr1   )attempt_numberoperation_modelhttp_responseparsed_responser8   request_context)RetryContext)r'   r0   r6   r;   r<   r1   r    r    r!   r)   t   s    
z&RetryEventAdapter.create_retry_contextc                 C   s*   |  }|jdk	r&|jdi | dS )z/Modify response back to user back from context.NZResponseMetadata)get_retry_metadatar<   
setdefaultupdate)r'   r1   metadatar    r    r!   r/      s
    
z3RetryEventAdapter.adapt_retry_response_from_contextN)r2   r3   r4   r5   r)   r/   r    r    r    r!   r   i   s   
r   c                   @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )r>   a  Normalize a response that we use to check if a retry should occur.

    This class smoothes over the different types of responses we may get
    from a service including:

        * A modeled error response from the service that contains a service
          code and error message.
        * A raw HTTP response that doesn't contain service protocol specific
          error keys.
        * An exception received while attempting to retrieve a response.
          This could be a ConnectionError we receive from our HTTP layer which
          could represent that we weren't able to receive a response from
          the service.

    This class guarantees that at least one of the above attributes will be
    non None.

    This class is meant to provide a read-only view into the properties
    associated with a possible retryable response.  None of the properties
    are meant to be modified directly.

    Nc                 C   s:   || _ || _|| _|| _|| _|d kr*i }|| _i | _d S r#   )r9   r:   r<   r;   r8   r=   _retry_metadata)r'   r9   r:   r<   r;   r8   r=   r    r    r!   r(      s    
zRetryContext.__init__c                 C   s4   | j dkrdS | j di }t|ts*dS |dS )zCheck if there was a parsed response with an error code.

        If we could not find any error codes, ``None`` is returned.

        NErrorZCode)r<   get
isinstancedict)r'   errorr    r    r!   get_error_code   s    

zRetryContext.get_error_codec                 K   s   | j jf | dS )zAdd key/value pairs to the retry metadata.

        This allows any objects during the retry process to add
        metadata about any checks/validations that happened.

        This gets added to the response metadata in the retry handler.

        N)rC   rA   )r'   r0   r    r    r!   add_retry_metadata   s    	zRetryContext.add_retry_metadatac                 C   s
   | j  S r#   )rC   copyr'   r    r    r!   r?      s    zRetryContext.get_retry_metadata)NNNNN)r2   r3   r4   r5   r(   rI   rJ   r?   r    r    r    r!   r>      s        
r>   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )r   c                 C   s   || _ || _d S r#   )_retry_checker_retry_backoff)r'   r   r   r    r    r!   r(      s    zRetryPolicy.__init__c                 C   s   | j |S r#   )rM   is_retryabler'   r1   r    r    r!   r*      s    zRetryPolicy.should_retryc                 C   s   | j |S r#   )rN   delay_amountrP   r    r    r!   r,      s    zRetryPolicy.compute_retry_delayN)r2   r3   r4   r(   r*   r,   r    r    r    r!   r      s   r   c                   @   s,   e Zd ZdZdZdejfddZdd ZdS )r         c                 C   s   | j | _|| _|| _d S r#   )_BASE_base_max_backoff_random)r'   Zmax_backoffrandomr    r    r!   r(      s    zExponentialBackoff.__init__c                 C   s    t |  | j|jd   | jS )a  Calculates delay based on exponential backoff.

        This class implements truncated binary exponential backoff
        with jitter::

            t_i = min(rand(0, 1) * 2 ** attempt, MAX_BACKOFF)

        where ``i`` is the request attempt (0 based).

           )minrW   rU   r9   rV   rP   r    r    r!   rQ     s    zExponentialBackoff.delay_amountN)r2   r3   r4   rT   Z_MAX_BACKOFFrX   r(   rQ   r    r    r    r!   r      s   r   c                   @   s   e Zd Zdd Zdd ZdS )MaxAttemptsCheckerc                 C   s
   || _ d S r#   )_max_attemptsr'   r   r    r    r!   r(     s    zMaxAttemptsChecker.__init__c                 C   sV   |j | jk }|jd}|r4t|dd| j|d< |sRtd| j |jdd |S )Nretriesmaxr   zMax attempts of %s reached.T)ZMaxAttemptsReached)r9   r\   r=   rE   r_   r-   r.   rJ   )r'   r1   Zunder_max_attemptsZretries_contextr    r    r!   rO     s    
 zMaxAttemptsChecker.is_retryableNr2   r3   r4   r(   rO   r    r    r    r!   r[     s   r[   c                   @   s<   e Zd ZdddgZddddgZeefZdd	d
Zdd Z	dS )TransientRetryableCheckerZRequestTimeoutZRequestTimeoutExceptionPriorRequestNotCompletei  i  i  i  Nc                 C   sP   |d kr| j d d  }|d kr,| jd d  }|d kr:| j}|| _|| _|| _d S r#   )_TRANSIENT_ERROR_CODES_TRANSIENT_STATUS_CODES_TRANSIENT_EXCEPTION_CLS_transient_error_codes_transient_status_codes_transient_exception_cls)r'   Ztransient_error_codesZtransient_status_codesZtransient_exception_clsr    r    r!   r(   6  s    z"TransientRetryableChecker.__init__c                 C   sJ   |  | jkrdS |jd k	r.|jj| jkr.dS |jd k	rFt|j| jS dS )NTF)rI   rf   r;   status_coderg   r8   rF   rh   rP   r    r    r!   rO   F  s    

 z&TransientRetryableChecker.is_retryable)NNN)
r2   r3   r4   rc   rd   r   r   re   r(   rO   r    r    r    r!   ra   *  s      
ra   c                   @   s>   e Zd Zddddddddd	d
ddddgZdddZdd ZdS )ThrottledRetryableCheckerZ
ThrottlingZThrottlingExceptionZThrottledExceptionZRequestThrottledExceptionZTooManyRequestsExceptionZ&ProvisionedThroughputExceededExceptionZTransactionInProgressExceptionZRequestLimitExceededZBandwidthLimitExceededZLimitExceededExceptionZRequestThrottledZSlowDownrb   ZEC2ThrottledExceptionNc                 C   s    |d kr| j d d  }|| _d S r#   )_THROTTLED_ERROR_CODES_throttled_error_codes)r'   Zthrottled_error_codesr    r    r!   r(   j  s    z"ThrottledRetryableChecker.__init__c                 C   s   |  | jkS r#   )rI   rl   rP   r    r    r!   rO   o  s    z&ThrottledRetryableChecker.is_retryable)N)r2   r3   r4   rk   r(   rO   r    r    r    r!   rj   V  s"   
rj   c                   @   s    e Zd ZdZdd Zdd ZdS )ModeledRetryableCheckerz0Check if an error has been modeled as retryable.c                 C   s   t  | _d S r#   )ModeledRetryErrorDetector_error_detectorrL   r    r    r!   r(   x  s    z ModeledRetryableChecker.__init__c                 C   s$   |  }|d krdS | j|d k	S )NF)rI   ro   detect_error_type)r'   r1   
error_coder    r    r!   rO   {  s    z$ModeledRetryableChecker.is_retryableN)r2   r3   r4   r5   r(   rO   r    r    r    r!   rm   u  s   rm   c                   @   s    e Zd ZdZdZdZdd ZdS )rn   z<Checks whether or not an error is a modeled retryable error.TRANSIENT_ERRORTHROTTLING_ERRORc                 C   s   |  }|j}|dks|js dS |jD ]Z}|jddk	r&|jdi dpR|j}||kr&|jd drv| j  S | j  S q&dS )aC  Detect the error type associated with an error code and model.

        This will either return:

            * ``self.TRANSIENT_ERROR`` - If the error is a transient error
            * ``self.THROTTLING_ERROR`` - If the error is a throttling error
            * ``None`` - If the error is neither type of error.

        NZ	retryablerH   codeZ
throttling)rI   r:   Zerror_shapesrB   rE   namers   rr   )r'   r1   rq   Zop_modelshapeZerror_code_to_checkr    r    r!   rp     s    


z+ModeledRetryErrorDetector.detect_error_typeN)r2   r3   r4   r5   rr   rs   rp   r    r    r    r!   rn     s   rn   c                   @   s   e Zd Zdd Zdd ZdS )ThrottlingErrorDetectorc                 C   s   t  | _t | _|| _d S r#   )rn   _modeled_error_detectorrj   _fixed_error_code_detectorr%   )r'   r   r    r    r!   r(     s    z ThrottlingErrorDetector.__init__c                 K   s6   | j jf |}| j|rdS | j|}|| jjkS )NT)r%   r)   ry   rO   rx   rp   rs   )r'   r0   r1   Z
error_typer    r    r!   is_throttling_error  s
    z+ThrottlingErrorDetector.is_throttling_errorN)r2   r3   r4   r(   rz   r    r    r    r!   rw     s   rw   c                   @   s$   e Zd ZdZefddZdd ZdS )r   zConcrete class that implements the standard retry policy checks.

    Specifically:

        not max_attempts and (transient or throttled or modeled_retry)

    c                 C   s6   t || _tt t t tt t gg| _	d S r#   )
r[   _max_attempts_checkerOrRetryCheckerra   rj   rm   r   ZRetryIDPCommunicationErrorZRetryDDBChecksumError_additional_checkersr]   r    r    r!   r(     s    
z StandardRetryConditions.__init__c                 C   s   | j |o| j|S r#   )r{   rO   r}   rP   r    r    r!   rO     s
    
z$StandardRetryConditions.is_retryableN)r2   r3   r4   r5   DEFAULT_MAX_ATTEMPTSr(   rO   r    r    r    r!   r     s   r   c                   @   s   e Zd Zdd Zdd ZdS )r|   c                 C   s
   || _ d S r#   )	_checkers)r'   Zcheckersr    r    r!   r(     s    zOrRetryChecker.__init__c                    s   t  fdd| jD S )Nc                 3   s   | ]}|  V  qd S r#   )rO   ).0checkerr1   r    r!   	<genexpr>  s     z.OrRetryChecker.is_retryable.<locals>.<genexpr>)anyr   rP   r    r   r!   rO     s    zOrRetryChecker.is_retryableNr`   r    r    r    r!   r|     s   r|   c                   @   s@   e Zd ZdZdZdZeefZdd Z	dd Z
dd	 Zd
d ZdS )r      rY   
   c                 C   s   || _ d | _d S r#   )_quotaZ_last_amount_acquired)r'   r   r    r    r!   r(     s    zRetryQuotaChecker.__init__c                 C   sF   |  |r| j}n| j}| j|}|r6||jd< dS |jdd dS )Nretry_quota_capacityT)ZRetryQuotaReachedF)_is_timeout_error_TIMEOUT_RETRY_REQUEST_RETRY_COSTr   acquirer=   rJ   )r'   r1   capacity_amountsuccessr    r    r!   r+     s    

z%RetryQuotaChecker.acquire_retry_quotac                 C   s   t |j| jS r#   )rF   r8   _TIMEOUT_EXCEPTIONSrP   r    r    r!   r     s    z#RetryQuotaChecker._is_timeout_errorc                 K   sZ   |d krd S |j }d|  kr&dk rVn n,d|krB| j| j n|d }| j| d S )N   i,  r   )ri   r   release_NO_RETRY_INCREMENT)r'   r1   r;   r0   ri   r   r    r    r!   r     s    	z%RetryQuotaChecker.release_retry_quotaN)r2   r3   r4   r   r   r   r   r   r   r(   r+   r   r   r    r    r    r!   r     s   r   )!r5   loggingrX   Zbotocore.exceptionsr   r   r   r   Zbotocore.retriesr   r   Zbotocore.retries.baser   r	   r~   	getLoggerr2   r-   r"   r   r   r>   r   r   r[   ra   rj   rm   rn   rw   r   r|   r   r    r    r    r!   <module>   s,   
$3S,%!