
    Peo                         d dl Z d dlZd dlZd dlmZmZmZ  e j                  e      Z	d Z
 G d d      Z G d d      Zy)    N)bucketstandard
throttlingc                    t        j                         }t        j                  d|j	                               }t        j
                  d|      }t        |      }t        j                  t        j                               }t        |||||      }| j                  j                  j                  d|j                         | 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   limiters          ;/usr/lib/python3/dist-packages/botocore/retries/adaptive.pyregister_retry_handlerr!   
   s    LLNE..(:(:(<M %%q>Lu%L"::$668  #!!/G KK"" KK%% N    c                   "    e Zd ZdZd Zd Zd Zy)r   g       @c                     || _         || _        || _        || _        || _        d| _        t        j                         | _        y )NF)	_rate_adjustor_rate_clocker_token_bucket_throttling_detector_clock_enabled	threadingLock_lock)selfr   r   r   r   r   s         r    __init__zClientRateLimiter.__init__)   s@     ,))$7!^^%
r"   c                 R    | j                   r| j                  j                          y y N)r*   r'   acquire)r.   requestkwargss      r    r   z$ClientRateLimiter.on_sending_request9   s     ==&&( r"   c                 j   | j                   j                         }| j                  j                         }| j                  5   | j
                  j                  di |s| j                  j                  |      }n~| j                  s|}n t        || j                  j                        }| j                  j                  ||      }t        j                  d||| j                  j                          d| _	        t        || j"                  |z        | j                  _        d d d        y # 1 sw Y   y xY w)NzfThrottling response received, new send rate: %s measured rate: %s, token bucket capacity available: %sT )r&   recordr)   r   r-   r(   is_throttling_errorr%   success_receivedr*   minr'   r
   error_receivedloggerdebugavailable_capacity_MAX_RATE_ADJUST_SCALE)r.   r4   measured_rate	timestampnew_raterate_to_uses         r    r   z'ClientRateLimiter.on_receiving_response>   s   **113KK,,.	ZZ 	@4,,@@J6J..??	J}}"/K"%%t'9'9'B'B#K  ..== $ !&&99 !%*-$55E+D'-	 	 	s   CD))D2N)__name__
__module____qualname__r?   r/   r   r   r6   r"   r    r   r   &   s     & )
r"   r   c                   <    e Zd ZdZdZdZeefdZddZed        Z	y)	r   z7Tracks the rate at which a client is sending a request.g?g      ?c                     || _         d| _        || _        t        j                  | j                   j                               | _        d| j                  z  | _        d| _	        t        j                         | _        y )Nr   r	   )r)   _measured_rate
_smoothingmathfloorr   _last_bucket_TIME_BUCKET_RANGE_time_bucket_scale_countr+   r,   r-   )r.   r   	smoothingtime_bucket_ranges       r    r/   zRateClocker.__init__c   s`     # JJt{{'?'?'AB"#d&=&="=^^%
r"   c                     | j                   5  | j                  j                         }t        j                  || j
                  z        | j
                  z  }| xj                  |z  c_        || j                  kD  rd| j                  t        || j                  z
        z  }|| j                  z  | j                  d| j                  z
  z  z   | _
        d| _        || _        | j                  cd d d        S # 1 sw Y   y xY w)Nr	   r   )r-   r)   r   rK   rL   rO   rP   rM   floatrJ   rI   )r.   amounttr   current_rates        r    r7   zRateClocker.recordq   s    ZZ 	'((*A

1t6667))*  KK6!K)))#{{U6D<M<M3M-NN'3doo'E''1t+>?'#  $*!&&	' 	' 	's   CC44C=c                     | j                   S r1   )rI   )r.   s    r    r@   zRateClocker.measured_rate   s    """r"   N)r	   )
rD   rE   rF   __doc___DEFAULT_SMOOTHINGrN   r/   r7   propertyr@   r6   r"   r    r   r   \   s7    A
 %,	&'" # #r"   r   )loggingrK   r+   botocore.retriesr   r   r   	getLoggerrD   r<   r!   r   r   r6   r"   r    <module>r_      s@       9 9			8	$83 3l(# (#r"   