Hone logo
Hone
Problems

Leaky Bucket Implementation in Python

The leaky bucket is a traffic shaping algorithm used to control the rate of data flow into a system. It's a valuable tool for preventing bursts of data from overwhelming a network or resource. Your task is to implement a leaky bucket in Python, ensuring it regulates incoming requests based on a defined capacity and leak rate.

Problem Description

You need to create a LeakyBucket class in Python. This class should manage a bucket with a fixed capacity and a constant leak rate. The class should have the following methods:

  • __init__(capacity, leak_rate): Initializes the leaky bucket with a given capacity (maximum number of tokens the bucket can hold) and leak_rate (number of tokens leaked per unit of time).
  • add_token(tokens): Adds a specified number of tokens to the bucket. If adding the tokens would exceed the bucket's capacity, the excess tokens are discarded.
  • consume_token(tokens): Attempts to consume a specified number of tokens from the bucket. If there are enough tokens, the tokens are removed, and the method returns True. If there are insufficient tokens, the method returns False. The bucket should "leak" tokens at the defined leak_rate before attempting to consume.
  • get_current_level(): Returns the current number of tokens in the bucket.

The bucket should automatically leak tokens at the defined leak_rate after each add_token or consume_token call. The leak should happen before any consumption is attempted.

Examples

Example 1:

Input: capacity = 10, leak_rate = 2
bucket = LeakyBucket(capacity, leak_rate)
bucket.add_token(5)
print(bucket.get_current_level())
bucket.consume_token(3)
print(bucket.get_current_level())

Output:

5
2

Explanation: Initially, the bucket has 5 tokens. After adding 5 tokens, the level is 5. The leak removes 2 tokens, leaving 3. Consuming 3 tokens leaves 0.

Example 2:

Input: capacity = 5, leak_rate = 1
bucket = LeakyBucket(capacity, leak_rate)
bucket.add_token(10)
print(bucket.get_current_level())
bucket.consume_token(5)
print(bucket.get_current_level())

Output:

5
0

Explanation: Adding 10 tokens to a bucket of capacity 5 results in the bucket being full (5 tokens). The leak removes 1 token, leaving 4. Consuming 5 tokens results in the bucket being empty (0 tokens).

Example 3: (Edge Case - Insufficient Tokens)

Input: capacity = 3, leak_rate = 1
bucket = LeakyBucket(capacity, leak_rate)
bucket.consume_token(5)
print(bucket.get_current_level())

Output:

0

Explanation: The bucket starts empty. Attempting to consume 5 tokens when there are 0 tokens results in the method returning False and the bucket remaining empty.

Constraints

  • capacity and leak_rate must be positive integers.
  • tokens added or consumed must be non-negative integers.
  • The leak_rate is applied before attempting to consume tokens.
  • The bucket level should never be negative.

Notes

  • Consider using a simple time unit for the leak rate (e.g., one token leaked per call to add_token or consume_token).
  • Think about how to handle the leak rate efficiently. You don't need to simulate real-time; the leak should occur conceptually after each operation.
  • Focus on clarity and correctness. While performance is a consideration, prioritize a well-structured and understandable solution.
  • No external libraries are allowed.
Loading editor...
python