Skip to main content

Rate limit safe token allocator with least recently used (LRU) strategy if multiple tokens are available and will avoid rate limit errors.

Project description

token-limit-guard

Least recently used token allocator with per-token rate limiting and optional blocking behavior. Perfect for juggling API keys or access tokens that must respect individual quotas.

Features

  • Accept tokens from a file or in-memory iterable
  • Configure rate limiting per token with a rolling time window
  • Blocking and non-blocking retrieval modes
  • Simple LRU selection to balance token usage
  • Thread-safe implementation with helpful logging for debugging

Installation

The package is published on PyPI:

pip install token-limit-guard

Quickstart

from token_limit_guard import TokenLimitGuard

token_guard = TokenLimitGuard(["tokenA", "tokenB", "tokenC"])
token_guard.set_limiting_factors(max_allowed_count=2, time_window_in_sec=60)

# Blocking call – waits for the next available token if all are exhausted
token = token_guard.get_a_token()

# Non-blocking call – returns None immediately when no token is available
maybe_token = token_guard.get_a_token(block=False)

# Convenience alias for non-blocking access
maybe_token = token_guard.try_get_a_token()

print(token)

Development

Install dev dependencies and run the tests with pytest:

python -m venv .token-venv
source .token-venv/bin/activate  # or .venv\\Scripts\\activate on Windows
pip install -e .[dev] # pip install -e '.[dev]'
pytest

Publishing to PyPI

pip install --upgrade build
python -m build
pip install --upgrade twine
python -m twine upload dist/*

Remember to bump the version in pyproject.toml and token_limit_guard/__init__.py before publishing.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rate_limit_safe_token_allocator-0.1.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file rate_limit_safe_token_allocator-0.1.1.tar.gz.

File metadata

File hashes

Hashes for rate_limit_safe_token_allocator-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b59a4a43df40789668cd8d7a507e13e4190d11925159ba66d1fe8d7e68fa31c8
MD5 404264bb74233336e99b154c1ed92343
BLAKE2b-256 7ff218f7be11e6092e9db6e8c668fe6b22c277e1bb7d8d22d2e78b9c20db8da0

See more details on using hashes here.

File details

Details for the file rate_limit_safe_token_allocator-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for rate_limit_safe_token_allocator-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0b788d8cb2a3e4c01ed0dafb1e939c1129b26b24c9857df997d1e75682803400
MD5 87f25d0bb5b04dab4c66092259846ffa
BLAKE2b-256 fcfe63224d2ae2ed939db4a505efe1458b5d2bf229ff9d0e3f2ef00fcc5206a8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page