o .&aL@sZdZddlZddlZddlmZmZddlmZmZddlm Z ddlm Z ddl m Z m Z dZeeZefd d ZGd d d eZGd ddeZGdddeZGdddeZGddde ZGddde ZGddde ZGddde ZGddde ZGdddeZGdd d eZGd!d"d"e ZGd#d$d$e Z Gd%d&d&eZ!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)ConnectionErrorHTTPClientError)ReadTimeoutErrorConnectTimeoutError)quota)special)BaseRetryBackoffBaseRetryableCheckercCsztt}|jjj}|}|jjd||j t t t |dt dt|d}d|}|jjjd||j|d|S)Nz after-call.%s) max_attempts) retry_checker retry_backoff) retry_policyretry_event_adapter retry_quotazretry-config-%szneeds-retry.%s) unique_id)RetryQuotaCheckerr RetryQuotameta service_model service_id hyphenizeeventsregisterrelease_retry_quota RetryHandler RetryPolicyStandardRetryConditionsExponentialBackoffRetryEventAdapter needs_retry)clientr rrservice_event_namehandlerrr$;/usr/lib/python3/dist-packages/botocore/retries/standard.pyregister_retry_handler's(    r&c@ 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||_||_||_dSN) _retry_policy_retry_event_adapter _retry_quota)selfrrrr$r$r%__init__Fs zRetryHandler.__init__cKsnd}|jjdi|}|j|r*|j|r$|j|}td|n tdntd|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$) r*create_retry_contextr) should_retryr+acquire_retry_quotacompute_retry_delayloggerdebug!adapt_retry_response_from_context)r,kwargs retry_delaycontextr$r$r%r Ks     zRetryHandler.needs_retryN)__name__ __module__ __qualname____doc__r-r r$r$r$r%r@s rc@r')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}|dur d}d}n|\}}t|d|d|||d|ddd}|S) z+Create context based on needs-retry kwargs.responseNattempts operationcaught_exception request_dictr7)attempt_numberoperation_model http_responseparsed_responser?request_context) RetryContext)r,r5r<rCrDr7r$r$r%r.js z&RetryEventAdapter.create_retry_contextcCs.|}|jdur|jdi|dSdS)z/Modify response back to user back from context.NResponseMetadata)get_retry_metadatarD setdefaultupdate)r,r7metadatar$r$r%r4s  z3RetryEventAdapter.adapt_retry_response_from_contextN)r8r9r:r;r.r4r$r$r$r%r`s rc@s8eZdZdZ   d ddZddZddZd d ZdS) rFaNormalize 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:||_||_||_||_||_|duri}||_i|_dSr()rArBrDrCr?rE_retry_metadata)r,rArBrDrCr?rEr$r$r%r-s zRetryContext.__init__cCs4|jdurdS|jdi}t|tsdS|dS)zCheck if there was a parsed response with an error code. If we could not find any error codes, ``None`` is returned. NErrorCode)rDget isinstancedict)r,errorr$r$r%get_error_codes   zRetryContext.get_error_codecKs|jjdi|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. Nr$)rLrJ)r,r5r$r$r%add_retry_metadatas zRetryContext.add_retry_metadatacCs |jSr()rLcopyr,r$r$r%rH zRetryContext.get_retry_metadata)NNNNN)r8r9r:r;r-rSrTrHr$r$r$r%rFs  rFc@s$eZdZddZddZddZdS)rcCs||_||_dSr()_retry_checker_retry_backoff)r,r r r$r$r%r-s zRetryPolicy.__init__cC |j|Sr()rX is_retryabler,r7r$r$r%r/ zRetryPolicy.should_retrycCrZr()rY delay_amountr\r$r$r%r1r]zRetryPolicy.compute_retry_delayN)r8r9r:r-r/r1r$r$r$r%rs rc@s,eZdZdZdZdejfddZddZdS)rcCs|j|_||_||_dSr()_BASE_base _max_backoff_random)r, max_backoffrandomr$r$r%r-s zExponentialBackoff.__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). )minrdrbrArcr\r$r$r%r^szExponentialBackoff.delay_amountN)r8r9r:ra _MAX_BACKOFFrfr-r^r$r$r$r%rs  rc@eZdZddZddZdS)MaxAttemptsCheckercC ||_dSr() _max_attemptsr,r r$r$r%r-rWzMaxAttemptsChecker.__init__cCs.|j|jk}|std|j|jdd|S)NzMax attempts of %s reached.T)MaxAttemptsReached)rArmr2r3rT)r,r7under_max_attemptsr$r$r%r[ s  zMaxAttemptsChecker.is_retryableNr8r9r:r-r[r$r$r$r%rk rkc@s<eZdZgdZgdZeefZ   dddZddZ dS) TransientRetryableChecker)RequestTimeoutRequestTimeoutExceptionPriorRequestNotComplete)iiiiNcCsP|dur |jdd}|dur|jdd}|dur|j}||_||_||_dSr()_TRANSIENT_ERROR_CODES_TRANSIENT_STATUS_CODES_TRANSIENT_EXCEPTION_CLS_transient_error_codes_transient_status_codes_transient_exception_cls)r,transient_error_codestransient_status_codestransient_exception_clsr$r$r%r-s z"TransientRetryableChecker.__init__cCsJ||jvr dS|jdur|jj|jvrdS|jdur#t|j|jSdS)NTF)rSrzrC status_coder{r?rPr|r\r$r$r%r[,s  z&TransientRetryableChecker.is_retryable)NNN) r8r9r:rwrxrrryr-r[r$r$r$r%rss rsc@s&eZdZgdZdddZddZdS)ThrottledRetryableChecker) ThrottlingThrottlingExceptionThrottledExceptionRequestThrottledExceptionTooManyRequestsException&ProvisionedThroughputExceededExceptionTransactionInProgressExceptionRequestLimitExceededBandwidthLimitExceededLimitExceededExceptionRequestThrottledSlowDownrvEC2ThrottledExceptionNcCs |dur |jdd}||_dSr()_THROTTLED_ERROR_CODES_throttled_error_codes)r,throttled_error_codesr$r$r%r-Ms z"ThrottledRetryableChecker.__init__cCs||jvSr()rSrr\r$r$r%r[Rsz&ThrottledRetryableChecker.is_retryabler()r8r9r:rr-r[r$r$r$r%r9s  rc@r')ModeledRetryableCheckerz0Check if an error has been modeled as retryable.cCs t|_dSr()ModeledRetryErrorDetector_error_detectorrVr$r$r%r-[r]z ModeledRetryableChecker.__init__cCs$|}|dur dS|j|duS)NF)rSrdetect_error_type)r,r7 error_coder$r$r%r[^sz$ModeledRetryableChecker.is_retryableN)r8r9r:r;r-r[r$r$r$r%rXs rc@s eZdZdZdZdZddZdS)rzsz.OrRetryChecker.is_retryable..)anyrr\r$rr%r[szOrRetryChecker.is_retryableNrqr$r$r$r%rrrrc@s@eZdZdZdZdZeefZddZ ddZ dd Z d d Z d S) rrg cCs||_d|_dSr()_quota_last_amount_acquired)r,rr$r$r%r-s zRetryQuotaChecker.__init__cCsF||r |j}n|j}|j|}|r||jd<dS|jdddS)Nretry_quota_capacityT)RetryQuotaReachedF)_is_timeout_error_TIMEOUT_RETRY_REQUEST _RETRY_COSTracquirerErT)r,r7capacity_amountsuccessr$r$r%r0s    z%RetryQuotaChecker.acquire_retry_quotacCst|j|jSr()rPr?_TIMEOUT_EXCEPTIONSr\r$r$r%rsz#RetryQuotaChecker._is_timeout_errorcKsb|durdS|j}d|krdkr/ndSd|vr#|j|jdS|d}|j|dSdS)Ni,r)rrrelease_NO_RETRY_INCREMENT)r,r7rCr5rrr$r$r%rs z%RetryQuotaChecker.release_retry_quotaN) r8r9r:rrrrrrr-r0rrr$r$r$r%rs r)"r;rfloggingbotocore.exceptionsrrrrbotocore.retriesrrbotocore.retries.baserr r getLoggerr8r2r&objectrrrFrrrkrsrrrrrrrr$r$r$r%s2     1L  & $