B -<`=@sddlZddlZGdddeZGdddeZGdddeZGdd d eZGd d d eZGd d d eZ GdddeZ GdddeZ dS)NcseZdZfddZZS)RequestExceededExceptioncs,||_||_d||f}tt||dS)aError when requested amount exceeds what is allowed The request that raised this error should be retried after waiting the time specified by ``retry_time``. :type requested_amt: int :param requested_amt: The originally requested byte amount :type retry_time: float :param retry_time: The length in time to wait to retry for the requested amount zeZdZdddZddZddZdd Zd d Zd d ZdS) LeakyBucketNcCsZt||_||_|dkr t|_t|_||_|dkr@t|_||_ |dkrVt |_ dS)a9A leaky bucket abstraction to limit bandwidth consumption :type rate: int :type rate: The maximum rate to allow. This rate is in terms of bytes per second. :type time_utils: TimeUtils :param time_utils: The time utility to use for interacting with time :type rate_tracker: BandwidthRateTracker :param rate_tracker: Tracks bandwidth consumption :type consumption_scheduler: ConsumptionScheduler :param consumption_scheduler: Schedules consumption retries when necessary N) float _max_raterr threadingLock_lock _rate_trackerBandwidthRateTracker_consumption_schedulerConsumptionScheduler)rZmax_raterZ rate_trackerZconsumption_schedulerr r r rs  zLeakyBucket.__init__c Csb|jR|j}|j|r,||||S|||rH||||n |||SWdQRXdS)acConsume an a requested amount :type amt: int :param amt: The amount of bytes to request to consume :type request_token: RequestToken :param request_token: The token associated to the consumption request that is used to identify the request. So if a RequestExceededException is raised the token should be used in subsequent retry consume() request. :raises RequestExceededException: If the consumption amount would exceed the maximum allocated bandwidth :rtype: int :returns: The amount consumed N) r>rrrA is_scheduled,_release_requested_amt_for_scheduled_request_projected_to_exceed_max_rate!_raise_request_exceeded_exception_release_requested_amt)ramt request_tokentime_nowr r r r-s     zLeakyBucket.consumecCs|j||}||jkS)N)r?get_projected_rater;)rrHrJZprojected_rater r r rE sz)LeakyBucket._projected_to_exceed_max_ratecCs|j||||S)N)rAprocess_scheduled_consumptionrG)rrHrIrJr r r rDsz8LeakyBucket._release_requested_amt_for_scheduled_requestcCs.|t|j}|j|||}t||ddS)N)rr)r:r;rAschedule_consumptionr)rrHrIrJZallocated_timerr r r rFs  z-LeakyBucket._raise_request_exceeded_exceptioncCs|j|||S)N)r?record_consumption_rate)rrHrJr r r rGsz"LeakyBucket._release_requested_amt)NNN) r r rrr-rErDrFrGr r r r r9s r9c@s,eZdZddZddZddZddZd S) rBcCsi|_d|_dS)z*Schedules when to consume a desired amountrN) _tokens_to_scheduled_consumption _total_wait)rr r r r!szConsumptionScheduler.__init__cCs ||jkS)zIndicates if a consumption request has been scheduled :type token: RequestToken :param token: The token associated to the consumption request that is used to identify the request. )rO)rtokenr r r rC&sz!ConsumptionScheduler.is_scheduledcCs&|j|7_|j|d|j|<|jS)aSchedules a wait time to be able to consume an amount :type amt: int :param amt: The amount of bytes scheduled to be consumed :type token: RequestToken :param token: The token associated to the consumption request that is used to identify the request. :type time_to_consume: float :param time_to_consume: The desired time it should take for that specific request amount to be consumed in regardless of previously scheduled consumption requests :rtype: float :returns: The amount of time to wait for the specific request before actually consuming the specified amount. )Z wait_durationtime_to_consume)rPrO)rrHrQrRr r r rM/sz)ConsumptionScheduler.schedule_consumptioncCs&|j|}t|j|dd|_dS)zProcesses a scheduled consumption request that has completed :type token: RequestToken :param token: The token associated to the consumption request that is used to identify the request. rRrN)rOpopmaxrP)rrQZscheduled_retryr r r rLIs z2ConsumptionScheduler.process_scheduled_consumptionN)r r rrrCrMrLr r r r rB s rBc@sBeZdZdddZeddZddZdd Zd d Zd d Z dS)r@皙?cCs||_d|_d|_dS)aTracks the rate of bandwidth consumption :type a: float :param a: The constant to use in calculating the exponentional moving average of the bandwidth rate. Specifically it is used in the following calculation: current_rate = alpha * new_rate + (1 - alpha) * current_rate This value of this constant should be between 0 and 1. N)_alpha _last_time _current_rate)ralphar r r rVs zBandwidthRateTracker.__init__cCs|jdkrdS|jS)zmThe current transfer rate :rtype: float :returns: The current tracked transfer rate Ng)rWrX)rr r r current_ratefs z!BandwidthRateTracker.current_ratecCs|jdkrdS|||S)aZGet the projected rate using a provided amount and time :type amt: int :param amt: The proposed amount to consume :type time_at_consumption: float :param time_at_consumption: The proposed time to consume at :rtype: float :returns: The consumption rate if that amt and time were consumed Ng)rW*_calculate_exponential_moving_average_rate)rrHtime_at_consumptionr r r rKqs z'BandwidthRateTracker.get_projected_ratecCs2|jdkr||_d|_dS||||_||_dS)aRecord the consumption rate based off amount and time point :type amt: int :param amt: The amount that got consumed :type time_at_consumption: float :param time_at_consumption: The time at which the amount was consumed Ng)rWrXr[)rrHr\r r r rNs  z,BandwidthRateTracker.record_consumption_ratecCs"||j}|dkrtdS||S)Nrinf)rWr:)rrHr\Z time_deltar r r _calculate_rates z$BandwidthRateTracker._calculate_ratecCs&|||}|j|d|j|jS)N)r^rVrX)rrHr\Znew_rater r r r[s z?BandwidthRateTracker._calculate_exponential_moving_average_rateN)rU) r r rrpropertyrZrKrNr^r[r r r r r@Us    r@) rr< Exceptionrobjectrrrrr9rBr@r r r r  s%jR5