Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README to include a usage example #2

Merged
merged 1 commit into from
Jul 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,46 @@ A *simple* token bucket implementation.
token bucket library. [Simple is better than
complex](https://peps.python.org/pep-0020/).

## Installation

`simple-token-bucket` is available on PyPI:

```
python -m pip install simple-token-bucket[redis]
```

This project requires Python 3.8 or newer to run.

## Usage example

```python
from simple_token_bucket import SimpleTokenBucket
from simple_token_bucket.backends.redis import RedisBackend

# Create a new bucket
third_party_api_rate_limit_bucket = SimpleTokenBucket(
name="third_party_api",
bucket_size=3,
interval=10,
)

third_party_api_rate_limit_bucket.try_get_token()
third_party_api_rate_limit_bucket.try_get_token()
third_party_api_rate_limit_bucket.try_get_token()

# will raises a NotEnoughTokens exception.
third_party_api_rate_limit_bucket.try_get_token()

# after 10 seconds everything works again.
import time
time.sleep(10)
third_party_api_rate_limit_bucket.try_get_token()
```

The latest documentation can be found here

> https://simple-token-bucket.readthedocs.io/en/latest/

## Contributing

Contributions are welcome. See Contributing section in docs.
Expand Down