o
    .&ßa  ã                   @   sr   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ e e¡Z	dd„ Z
G dd„ deƒZG d	d
„ d
eƒZdS )é    N)Úbucket)Ú
throttling)Ústandardc                 C   sz   t  ¡ }tjd| ¡ d�}t jd|d�}t|ƒ}tjt 	¡ d�}t
|||||d�}| jj d|j¡ | jj d|j¡ |S )	Nr   )Ústarting_max_rateÚ
start_timeé   )Úmax_rateÚclock)Úretry_event_adapter)Úrate_adjustorÚrate_clockerÚtoken_bucketÚthrottling_detectorr	   zbefore-sendzneeds-retry)r   ÚClockr   ÚCubicCalculatorÚcurrent_timeÚTokenBucketÚRateClockerr   ÚThrottlingErrorDetectorÚRetryEventAdapterÚClientRateLimiterÚmetaÚeventsÚregisterÚon_sending_requestÚon_receiving_response)Úclientr	   r   r   r   r   Úlimiter© r   ú;/usr/lib/python3/dist-packages/botocore/retries/adaptive.pyÚregister_retry_handler   s.   ÿÿûÿÿr    c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	r   g       @c                 C   s2   || _ || _|| _|| _|| _d| _t ¡ | _d S )NF)	Ú_rate_adjustorÚ_rate_clockerÚ_token_bucketÚ_throttling_detectorÚ_clockÚ_enabledÚ	threadingÚLockÚ_lock)Úselfr   r   r   r   r	   r   r   r   Ú__init__*   s   zClientRateLimiter.__init__c                 K   s   | j r
| j ¡  d S d S ©N)r&   r#   Úacquire)r*   ÚrequestÚkwargsr   r   r   r   4   s   ÿz$ClientRateLimiter.on_sending_requestc                 K   s¶   | j  ¡ }| j ¡ }| j�D | jjdi |¤Žs| j |¡}n!| j	s$|}nt
|| jjƒ}| j ||¡}t d||| jj¡ d| _	t
|| j| ƒ| j_W d   ƒ d S 1 sTw   Y  d S )NzfThrottling response received, new send rate: %s measured rate: %s, token bucket capacity available: %sTr   )r"   Úrecordr%   r   r)   r$   Úis_throttling_errorr!   Úsuccess_receivedr&   Úminr#   r   Úerror_receivedÚloggerÚdebugÚavailable_capacityÚ_MAX_RATE_ADJUST_SCALE)r*   r/   Úmeasured_rateÚ	timestampÚnew_rateÚrate_to_user   r   r   r   9   s(   

ÿý

ÿ"ñz'ClientRateLimiter.on_receiving_responseN)Ú__name__Ú
__module__Ú__qualname__r8   r+   r   r   r   r   r   r   r   &   s
    
r   c                   @   s<   e Zd ZdZdZdZeefdd„Zddd„Zed	d
„ ƒZ	dS )r   z7Tracks the rate at which a client is sending a request.gš™™™™™é?g      à?c                 C   sD   || _ d| _|| _t | j  ¡ ¡| _d| j | _d| _	t
 ¡ | _d S )Nr   r   )r%   Ú_measured_rateÚ
_smoothingÚmathÚfloorr   Ú_last_bucketÚ_TIME_BUCKET_RANGEÚ_time_bucket_scaleÚ_countr'   r(   r)   )r*   r	   Ú	smoothingÚtime_bucket_ranger   r   r   r+   V   s   zRateClocker.__init__r   c                 C   s¤   | j �E | j ¡ }t || j ¡| j }|  j|7  _|| jkr>| jt|| j ƒ }|| j	 | j
d| j	   | _
d| _|| _| j
W  d   ƒ S 1 sKw   Y  d S )Nr   r   )r)   r%   r   rB   rC   rF   rG   rD   ÚfloatrA   r@   )r*   ÚamountÚtr   Úcurrent_rater   r   r   r0   `   s(   
ÿÿ
ÿÿÿ$òzRateClocker.recordc                 C   s   | j S r,   )r@   )r*   r   r   r   r9   q   s   zRateClocker.measured_rateN)r   )
r=   r>   r?   Ú__doc__Ú_DEFAULT_SMOOTHINGrE   r+   r0   Úpropertyr9   r   r   r   r   r   O   s    
ÿ

r   )rB   Úloggingr'   Úbotocore.retriesr   r   r   Ú	getLoggerr=   r5   r    Úobjectr   r   r   r   r   r   Ú<module>   s    
)