
     h	                     @    d Z ddlmZ ddlmZ dZ G d d          ZdS )z.Token bucket implementation for rate limiting.    )deque)	monotonic)TokenBucketc                   N    e Zd ZdZdZdZdZddZd Zd Z	d Z
ddZdd	Zd
 ZdS )r   a  Token Bucket Algorithm.

    See Also:
        https://en.wikipedia.org/wiki/Token_Bucket

        Most of this code was stolen from an entry in the ASPN Python Cookbook:
        https://code.activestate.com/recipes/511490/

    Warning:
        Thread Safety: This implementation is not thread safe.
        Access to a `TokenBucket` instance should occur within the critical
        section of any multithreaded code.
    N   c                     t          |          | _        || _        t          |          | _        t	                      | _        t                      | _        d S N)floatcapacity_tokens	fill_rater   	timestampr   contents)selfr   r   s      N/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/kombu/utils/limits.py__init__zTokenBucket.__init__!   s?    hy))"    c                 :    | j                             |           d S r	   )r   append)r   items     r   addzTokenBucket.add(   s    T"""""r   c                 4    | j                                         S r	   )r   popleftr   s    r   popzTokenBucket.pop+   s    }$$&&&r   c                 8    | j                                          d S r	   )r   clearr   s    r   clear_pendingzTokenBucket.clear_pending.   s    r   c                 Z    ||                                  k    r| xj        |z  c_        dS dS )a  Check if one or more tokens can be consumed.

        Returns:
            bool: true if the number of tokens can be consumed
                from the bucket.  If they can be consumed, a call will also
                consume the requested number of tokens from the bucket.
                Calls will only consume `tokens` (the number requested)
                or zero tokens -- it will never consume a partial number
                of tokens.
        TF)_get_tokensr   )r   tokenss     r   can_consumezTokenBucket.can_consume1   s5     T%%''''LLF"LL4ur   c                 d    |                                  }t          ||          }||z
  | j        z  S )zoReturn estimated time of token availability.

        Returns:
            float: the time in seconds.
        )r    maxr   )r   r!   r   s      r   expected_timezTokenBucket.expected_timeA   s6     ""$$VW%% DN22r   c                     | j         | j        k     rIt                      }| j        || j        z
  z  }t          | j        | j         |z             | _         || _        | j         S r	   )r   r   r   r   r   min)r   nowdeltas      r   r    zTokenBucket._get_tokensK   sV    <$-''++CNcDN&:;Et}dlU.BCCDL DN|r   )r   )__name__
__module____qualname____doc__r   r   r   r   r   r   r   r"   r%   r     r   r   r   r   	   s          I H I       # # #' ' '      3 3 3 3    r   r   N)r-   collectionsr   timer   __all__r   r.   r   r   <module>r2      sp    4 4            
H H H H H H H H H Hr   