B ㊇cl9@s ddlZddlZddlZddlmZddlmZmZmZm Z m Z e e Z deee e giZddZddZd%d d Zd&d d Zd'd dZddZddZddZGdddZGdddZGdddeZGdddeZGdddeZGdd d eZGd!d"d"eZGd#d$d$eZdS)(N)crc32) ChecksumErrorConnectionClosedErrorConnectionErrorEndpointConnectionErrorReadTimeoutErrorZGENERAL_CONNECTION_ERRORcCs<|dkrt}n|dkr(td||||d}|S)a1Calculate time to sleep based on exponential function. The format is:: base * growth_factor ^ (attempts - 1) If ``base`` is set to 'rand' then a random number between 0 and 1 will be used as the base. Base must be greater than 0, otherwise a ValueError will be raised. Zrandrz.The 'base' param must be greater than 0, got: )random ValueError)base growth_factorattemptsZ time_to_sleeprp/private/var/folders/8c/hx9_v10d5x38qmnzt13b7b8j1k3n5b/T/pip-target-x6xd5gna/lib/python/botocore/retryhandler.pydelay_exponential+s   rcCstjt||dS)zCreate an exponential delay function based on the attempts. This is used so that you only have to pass it the attempts parameter to calculate the delay. )r r ) functoolspartialr)r r rrr!create_exponential_delay_functionBsrcCs$t||d}t||d}t||dS)N)operation_name)checkeraction) create_checker_from_retry_configcreate_retry_action_from_config RetryHandler)configrrrrrrcreate_retry_handlerNs   rcCs0|dd}|ddkr,t|d|ddSdS)N __default__delaytypeZ exponentialr r )r r )r)rrZ delay_configrrrrXs   rc Csg}d}g}d|krn|ddg}|dd}x<|D]4}||}|t|t|}|dk r6||q6W|dk r||dk r||d} x<| D]4}|t| |t| |}|dk r||qWt|dkrt|d|dSt|} t| |t|dSdS)Nrpolicies max_attemptsrr)r )r retryable_exceptions) getappend_create_single_checker_extract_retryable_exceptionextendlenMaxAttemptsDecorator MultiCheckertuple) rrcheckersr r!rkeyZcurrent_configZretry_exceptionZoperation_policiesZ multi_checkerrrrres6      rcCs2d|dkrt|ddSd|dkr.tSdS)Nresponse applies_when socket_errors)_create_single_response_checkerExceptionRaiser)rrrrr$s   r$cCsZd|krt|d|dd}n8d|kr6t|dd}n d|krNt|dd}ntd|S)NZservice_error_codeZhttp_status_code) status_code error_code)r2 crc32body)headerzUnknown retry policy)ServiceErrorCodeCheckerHTTPStatusCodeChecker CRC32Checkerr )r-rrrrr0sr0cCsR|d}d|dikrtgSd|krNg}x|dD]}|t|q4W|SdS)Nr.r4r-r/)r"rr& EXCEPTION_MAP)rr. exceptionsnamerrrr%sr%c@s eZdZdZddZddZdS)raRetry handler. The retry handler takes two params, ``checker`` object and an ``action`` object. The ``checker`` object must be a callable object and based on a response and an attempt number, determines whether or not sufficient criteria for a retry has been met. If this is the case then the ``action`` object (which also is a callable) determines what needs to happen in the event of a retry. cCs||_||_dS)N)_checker_action)selfrrrrr__init__szRetryHandler.__init__cKsn|||d}t|jtr8|ddd}|d|i|jf|r`|j|d}td||Stdd S) zHandler for a retry. Intended to be hooked up to an event handler (hence the **kwargs), this will process retries appropriately. )attempt_numberr-caught_exceptionZ request_dictcontextretriesretries_context)r zRetry needed, action of: %szNo retry needed.N) isinstancer<r(r"updater=loggerdebug)r>r r-rAkwargsZchecker_kwargsrDresultrrr__call__s    zRetryHandler.__call__N)__name__ __module__ __qualname____doc__r?rKrrrrrs rc@s(eZdZdZddZddZddZdS) BaseCheckerzBase class for retry checkers. Each class is responsible for checking a single criteria that determines whether or not a retry should not happen. cCs4|dk r|||S|dk r(|||StddS)aDetermine if retry criteria matches. Note that either ``response`` is not None and ``caught_exception`` is None or ``response`` is None and ``caught_exception`` is not None. :type attempt_number: int :param attempt_number: The total number of times we've attempted to send the request. :param response: The HTTP response (if one was received). :type caught_exception: Exception :param caught_exception: Any exception that was caught while trying to send the HTTP response. :return: True, if the retry criteria matches (and therefore a retry should occur. False if the criteria does not match. Nz,Both response and caught_exception are None.)_check_response_check_caught_exceptionr )r>r@r-rArrrrKs  zBaseChecker.__call__cCsdS)Nr)r>r@r-rrrrQszBaseChecker._check_responsecCsdS)Nr)r>r@rArrrrRsz#BaseChecker._check_caught_exceptionN)rLrMrNrOrKrQrRrrrrrPsrPc@s*eZdZdZd ddZddZddZdS) r(aAllow retries up to a maximum number of attempts. This will pass through calls to the decorated retry checker, provided that the number of attempts does not exceed max_attempts. It will also catch any retryable_exceptions passed in. Once max_attempts has been exceeded, then False will be returned or the retryable_exceptions that was previously being caught will be raised. NcCs||_||_||_dS)N)r< _max_attempts_retryable_exceptions)r>rr r!rrrr?szMaxAttemptsDecorator.__init__cCsz|rt|dd|j|d<||||}|rr||jkrl|dk r\d|dkr\d|ddd<td|dS|SndSdS) NmaxrZResponseMetadatarTZMaxAttemptsReachedz0Reached the maximum number of retry attempts: %sF)rUr"rS _should_retryrGrH)r>r@r-rArDZ should_retryrrrrKs  zMaxAttemptsDecorator.__call__c Csh|jrV||jkrVy||||S|jk rR}ztjd|dddSd}~XYqdXn||||SdS)Nz,retry needed, retryable exception caught: %sT)exc_info)rTrSr<rGrH)r>r@r-rAerrrrV0s z"MaxAttemptsDecorator._should_retry)N)rLrMrNrOr?rKrVrrrrr(s  r(c@seZdZddZddZdS)r7cCs ||_dS)N) _status_code)r>r2rrrr?DszHTTPStatusCodeChecker.__init__cCs*|dj|jkr"td|jdSdSdS)Nrz5retry needed: retryable HTTP status code received: %sTF)r2rYrGrH)r>r@r-rrrrQGs z%HTTPStatusCodeChecker._check_responseN)rLrMrNr?rQrrrrr7Csr7c@seZdZddZddZdS)r6cCs||_||_dS)N)rY _error_code)r>r2r3rrrr?Ssz ServiceErrorCodeChecker.__init__cCsJ|dj|jkrF|ddid}||jkrFtd|j|jdSdS)NrrErrorZCodez>retry needed: matching HTTP status and error code seen: %s, %sTF)r2rYr"rZrGrH)r>r@r-Zactual_error_coderrrrQWs z'ServiceErrorCodeChecker._check_responseN)rLrMrNr?rQrrrrr6Rsr6c@seZdZddZddZdS)r)cCs ||_dS)N) _checkers)r>r+rrrr?fszMultiChecker.__init__cCs(x"|jD]}||||}|r|SqWdS)NF)r\)r>r@r-rArZchecker_responserrrrKis   zMultiChecker.__call__N)rLrMrNr?rKrrrrr)esr)c@seZdZddZddZdS)r8cCs ||_dS)N) _header_name)r>r5rrrr?tszCRC32Checker.__init__cCst|d}|j|j}|dkr.td|jnBt|djd@}|t|ksptdt||tdt||ddS)Nrz?crc32 check skipped, the %s header is not in the http response.lz>retry needed: crc32 check failed, expected != actual: %s != %sr)Z checksum_typeZexpected_checksumZactual_checksum) headersr"r]rGrHrcontentintr)r>r@r- http_responseZ expected_crcZ actual_crc32rrrrQxs   zCRC32Checker._check_responseN)rLrMrNr?rQrrrrr8ssr8c@seZdZdZddZdS)r1z`Raise any caught exceptions. This class will raise any non None ``caught_exception``. cCs|dS)Nr)r>r@rArrrrRsz'ExceptionRaiser._check_caught_exceptionN)rLrMrNrOrRrrrrr1sr1)N)N)N)rloggingr binasciirZbotocore.exceptionsrrrrr getLoggerrLrGr9rrrrrr$r0r%rrPr(r7r6r)r8r1rrrrs4   "  ).?