o
    .&ßaº  ã                   @   sD   d Z ddlZddlZddlmZ G dd„ deƒZG dd„ deƒZdS )zEThis module implements token buckets used for client side throttling.é    N)ÚCapacityNotAvailableErrorc                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚClockc                 C   s   d S ©N© ©Úselfr   r   ú9/usr/lib/python3/dist-packages/botocore/retries/bucket.pyÚ__init__	   s   zClock.__init__c                 C   s   t  |¡ d S r   )ÚtimeÚsleep©r   Úamountr   r   r   r      s   zClock.sleepc                 C   s   t   ¡ S r   )r
   r   r   r   r   Úcurrent_time   s   zClock.current_timeN)Ú__name__Ú
__module__Ú__qualname__r	   r   r   r   r   r   r   r      s    r   c                   @   sp   e Zd ZdZefdd„Zedd„ ƒZejdd„ ƒZedd„ ƒZed	d
„ ƒZ	ddd„Z
dd„ Zdd„ Zdd„ ZdS )ÚTokenBucketg      à?c                 C   sF   d | _ d | _d| _|| _d | _|| _t ¡ | _t 	| j¡| _
|| _d S )Nr   )Ú
_fill_rateÚ_max_capacityÚ_current_capacityÚ_clockÚ_last_timestampÚ	_min_rateÚ	threadingÚLockÚ_lockÚ	ConditionÚ_new_fill_rate_conditionÚmax_rate)r   r   ÚclockÚmin_rater   r   r   r	      s   

zTokenBucket.__init__c                 C   ó   | j S r   )r   r   r   r   r   r   "   ó   zTokenBucket.max_ratec                 C   sr   | j �, |  ¡  t|| jƒ| _|dkr|| _nd| _t| j| jƒ| _| j  ¡  W d   ƒ d S 1 s2w   Y  d S )Né   )	r   Ú_refillÚmaxr   r   r   Úminr   Únotify)r   Úvaluer   r   r   r   &   s   þ"îc                 C   r!   r   )r   r   r   r   r   Úmax_capacity<   r"   zTokenBucket.max_capacityc                 C   r!   r   )r   r   r   r   r   Úavailable_capacity@   r"   zTokenBucket.available_capacityr#   Tc                 C   s8   | j � | j||d�W  d  ƒ S 1 sw   Y  dS )aJ  Acquire token or return amount of time until next token available.

        If block is True, then this method will block until there's sufficient
        capacity to acquire the desired amount.

        If block is False, then this method will return True is capacity
        was successfully acquired, False otherwise.

        )r   ÚblockN)r   Ú_acquire)r   r   r+   r   r   r   ÚacquireD   s   
$ÿzTokenBucket.acquirec                 C   sx   |   ¡  || jkr|  j|8  _dS |stƒ ‚|  |¡}|dkr3| j |¡ |   ¡  |  |¡}|dks |  j|8  _dS )NTr   )r$   r   r   Ú_sleep_amountr   Úwait)r   r   r+   Úsleep_amountr   r   r   r,   Q   s   


÷
zTokenBucket._acquirec                 C   s   || j  | j S r   )r   r   r   r   r   r   r.   h   s   zTokenBucket._sleep_amountc                 C   sT   | j  ¡ }| jd u r|| _d S | j}|| j | j }t| j|| ƒ}|| _|| _d S r   )r   r   r   r   r   r&   r   )r   Ú	timestampÚcurrent_capacityÚfill_amountÚnew_capacityr   r   r   r$   k   s   


zTokenBucket._refillN)r#   T)r   r   r   Ú	_MIN_RATEr	   Úpropertyr   Úsetterr)   r*   r-   r,   r.   r$   r   r   r   r   r      s    




r   )Ú__doc__r
   r   Úbotocore.exceptionsr   Úobjectr   r   r   r   r   r   Ú<module>   s    