B 䊇cNã@s<dZddlZddlZddlmZmZmZmZddlm Z m Z ddl m Z m Z dZe e¡Zefdd„ZGd d „d ƒZGd d „d ƒZGd d„dƒZGdd„dƒZGdd„de ƒZGdd„de ƒZGdd„de ƒZGdd„de ƒZGdd„de ƒZGdd„dƒZGdd„dƒZGdd „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)ÚConnectionErrorÚConnectTimeoutErrorÚHTTPClientErrorÚReadTimeoutError)ÚquotaÚspecial)ÚBaseRetryableCheckerÚBaseRetryBackoffécCs|tt ¡ƒ}|jjj}| ¡}|jj d|›�|j ¡t t t |d�t ƒd�tƒ|d�}d|}|jjjd||j|d�|S)Nz after-call.)Ú max_attempts)Ú retry_checkerÚ retry_backoff)Ú retry_policyÚretry_event_adapterÚ retry_quotazretry-config-%szneeds-retry.%s)Ú unique_id)ÚRetryQuotaCheckerrZ RetryQuotaÚmetaZ service_modelÚ service_idZ hyphenizeÚeventsÚregisterÚrelease_retry_quotaÚ RetryHandlerÚ RetryPolicyÚStandardRetryConditionsÚExponentialBackoffÚRetryEventAdapterÚ needs_retry)Úclientr rrZservice_event_nameÚhandlerr©r út/private/var/folders/8c/hx9_v10d5x38qmnzt13b7b8j1k3n5b/T/pip-target-x6xd5gna/lib/python/botocore/retries/standard.pyÚregister_retry_handler*s"  r"c@s eZdZdZdd„Zdd„ZdS)rz™Bridge 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 |¡}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.zNot 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!rQs     zRetryHandler.needs_retryN)Ú__name__Ú __module__Ú __qualname__Ú__doc__r'rr r r r!rEsrc@s eZdZdZdd„Zdd„ZdS)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 operationÚcaught_exceptionZ request_dictr0)Úattempt_numberÚoperation_modelÚ http_responseÚparsed_responser6Úrequest_context)Ú RetryContext)r&r/r5r9r:r0r r r!r(tsz&RetryEventAdapter.create_retry_contextcCs*| ¡}|jdk r&|j di¡ |¡dS)z/Modify response back to user back from context.NZResponseMetadata)Úget_retry_metadatar:Ú setdefaultÚupdate)r&r0Úmetadatar r r!r.�s z3RetryEventAdapter.adapt_retry_response_from_contextN)r1r2r3r4r(r.r r r r!ris rc@s2eZdZdZd dd„Zdd„Zdd„Zd d „ZdS) 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. NcCs:||_||_||_||_||_|dkr*i}||_i|_dS)N)r7r8r:r9r6r;Ú_retry_metadata)r&r7r8r:r9r6r;r r r!r'´s zRetryContext.__init__cCs4|jdkrdS|j di¡}t|tƒs*dS| d¡S)z‡Check if there was a parsed response with an error code. If we could not find any error codes, ``None`` is returned. NÚErrorZCode)r:ÚgetÚ isinstanceÚdict)r&Úerrorr r r!Úget_error_codeÓs   zRetryContext.get_error_codecKs|jjf|ŽdS)zúAdd 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_metadataàs zRetryContext.add_retry_metadatacCs |j ¡S)N)rAÚcopy)r&r r r!r=ëszRetryContext.get_retry_metadata)NNNNN)r1r2r3r4r'rGrHr=r r r r!r<œs   r<c@s$eZdZdd„Zdd„Zdd„ZdS)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!rïsrc@s,eZdZdZdZdejfdd„Zdd„ZdS)réécCs|j|_||_||_dS)N)Ú_BASEÚ_baseÚ _max_backoffÚ_random)r&Z max_backoffÚrandomr r r!r'szExponentialBackoff.__init__cCs t| ¡|j|jd|jƒS)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!rûsrc@seZdZdd„Zdd„ZdS)ÚMaxAttemptsCheckercCs ||_dS)N)Ú _max_attempts)r&r r r r!r'szMaxAttemptsChecker.__init__cCsV|j|jk}|j d¡}|r4t| dd¡|jƒ|d<|sRt d|j¡|jdd�|S)NÚretriesÚmaxrzMax 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)ÚTransientRetryableCheckerZRequestTimeoutZRequestTimeoutExceptionÚPriorRequestNotCompleteiôiöi÷iøNcCsP|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|jƒSdS)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 dgZddd„Zdd„ZdS)Ú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 eZdZdZdd„Zdd„ZdS)Ú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)rGriÚdetect_error_type)r&r0Ú error_coder r r!rL{sz$ModeledRetryableChecker.is_retryableN)r1r2r3r4r'rLr r r r!rgusrgc@s eZdZdZdZdZdd„ZdS)rhzÜsz.OrRetryChecker.is_retryable..)Úanyry)r&r0r )r0r!rLÛszOrRetryChecker.is_retryableN)r1r2r3r'rLr r r r!rv×srvc@s@eZdZdZdZdZeefZdd„Z dd„Z dd „Z d d „Z d S) rérUé cCs||_d|_dS)N)Ú_quotaZ_last_amount_acquired)r&rr r r!r'ëszRetryQuotaChecker.__init__cCsF| |¡r|j}n|j}|j |¡}|r6||jd<dS|jdd�dS)NÚretry_quota_capacityT)ZRetryQuotaReachedF)Ú_is_timeout_errorÚ_TIMEOUT_RETRY_REQUESTÚ _RETRY_COSTr€Úacquirer;rH)r&r0Úcapacity_amountÚsuccessr r r!r*ðs    z%RetryQuotaChecker.acquire_retry_quotacCst|j|jƒS)N)rDr6Ú_TIMEOUT_EXCEPTIONS)r&r0r r r!r‚ÿsz#RetryQuotaChecker._is_timeout_errorcKsZ|dkr dS|j}d|kr&dkrVnn,d|krB|j |j¡n|d}|j |¡dS)NéÈi,r�)rcr€ÚreleaseÚ_NO_RETRY_INCREMENT)r&r0r9r/rcr†r r r!rs z%RetryQuotaChecker.release_retry_quotaN) r1r2r3r„r‹rƒrrrˆr'r*r‚rr r r r!rßsr)!r4ÚloggingrTZbotocore.exceptionsrrrrZbotocore.retriesrrZbotocore.retries.baserr rxÚ getLoggerr1r,r"rrr<rrrWr[rdrgrhrqrrvrr r r r!Ús,  $3S , %!