Current File : //usr/lib/python3/dist-packages/s3transfer/__pycache__/bandwidth.cpython-312.pyc
�

�<�e=���ddlZddlZGd�de�ZGd�d�ZGd�d�ZGd�d	�ZGd
�d�ZGd�d
�ZGd�d�Z	Gd�d�Z
y)�Nc���eZdZ�fd�Z�xZS)�RequestExceededExceptionc�d��||_||_dj||�}t�|�|�y)a�Error 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
        z<Request amount {} exceeded the amount available. Retry in {}N)�
requested_amt�
retry_time�format�super�__init__)�selfrr�msg�	__class__s    ��6/usr/lib/python3/dist-packages/s3transfer/bandwidth.pyr
z!RequestExceededException.__init__s8���+���$���L�S�S��:�
��	�����)�__name__�
__module__�__qualname__r
�
__classcell__)r
s@rrrs
����rrc��eZdZdZy)�RequestTokenzDA token to pass as an identifier when consuming from the LeakyBucketN)rrr�__doc__�rrrr's��N�rrc��eZdZd�Zd�Zy)�	TimeUtilsc�*�tj�S)zgGet the current time back

        :rtype: float
        :returns: The current time in seconds
        )�time�rs rrzTimeUtils.time.s���y�y�{�rc�,�tj|�S)zwSleep for a designated time

        :type value: float
        :param value: The time to sleep for in seconds
        )r�sleep)r�values  rrzTimeUtils.sleep6s���z�z�%� � rN)rrrrrrrrrr-s���!rrc��eZdZdd�Z	dd�Zy)�BandwidthLimiterNc�D�||_||_|�t�|_yy)aLimits bandwidth for shared S3 transfers

        :type leaky_bucket: LeakyBucket
        :param leaky_bucket: The leaky bucket to use limit bandwidth

        :type time_utils: TimeUtils
        :param time_utils: Time utility to use for interacting with time.
        N)�
_leaky_bucket�_time_utilsr)r�leaky_bucket�
time_utilss   rr
zBandwidthLimiter.__init__@s)��*���%�����(�{�D��rc�n�t||j||j�}|s|j�|S)a�Wraps a fileobj in a bandwidth limited stream wrapper

        :type fileobj: file-like obj
        :param fileobj: The file-like obj to wrap

        :type transfer_coordinator: s3transfer.futures.TransferCoordinator
        param transfer_coordinator: The coordinator for the general transfer
            that the wrapped stream is a part of

        :type enabled: boolean
        :param enabled: Whether bandwidth limiting should be enabled to start
        )�BandwidthLimitedStreamr#r$�disable_bandwidth_limiting)r�fileobj�transfer_coordinator�enabled�streams     r�get_bandwith_limited_streamz,BandwidthLimiter.get_bandwith_limited_streamNs:��(��T�'�'�)=�t�?O�?O�
����-�-�/��
r�N)T)rrrr
r.rrrr!r!?s��+�6:�rr!c�\�eZdZ		dd�Zd�Zd�Zd�Zd�Zd�Zd�Z	dd	�Z
d
�Zd�Zd�Z
d
�Zy)r(Nc��||_||_||_||_|�t	�|_d|_t
�|_d|_||_	y)a[Limits bandwidth for reads on a wrapped stream

        :type fileobj: file-like object
        :param fileobj: The file like object to wrap

        :type leaky_bucket: LeakyBucket
        :param leaky_bucket: The leaky bucket to use to throttle reads on
            the stream

        :type transfer_coordinator: s3transfer.futures.TransferCoordinator
        param transfer_coordinator: The coordinator for the general transfer
            that the wrapped stream is a part of

        :type time_utils: TimeUtils
        :param time_utils: The time utility to use for interacting with time
        NTr)
�_fileobjr#�_transfer_coordinatorr$r�_bandwidth_limiting_enabledr�_request_token�_bytes_seen�_bytes_threshold)rr*r%r+r&�bytes_thresholds      rr
zBandwidthLimitedStream.__init__fsU��0 ��
�)���%9��"�%�����(�{�D��+/��(�*�n������ /��rc��d|_y)z0Enable bandwidth limiting on reads to the streamTN�r4rs r�enable_bandwidth_limitingz0BandwidthLimitedStream.enable_bandwidth_limiting�s
��+/��(rc��d|_y)z1Disable bandwidth limiting on reads to the streamFNr:rs rr)z1BandwidthLimitedStream.disable_bandwidth_limiting�s
��+0��(rc�8�|js|jj|�S|xj|z
c_|j|jkr|jj|�S|j�|jj|�S)zhRead a specified amount

        Reads will only be throttled if bandwidth limiting is enabled.
        )r4r2�readr6r7�_consume_through_leaky_bucket)r�amounts  rr>zBandwidthLimitedStream.read�s��
�/�/��=�=�%�%�f�-�-�	
���F�"�����d�3�3�3��=�=�%�%�f�-�-��*�*�,��}�}�!�!�&�)�)rc�r�|jjs9	|jj|j|j
�d|_y|jj�#t$r/}|jj|j�Yd}~nd}~wwxYw|jjs���i)Nr)
r3�	exceptionr#�consumer6r5rr$rr)r�es  rr?z4BandwidthLimitedStream._consume_through_leaky_bucket�s����,�,�6�6�
5��"�"�*�*��$�$�d�&9�&9��$%�� ���,�,�6�6�6��,�
5�� � �&�&�q�|�|�4�4��
5���,�,�6�6�s�7A&�&	B�/%B�Bc�$�|j�y)z6Signal that data being read is being transferred to S3N)r;rs r�signal_transferringz*BandwidthLimitedStream.signal_transferring�s���&�&�(rc�$�|j�y)z:Signal that data being read is not being transferred to S3N)r)rs r�signal_not_transferringz.BandwidthLimitedStream.signal_not_transferring�s���'�'�)rc�<�|jj||�yr/)r2�seek)r�where�whences   rrJzBandwidthLimitedStream.seek�s���
�
���5�&�)rc�6�|jj�Sr/)r2�tellrs rrNzBandwidthLimitedStream.tell�s���}�}�!�!�#�#rc��|jr|jr|j�|jj	�yr/)r4r6r?r2�closers rrPzBandwidthLimitedStream.close�s1���+�+��0@�0@�
�.�.�0��
�
���rc��|Sr/rrs r�	__enter__z BandwidthLimitedStream.__enter__�s���rc�$�|j�yr/)rP)r�args�kwargss   r�__exit__zBandwidthLimitedStream.__exit__�s���
�
�r)Ni)r)rrrr
r;r)r>r?rFrHrJrNrPrRrVrrrr(r(esG���"�
!0�F0�1�*�(7�$)�*�*�$���rr(c�8�eZdZ			dd�Zd�Zd�Zd�Zd�Zd�Zy)	�LeakyBucketNc���t|�|_||_|�t�|_t	j
�|_||_|�t�|_||_	|�t�|_	yy)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_rater$r�	threading�Lock�_lock�
_rate_tracker�BandwidthRateTracker�_consumption_scheduler�ConsumptionScheduler)r�max_rater&�rate_tracker�consumption_schedulers     rr
zLeakyBucket.__init__�sl��.�x����%�����(�{�D���^�^�%��
�)�����!5�!7�D��&;��#� �(�*>�*@�D�'�)rc�l�|j5|jj�}|jj	|�r|j|||�cddd�S|j
||�r|j|||�n|j||�cddd�S	ddd�y#1swYyxYw)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^r$rra�is_scheduled�,_release_requested_amt_for_scheduled_request�_projected_to_exceed_max_rate�!_raise_request_exceeded_exception�_release_requested_amt�r�amt�
request_token�time_nows    rrCzLeakyBucket.consume�s���$�Z�Z�	B��'�'�,�,�.�H��*�*�7�7�
�F��H�H�����	B�	B��3�3�C��B��6�6������2�2�3��A�	B�	B��	B�	B�	B�s�AB*�7B*�*B3c�X�|jj||�}||jkDSr/)r_�get_projected_rater[)rrmro�projected_rates    rriz)LeakyBucket._projected_to_exceed_max_rates)���+�+�>�>�s�H�M������.�.rc�\�|jj|�|j||�Sr/)ra�process_scheduled_consumptionrkrls    rrhz8LeakyBucket._release_requested_amt_for_scheduled_requests0��	
�#�#�A�A��	
��*�*�3��9�9rc��|t|j�z}|jj|||�}t	||���)N)rr)rZr[ra�schedule_consumptionr)rrmrnro�allocated_timers      rrjz-LeakyBucket._raise_request_exceeded_exception%sF���u�T�^�^�4�4���0�0�E�E����
�
�'��*�
�	
rc�>�|jj||�|Sr/)r_�record_consumption_rate)rrmros   rrkz"LeakyBucket._release_requested_amt.s�����2�2�3��A��
r)NNN)	rrrr
rCrirhrjrkrrrrXrX�s.����"�!A�FB�>/�:�
�rrXc�$�eZdZd�Zd�Zd�Zd�Zy)rbc� �i|_d|_y)z*Schedules when to consume a desired amountrN)� _tokens_to_scheduled_consumption�_total_waitrs rr
zConsumptionScheduler.__init__4s��02��-���rc��||jvS)z�Indicates 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.
        )r|)r�tokens  rrgz!ConsumptionScheduler.is_scheduled9s����=�=�=�=rc�|�|xj|z
c_|j|d�|j|<|jS)a�Schedules 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.
        )�
wait_duration�time_to_consume)r}r|)rrmrr�s    rrvz)ConsumptionScheduler.schedule_consumptionBsA��&	
���O�+��!�-�-�.�8
��-�-�e�4����rc�|�|jj|�}t|j|dz
d�|_y)z�Processes 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.
        r�rN)r|�pop�maxr})rr�scheduled_retrys   rrtz2ConsumptionScheduler.process_scheduled_consumption\s=���?�?�C�C�E�J�������/@�A�A�1�
��rN)rrrr
rgrvrtrrrrbrb3s���
>� �4

rrbc�<�eZdZdd�Zed��Zd�Zd�Zd�Zd�Z	y)	r`c�.�||_d|_d|_y)a�Tracks 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)r�alphas  rr
zBandwidthRateTracker.__init__js��������!��rc�4�|j�y|jS)zmThe current transfer rate

        :rtype: float
        :returns: The current tracked transfer rate
        �)r�r�rs r�current_ratez!BandwidthRateTracker.current_ratezs���?�?�"���!�!�!rc�@�|j�y|j||�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
        r�)r��*_calculate_exponential_moving_average_rate�rrm�time_at_consumptions   rrqz'BandwidthRateTracker.get_projected_rate�s*���?�?�"���>�>��$�
�	
rc�v�|j�||_d|_y|j||�|_||_y)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
        Nr�)r�r�r�r�s   rryz,BandwidthRateTracker.record_consumption_rate�sB���?�?�"�1�D�O�!$�D���!�L�L��$�
���.��rc�J�||jz
}|dkrtd�S||zS)Nr�inf)r�rZ)rrmr��
time_deltas    r�_calculate_ratez$BandwidthRateTracker._calculate_rate�s.��(�4�?�?�:�
���?�
��<���j�!�!rc�~�|j||�}|j|zd|jz
|jzzS)N�)r�r�r�)rrmr��new_rates    rr�z?BandwidthRateTracker._calculate_exponential_moving_average_rate�s>���'�'��-@�A���{�{�X�%��T�[�[��D�<N�<N�(N�N�NrN)g�������?)
rrrr
�propertyr�rqryr�r�rrrr`r`is0��"� �"��"�
�$.�$"�Orr`)r\r�	Exceptionrrrr!r(rXrbr`rrr�<module>r�sk�����y��,	�	�!�!�$#�#�Ln�n�bZ�Z�z3
�3
�lNO�NOr