This document is for Kombu's development version, which can be significantly different from previous releases. Get the stable docs here: 5.3.

# Rate limiting - `kombu.utils.limits`

Token bucket implementation for rate limiting.

class kombu.utils.limits.TokenBucket(fill_rate, capacity=1)

  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.

  add(item)
  can_consume(tokens=1)

    Check if one or more tokens can be consumed.

    Returns:

      bool – 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.

    Return type:

      true if the number of tokens can be consumed

  capacity = 1

    Maximum number of tokens in the bucket.

  clear_pending()
  expected_time(tokens=1)

    Return estimated time of token availability.

    Returns:

      float

    Return type:

      the time in seconds.

  fill_rate = None

    The rate in tokens/second that the bucket will be refilled.

  pop()
  timestamp = None

    Timestamp of the last time a token was taken out of the bucket.