U
    N8c                     @   s@   d Z ddlZddlZddlmZ G dd dZG dd d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   ;/tmp/pip-unpacked-wheel-ozje0y8b/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   ZclockZmin_rater   r   r   r	      s    
zTokenBucket.__init__c                 C   s   | j S r   )r   r   r   r   r   r   "   s    zTokenBucket.max_ratec              	   C   s\   | j L |   t|| j| _|dkr.|| _nd| _t| j| j| _| j   W 5 Q R X d S )N   )	r   _refillmaxr   r   r   minr   notify)r   valuer   r   r   r   &   s     c                 C   s   | j S r   )r   r   r   r   r   max_capacity;   s    zTokenBucket.max_capacityc                 C   s   | j S r   )r   r   r   r   r   available_capacity?   s    zTokenBucket.available_capacityr   Tc              
   C   s,   | j  | j||dW  5 Q R  S Q R X 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   acquireC   s    
zTokenBucket.acquirec                 C   sv   |    || jkr$|  j|8  _dS |s.t | |}|dkr`| j| |    | |}q8|  j|8  _dS d S )NTr   )r    r   r   _sleep_amountr   wait)r   r   r'   Zsleep_amountr   r   r   r(   P   s    

zTokenBucket._acquirec                 C   s   || j  | j S r   )r   r   r   r   r   r   r*   g   s    zTokenBucket._sleep_amountc                 C   sT   | j  }| jd kr|| _d S | j}|| j | j }t| j|| }|| _|| _d S r   )r   r   r   r   r   r"   r   )r   	timestampZcurrent_capacityZfill_amountZnew_capacityr   r   r   r    j   s    

zTokenBucket._refillN)r   T)r   r   r   Z	_MIN_RATEr	   propertyr   setterr%   r&   r)   r(   r*   r    r   r   r   r   r      s   




r   )__doc__r   r
   Zbotocore.exceptionsr   r   r   r   r   r   r   <module>   s
   