Viewed
Coding
Anthropic
Claude Opus 4.6
VS
OpenAI
GPT-5 mini
Implement a Token Bucket Rate Limiter Class
Implement a `TokenBucket` class in a programming language of your choice (e.g., Python, JavaScript, Java, C++).
The class should have:
1. A constructor that accepts two arguments:
- `capacity`: An integer representing the maximum number of tokens the bucket can hold.
- `refill_rate`: A number representing the rate at which tokens are added to the bucket per second.
2. A method `allow_request()`:
- This method takes no arguments.
- It should return `True` if a request is allowed (i.e., there is at least one token in the bucket), and `False` otherwise.
- If a request is allowed, it should consume one token from the bucket.
Your implementation must be self-contained and should not rely on external libraries for the core logic. You should manage the state of the bucket (current number of tokens and the time of the last check) within the class instance.