Metadata-Version: 2.4
Name: rate-limiter-df
Version: 0.1.0
Summary: A decorator-based rate limiter using the token bucket algorithm
Author: Dan Foster
Maintainer: Dan Foster
License: Copyright 2025 Daniel Foster
        
        Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
        
Project-URL: Homepage, https://github.com/yourusername/rate-limiter-df
Project-URL: Repository, https://github.com/yourusername/rate-limiter-df
Keywords: RateLimiter,Rate,Limiter
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Python: >=3.7
Description-Content-Type: text/markdown
License-File: LICENSE.txt
Provides-Extra: dev
Requires-Dist: mypy; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Requires-Dist: pytest; extra == "dev"
Dynamic: license-file

# Rate Limiter

A Python decorator-based rate limiter using the token bucket algorithm.

Made by Dan Foster

## Features

- Simple decorator-based API rate limiting
- Token bucket algorithm for smooth rate limiting (both short bursts and constant usage are supported)
- Per-key rate limiting support (e.g., per user ID)
- Thread-safe implementation
- Zero external dependencies (Python standard library only)

## Installation

pip install -e .

### Basic Rate Limiting

```python
from rate_limiter_df import RateLimiter, RateLimitExceeded

@RateLimiter(calls=10, period=60)  # 10 calls per 60 seconds
def my_api_call():
    # Your function code goes here
    return "Success"

try:
    result = my_api_call()
except RateLimitExceeded as e:
    print(f"Rate limit exceeded: {e}")
```

### Per-Key Rate Limiting

Rate limit different keys (e.g., user IDs) independently:

```python
def get_user_id(user_id, **kwargs):
    return user_id

@RateLimiter(calls=5, period=60, per_key=get_user_id)
def process_user_request(user_id, data):
    # Each user gets their own rate limit
    return f"Processed request for user {user_id}"

# User 1 can make 5 calls
process_user_request(user_id=1, data="...")
process_user_request(user_id=1, data="...")

# User 2 has their own separate rate limit
process_user_request(user_id=2, data="...")
```

### Advanced Example

```python
from rate_limiter_df import RateLimiter, RateLimitExceeded
import time

@RateLimiter(calls=3, period=5.0)
def expensive_operation():
    print("Performing expensive operation...")
    return "Done"

# Make multiple calls
for i in range(5):
    try:
        result = expensive_operation()
        print(f"Call {i+1}: {result}")
    except RateLimitExceeded as e:
        print(f"Call {i+1}: {e}")
        time.sleep(1)  # Wait before retrying
```

## How It Works

The rate limiter uses the **token bucket algorithm**:

- Each function (or key) has a bucket that holds tokens
- Tokens are consumed when the function is called
- Tokens refill over time at a constant rate
- If no tokens are available, a `RateLimitExceeded` exception is raised

## API Reference

### `RateLimiter(calls, period, per_key=None)`

Creates a rate limiter decorator.

**Parameters:**
- `calls` (int): Maximum number of calls allowed
- `period` (float): Time period in seconds
- `per_key` (callable, optional): Function to extract a key from function arguments for per-key rate limiting

**Returns:**
- A decorator that can be applied to functions

### `RateLimitExceeded`

Exception raised when the rate limit is exceeded. The exception message includes information about when to retry.

## License

MIT License - see LICENSE.txt for details.
