B 䊇cN@s<dZddlZddlZddlmZmZmZmZddlm Z m Z ddl m Z m Z dZeeZefddZGd d d ZGd d d ZGd ddZGdddZGddde ZGddde ZGddde ZGddde ZGddde ZGdddZGdddZGdd d e ZGd!d"d"e ZGd#d$d$Z dS)%aCStandard 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)ConnectionErrorConnectTimeoutErrorHTTPClientErrorReadTimeoutError)quotaspecial)BaseRetryableCheckerBaseRetryBackoffcCs|tt}|jjj}|}|jjd||j t t t |dt dt|d}d|}|jjjd||j|d|S)Nz after-call.) max_attempts) retry_checker retry_backoff) retry_policyretry_event_adapter retry_quotazretry-config-%szneeds-retry.%s) unique_id)RetryQuotaCheckerrZ RetryQuotametaZ service_model service_idZ hyphenizeeventsregisterrelease_retry_quota RetryHandler RetryPolicyStandardRetryConditionsExponentialBackoffRetryEventAdapter needs_retry)clientr rrZservice_event_namehandlerrr t/private/var/folders/8c/hx9_v10d5x38qmnzt13b7b8j1k3n5b/T/pip-target-x6xd5gna/lib/python/botocore/retries/standard.pyregister_retry_handler*s"  r"c@s eZdZdZddZddZdS)rzBridge between botocore's event system and this module. This class is intended to be hooked to botocore's event system as an event handler. cCs||_||_||_dS)N) _retry_policy_retry_event_adapter _retry_quota)selfrrrr r r!__init__LszRetryHandler.__init__cKsjd}|jjf|}|j|rP|j|rD|j|}td|qZtdn td|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.zNot retrying request.) r$create_retry_contextr# should_retryr%acquire_retry_quotacompute_retry_delayloggerdebug!adapt_retry_response_from_context)r&kwargsZ retry_delaycontextr r r!rQs     zRetryHandler.needs_retryN)__name__ __module__ __qualname____doc__r'rr r r r!rEsrc@s eZdZdZddZddZdS)ra{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. cKsN|d}|dkrd}d}n|\}}t|d|d|||d|ddd}|S) z+Create context based on needs-retry kwargs.responseNZattemptsZ operationcaught_exceptionZ request_dictr0)attempt_numberoperation_model http_responseparsed_responser6request_context) RetryContext)r&r/r5r9r:r0r r r!r(tsz&RetryEventAdapter.create_retry_contextcCs*|}|jdk r&|jdi|dS)z/Modify response back to user back from context.NZResponseMetadata)get_retry_metadatar: setdefaultupdate)r&r0metadatar r r!r.s z3RetryEventAdapter.adapt_retry_response_from_contextN)r1r2r3r4r(r.r r r r!ris rc@s2eZdZdZd ddZddZddZd d ZdS) r<aNormalize 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. NcCs:||_||_||_||_||_|dkr*i}||_i|_dS)N)r7r8r:r9r6r;_retry_metadata)r&r7r8r:r9r6r;r r r!r's zRetryContext.__init__cCs4|jdkrdS|jdi}t|ts*dS|dS)zCheck if there was a parsed response with an error code. If we could not find any error codes, ``None`` is returned. NErrorZCode)r:get isinstancedict)r&errorr r r!get_error_codes   zRetryContext.get_error_codecKs|jjf|dS)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)rAr?)r&r/r r r!add_retry_metadatas zRetryContext.add_retry_metadatacCs |jS)N)rAcopy)r&r r r!r=szRetryContext.get_retry_metadata)NNNNN)r1r2r3r4r'rGrHr=r r r r!r<s   r<c@s$eZdZddZddZddZdS)rcCs||_||_dS)N)_retry_checker_retry_backoff)r&r r r r r!r'szRetryPolicy.__init__cCs |j|S)N)rJ is_retryable)r&r0r r r!r)szRetryPolicy.should_retrycCs |j|S)N)rK delay_amount)r&r0r r r!r+szRetryPolicy.compute_retry_delayN)r1r2r3r'r)r+r r r r!rsrc@s,eZdZdZdZdejfddZddZdS)rcCs|j|_||_||_dS)N)_BASE_base _max_backoff_random)r&Z max_backoffrandomr r r!r'szExponentialBackoff.__init__cCs t||j|jd|jS)aCalculates 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). )minrSrQr7rR)r&r0r r r!rMszExponentialBackoff.delay_amountN)r1r2r3rPZ _MAX_BACKOFFrTr'rMr r r r!rsrc@seZdZddZddZdS)MaxAttemptsCheckercCs ||_dS)N) _max_attempts)r&r r r r!r'szMaxAttemptsChecker.__init__cCsV|j|jk}|jd}|r4t|dd|j|d<|sRtd|j|jdd|S)NretriesmaxrzMax attempts of %s reached.T)ZMaxAttemptsReached)r7rXr;rCrZr,r-rH)r&r0Zunder_max_attemptsZretries_contextr r r!rLs   zMaxAttemptsChecker.is_retryableN)r1r2r3r'rLr r r r!rWsrWc@s<eZdZdddgZddddgZeefZd d d Zd d Z dS)TransientRetryableCheckerZRequestTimeoutZRequestTimeoutExceptionPriorRequestNotCompleteiiiiNcCsP|dkr|jdd}|dkr,|jdd}|dkr:|j}||_||_||_dS)N)_TRANSIENT_ERROR_CODES_TRANSIENT_STATUS_CODES_TRANSIENT_EXCEPTION_CLS_transient_error_codes_transient_status_codes_transient_exception_cls)r&Ztransient_error_codesZtransient_status_codesZtransient_exception_clsr r r!r'6sz"TransientRetryableChecker.__init__cCsJ||jkrdS|jdk r.|jj|jkr.dS|jdk rFt|j|jSdS)NTF)rGr`r9 status_coderar6rDrb)r&r0r r r!rLFs   z&TransientRetryableChecker.is_retryable)NNN) r1r2r3r]r^rrr_r'rLr r r r!r[*s  r[c@s>eZdZddddddddd d d d d dgZdddZddZdS)ThrottledRetryableCheckerZ ThrottlingZThrottlingExceptionZThrottledExceptionZRequestThrottledExceptionZTooManyRequestsExceptionZ&ProvisionedThroughputExceededExceptionZTransactionInProgressExceptionZRequestLimitExceededZBandwidthLimitExceededZLimitExceededExceptionZRequestThrottledZSlowDownr\ZEC2ThrottledExceptionNcCs |dkr|jdd}||_dS)N)_THROTTLED_ERROR_CODES_throttled_error_codes)r&Zthrottled_error_codesr r r!r'jsz"ThrottledRetryableChecker.__init__cCs||jkS)N)rGrf)r&r0r r r!rLosz&ThrottledRetryableChecker.is_retryable)N)r1r2r3rer'rLr r r r!rdVs  rdc@s eZdZdZddZddZdS)ModeledRetryableCheckerz0Check if an error has been modeled as retryable.cCs t|_dS)N)ModeledRetryErrorDetector_error_detector)r&r r r!r'xsz ModeledRetryableChecker.__init__cCs$|}|dkrdS|j|dk S)NF)rGridetect_error_type)r&r0 error_coder r r!rL{sz$ModeledRetryableChecker.is_retryableN)r1r2r3r4r'rLr r r r!rgusrgc@s eZdZdZdZdZddZdS)rhzsz.OrRetryChecker.is_retryable..)anyry)r&r0r )r0r!rLszOrRetryChecker.is_retryableN)r1r2r3r'rLr r r r!rvsrvc@s@eZdZdZdZdZeefZddZ ddZ dd Z d d Z d S) rrU cCs||_d|_dS)N)_quotaZ_last_amount_acquired)r&rr r r!r'szRetryQuotaChecker.__init__cCsF||r|j}n|j}|j|}|r6||jd<dS|jdddS)Nretry_quota_capacityT)ZRetryQuotaReachedF)_is_timeout_error_TIMEOUT_RETRY_REQUEST _RETRY_COSTracquirer;rH)r&r0capacity_amountsuccessr r r!r*s    z%RetryQuotaChecker.acquire_retry_quotacCst|j|jS)N)rDr6_TIMEOUT_EXCEPTIONS)r&r0r r r!rsz#RetryQuotaChecker._is_timeout_errorcKsZ|dkr dS|j}d|kr&dkrVnn,d|krB|j|jn|d}|j|dS)Ni,r)rcrrelease_NO_RETRY_INCREMENT)r&r0r9r/rcrr r r!rs z%RetryQuotaChecker.release_retry_quotaN) r1r2r3rrrrrrr'r*rrr r r r!rsr)!r4loggingrTZbotocore.exceptionsrrrrZbotocore.retriesrrZbotocore.retries.baserr rx getLoggerr1r,r"rrr<rrrWr[rdrgrhrqrrvrr r r r!s,  $3S , %!