ó
O'—^c           @@  sI   d  Z  d d l m Z d d l m Z d g Z d e f d „  ƒ  YZ d S(   sX   
kombu.utils.limits
==================

Token bucket implementation for rate limiting.

i    (   t   absolute_import(   t	   monotonict   TokenBucketc           B@  sM   e  Z d  Z d Z d Z d Z d d „ Z d d „ Z d d „ Z	 d „  Z
 RS(   s—  Token Bucket Algorithm.

    See http://en.wikipedia.org/wiki/Token_Bucket
    Most of this code was stolen from an entry in the ASPN Python Cookbook:
    http://code.activestate.com/recipes/511490/

    .. admonition:: Thread safety

        This implementation is not thread safe. Access to a `TokenBucket`
        instance should occur within the critical section of any multithreaded
        code.

    i   c         C@  s7   t  | ƒ |  _ | |  _ t  | ƒ |  _ t ƒ  |  _ d  S(   N(   t   floatt   capacityt   _tokenst	   fill_rateR   t	   timestamp(   t   selfR   R   (    (    s4   /tmp/pip-unpacked-wheel-UAnTfW/kombu/utils/limits.pyt   __init__'   s    	c         C@  s)   | |  j  ƒ  k r% |  j | 8_ t St S(   sL  Return :const:`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.(   t   _get_tokensR   t   Truet   False(   R   t   tokens(    (    s4   /tmp/pip-unpacked-wheel-UAnTfW/kombu/utils/limits.pyt   can_consume-   s    c         C@  s*   |  j  ƒ  } t | | ƒ } | | |  j S(   s„   Return the time (in seconds) when a new token is expected
        to be available. This will not consume any tokens from the bucket.(   R
   t   maxR   (   R   R   R   (    (    s4   /tmp/pip-unpacked-wheel-UAnTfW/kombu/utils/limits.pyt   expected_time8   s    c         C@  s^   |  j  |  j k  rW t ƒ  } |  j | |  j } t |  j |  j  | ƒ |  _  | |  _ n  |  j  S(   N(   R   R   R   R   R   t   min(   R   t   nowt   delta(    (    s4   /tmp/pip-unpacked-wheel-UAnTfW/kombu/utils/limits.pyR
   ?   s    	N(   t   __name__t
   __module__t   __doc__t   NoneR   R   R   R	   R   R   R
   (    (    (    s4   /tmp/pip-unpacked-wheel-UAnTfW/kombu/utils/limits.pyR      s   N(   R   t
   __future__R    t
   kombu.fiveR   t   __all__t   objectR   (    (    (    s4   /tmp/pip-unpacked-wheel-UAnTfW/kombu/utils/limits.pyt   <module>   s   	