pax_global_header 0000666 0000000 0000000 00000000064 15134637643 0014526 g ustar 00root root 0000000 0000000 52 comment=634d16686b7707bca8ed3534051982791b9ef355
vutran1710-PyrateLimiter-634d166/ 0000775 0000000 0000000 00000000000 15134637643 0016425 5 ustar 00root root 0000000 0000000 vutran1710-PyrateLimiter-634d166/.coveragerc 0000664 0000000 0000000 00000000131 15134637643 0020541 0 ustar 00root root 0000000 0000000 [run]
concurrency = multiprocessing
parallel = True
branch = True
source = pyrate_limiter vutran1710-PyrateLimiter-634d166/.github/ 0000775 0000000 0000000 00000000000 15134637643 0017765 5 ustar 00root root 0000000 0000000 vutran1710-PyrateLimiter-634d166/.github/workflows/ 0000775 0000000 0000000 00000000000 15134637643 0022022 5 ustar 00root root 0000000 0000000 vutran1710-PyrateLimiter-634d166/.github/workflows/build_test.yml 0000664 0000000 0000000 00000010703 15134637643 0024704 0 ustar 00root root 0000000 0000000 name: Python package - UV
on:
push:
branches:
- master
tags:
- "v*"
pull_request:
branches:
- master
concurrency:
# cancels superseded runs
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
LATEST_PY_VERSION: "3.14"
REDIS: "redis://localhost:6379"
jobs:
smoke:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python ${{ env.LATEST_PY_VERSION }}
uses: actions/setup-python@v5
with:
python-version: ${{ env.LATEST_PY_VERSION }}
allow-prereleases: true
- name: Install uv
uses: astral-sh/setup-uv@v6
with:
enable-cache: true
python-version: ${{ env.LATEST_PY_VERSION }}
- name: Lint
run: |
uv run nox -e lint
- run: uv sync --all-groups
- run: uv run nox -e smoke
check-others:
needs: smoke
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
include:
- os: macos-latest
python-version: "3.14"
#- os: windows-latest
# python-version: '3.14'
- os: ubuntu-latest
python-version: "pypy3.11"
steps:
- name: Check out code
uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true
- name: Install uv
uses: astral-sh/setup-uv@v6
with:
enable-cache: true
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: uv sync --all-groups
- name: Test and Coverage
run: |
uv run nox -e notlinux
check-linux:
needs: smoke
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
python-version: ["3.10", "3.14", "3.14t"]
os: [ubuntu-latest]
services:
pyrate_redis:
image: redis:7
ports:
- 6379:6379
# Set health checks to wait until redis has started
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
pyrate_postgres:
image: postgres:18
env:
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
options: >-
--health-cmd "pg_isready -U postgres"
--health-interval 10s --health-timeout 5s --health-retries 5
steps:
- name: Check out code
uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install uv
uses: astral-sh/setup-uv@v6
with:
enable-cache: true
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: uv sync --all-groups
# Run linting (latest python version only)
- name: Build and install package
if: ${{ matrix.python-version == env.LATEST_PY_VERSION && matrix.os == 'ubuntu-latest' }}
run: |
uv build
uv pip install dist/*.whl
# Run tests and coverage report (all python versions)
- name: Test and Coverage
run: |
uv run nox -e cover
# Latest python version: send coverage report to codecov
- name: "Upload coverage report to Codecov"
if: ${{ matrix.python-version == env.LATEST_PY_VERSION && matrix.os == 'ubuntu-latest' }}
uses: codecov/codecov-action@v2
- name: Upload dist, if this is latest
if: ${{ matrix.python-version == env.LATEST_PY_VERSION && matrix.os == 'ubuntu-latest' }}
uses: actions/upload-artifact@v4
with:
name: dist
path: dist
publish:
needs: [check-linux, check-others]
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/v')
permissions:
contents: read
id-token: write
steps:
- name: Download dist
uses: actions/download-artifact@v4
with:
name: dist
path: dist
- name: Upload package to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
vutran1710-PyrateLimiter-634d166/.gitignore 0000664 0000000 0000000 00000000336 15134637643 0020417 0 ustar 00root root 0000000 0000000 .coverage
.coverage.*
pyrate_limiter.egg-info/
__pycache__/
dist/
docs/_build/
docs/modules/
htmlcov/
env/
test-reports/
.vim/
.vscode/
.idea/
.coveralls.yml
.DS_Store
.venv
xdg_cache
*\.sqlite*
_build_html
coverage.xml
vutran1710-PyrateLimiter-634d166/.pre-commit-config.yaml 0000664 0000000 0000000 00000001023 15134637643 0022702 0 ustar 00root root 0000000 0000000 repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0
hooks:
- id: check-yaml
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.12.9
hooks:
- id: ruff-check
types_or: [ python, pyi, jupyter ]
args: [ --fix ]
- id: ruff-format
types_or: [ python, pyi, jupyter ]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.17.1
hooks:
- id: mypy
additional_dependencies: [types-filelock, types-requests, types-redis]
vutran1710-PyrateLimiter-634d166/.python-version 0000664 0000000 0000000 00000000004 15134637643 0021424 0 ustar 00root root 0000000 0000000 3.10 vutran1710-PyrateLimiter-634d166/.readthedocs.yml 0000664 0000000 0000000 00000000613 15134637643 0021513 0 ustar 00root root 0000000 0000000 # Read the Docs configuration file
version: 2
build:
os: "ubuntu-24.04"
tools:
python: "3.12"
commands:
- pip install uv
- uv sync --all-groups --all-extras
- uv run python -m sphinx docs $READTHEDOCS_OUTPUT/html -b html -W
sphinx:
builder: html
configuration: docs/conf.py
python:
install:
- method: pip
path: .
extra_requirements:
- docs
vutran1710-PyrateLimiter-634d166/CHANGELOG.md 0000664 0000000 0000000 00000017251 15134637643 0020244 0 ustar 00root root 0000000 0000000 # Change Log
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [4.0.0]
Fourth major release with significant API simplification and breaking changes.
### Breaking Changes
#### Exceptions Removed
- `BucketFullException` and `LimiterDelayException` have been removed
- Rate limiting now uses blocking behavior instead of exception-based flow control
#### Limiter Constructor Simplified
- Removed `clock` parameter - each bucket now manages its own clock via `bucket.now()`
- Removed `raise_when_fail` parameter - no exceptions are raised
- Removed `max_delay` parameter - blocking is now controlled per-call
- Removed `retry_until_max_delay` parameter
- Added `buffer_ms` parameter (default 50ms) for clock drift tolerance
#### Clock Changes
- `TimeClock` removed - use `MonotonicClock` instead
- `SQLiteClock` removed - buckets now manage their own time
- `TimeAsyncClock` renamed to `MonotonicAsyncClock`
- Clock is no longer passed to Limiter; buckets have built-in `now()` method
#### try_acquire API Changes
- New signature: `try_acquire(name="pyrate", weight=1, blocking=True, timeout=-1)`
- `blocking=True` (default): blocks until permit is acquired
- `blocking=False`: returns `False` immediately if bucket is full
- Added `try_acquire_async()` for proper async acquisition with asyncio.Lock
#### Decorator API Changes
- Old: `limiter.as_decorator()(mapping_func)(target_func)` where mapping returns `(name, weight)`
- New: `limiter.as_decorator(name="...", weight=...)(target_func)` with direct parameters
#### BucketFactory Changes
- `schedule_leak(bucket, clock)` now `schedule_leak(bucket)` - no clock parameter
- `create(clock, bucket_class, ...)` now `create(bucket_class, ...)` - no clock parameter
### New Features
- Context manager support: `with Limiter(...) as limiter:` and `with bucket:`
- `limiter.close()` method for resource cleanup
- `limiter_factory` module with convenience functions:
- `create_sqlite_limiter()`
- `create_inmemory_limiter()`
- `init_global_limiter()`
- `MultiprocessBucket` for multiprocessing environments
- Web request helpers in `pyrate_limiter.extras`:
- `aiohttp_limiter.RateLimitedSession`
- `httpx_limiter.RateLimiterTransport` / `AsyncRateLimiterTransport`
- `requests_limiter.RateLimitedRequestsSession`
- Time is now monotonic by default (`monotonic_ns()`)
- `buffer_ms` configurable per-Limiter (default 50ms)
### Improvements
- Migrated from Poetry to UV for builds
- Switched from Bitnami to official Redis/Postgres Docker images
- Pre-commit switched to Ruff
- Improved multiprocessing support with proper locking
- Better asyncio support with thread-local async locks
## [3.9.0]
* Introduce MultiprocessBucket
* Update documentation to include MultiprocessBucket
* Add delay configure
* Simplify lock interface for SQLFileLock & MultiprocessBucket
## [3.8.1]
* Keep Retrying until Max Delay Has Expired
* Postgres performance turning
* Fix cursor leaks on SQLiteBucket
## [3.8.0]
* Add FileLock option for SQLiteBucket
## [3.7.1]
* Update package metadata and local dev config to support python 3.13
## [3.7.0]
* Add method to remove bucket
## [3.6.2]
* Fix table creation for SQLiteBucket
## [3.6.1]
* Support creating/getting bucket asynchronously
## [3.6.0]
* Use psycopg3 for PostgresBucket
## [3.5.1]
* Fix dependencies for "all" package extra
## [3.5.0]
* Add PostgresBucket backend
## [3.4.1]
* Fix: unnecessary warning during async check
## [3.4.0]
* Improved in-memory-bucket performance
## [3.3.0]
* Fix background task for leaking
## [3.2.1] - 2024-02-13
* Fix Redis CROSSSLOT Keys following issue [#126](https://github.com/vutran1710/PyrateLimiter/issues/126)
## [3.1.1] - 2024-01-02
* Fix broken SqliteBucket following issue [#132](https://github.com/vutran1710/PyrateLimiter/issues/132)
## [3.1.0] - 2023-08-28
* Allow to pass rates directly to Limiter to use default ImMemoryBucket with Limiter
* Allow to pass *Duration* to `max_delay` argument of Limiter
## [3.0.2] - 2023-08-28
* Critical bug fix: importing redis fail crashing apps
## [3.0.0] - 2023-08-28
Third major release with API breaking changes:
- Drop python 3.7 (only python ^3.8)
- Bucket must be initialized before passing to Limiter
- Auto leaking (provided by BucketFactory)
- Decorator API changes
- Limiter workable with both async/sync out-of-the-box
- Async RedisBucket built-in
- Contextmanager not available yet
## [2.10.0] - 2023-02-26
### Updates
* Add change log to sdist
* Improve test coverage
* Force check some bucket-keyword arguments
## [2.9.1] - 2023-02-26
### Fixed
* Fix unit test to make test results stable
* Fix remaining-time calculation using exact 3 decimals only
* Increase test intesity to ensure correctness
## [2.8.5] - TBD
### Fixed
* Fix SQLite OperationalError when getting more items than SQLite variable limit
## [2.8.4] - 2022-11-23
### Fixed
* Build both `wheel` and `sdist` on publish
## [2.8.3] - 2022-10-17
### Added
* Add option to expire redis key when using RedisBucket
## [2.8.2] - 2022-09-24
### Removed
* Python 3.6 support
## [2.8.1] - 2022-04-11
### Added
* Add Sphinx config
* Add documentation site: https://pyrate-limiter.readthedocs.io
* Add some missing type hints
* Add package metadata to indicate PEP-561 compliance
## [2.8.0] - 2022-04-10
### Added
* Add `flush()` method to all bucket classes
## [2.7.0] - 2022-04-06
### Added
* Add `FileLockSQliteBucket` for a SQLite backend with file-based locking
* Add optional backend dependencies to package metadata
## [2.6.3] - 2022-04-05
### Fixed
* Make SQLite bucket thread-safe and multiprocess-safe
## [2.6.2] - 2022-03-30
### Fixed
* Remove development scripts from package published on PyPI
### Added
* Add `nox` to run development scripts
## [2.6.1] - 2022-03-30
### Updated
* Replace all formatting/linting tools with *pre-commit*
## [2.6.0] - 2021-12-08
### Added
* Add `SQliteBucket` to persist rate limit data in a SQLite database
## [2.5.0] - 2021-12-08
### Added
* Custom time source
## [2.4.6] - 2021-09-30
* Add `RedisClusterBucket` to support using `PyrateLimiter` with `redis-py-cluster`
* Update README, add Table of Content
## [2.3.6] - 2021-09-23
* Run CI tests for all supported python versions
* Fix issue with deployments on Travis CI
## [2.3.5] - 2021-09-22
### Added
* Use `time.monotonic()` instead of `time.time()`
* Support for floating point rate-limiting delays (more granular than 1 second)
## [2.3.4] - 2021-06-01
### Fixed
* Bucket group initialization
## [2.3.3] - 2021-05-08
### Added
* Support for python 3.6
## [2.3.2] - 2021-05-06
### Fixed
* Incorrect type hint
## [2.3.1] - 2021-04-26
### Added
* LICENSE file to be included in PyPI package
### Fixed
* Incorrect delay time when using using `Limiter.ratelimit()` with `delay=True`
## [2.3.0] - 2021-03-01
### Added
* Support for using `Limiter.ratelimit()` as a contextmanager or async contextmanager
* Separate `LimitContextDecorator` class to handle `Limiter.ratelimit()` behavior
* Package published on conda-forge
## [2.2.2] - 2021-03-03
### Changed
* Internal: Reduce cognitive complexity
## [2.2.1] - 2021-03-02
### Fixed
* Incorrect check log against time-window
## [2.2.0] - 2021-02-26
### Added
* `Limiter.ratelimit()` method, an async-compatible decorator that optionally adds rate-limiting delays
## [2.1.0] - 2021-02-21
## [2.0.3] - 2020-06-01
## [2.0.2] - 2020-06-01
## [2.0.1] - 2020-06-01
## [2.0.0] - 2019-12-29
## [1.1.0] - 2019-12-17
### Removed
- Code duplication
### Added
- Thread lock for Bucket's state modification in case of Multi-threading
- Html Cover Report
### Fixed
- LocalBucket's default init value being mutated
- Typos. A lot of friggin' typos.
vutran1710-PyrateLimiter-634d166/CONTRIBUTING.md 0000664 0000000 0000000 00000003374 15134637643 0020665 0 ustar 00root root 0000000 0000000 # Contributing Guide
Here are some basic instructions for local development setup and contributing to the project.
## Setup & Commands
To setup local development, *uv* and *Python 3.9+* are required. Follow instructions at for installing uv.
Then, in the repository directory, run the following to install all optional backend dependencies and dev dependencies:
```shell
$ uv sync --all-groups --all-extras
```
Some shortcuts are included for some common development tasks, using [nox](https://nox.thea.codes):
- Run tests with: `nox -e test`
- To run tests with coverage: `nox -e cover`
- Format & check for lint error: `nox -e lint`
- To run linting for every commit, run: `pre-commit install`
## Documentation
Documentation is generated using [Sphinx](https://www.sphinx-doc.org) and published on readthedocs.io.
To build this documentation locally:
```
nox -e docs
```
## Guideline & Notes
We have GitHub Action CICD to do the checking, testing and publishing work. So, there are few small notes when making Pull Request:
- All existing tests must pass (Of course!)
- Reduction in *Coverage* shall result in failure. (below 98% is not accepted)
- When you are making bug fixes, or adding more features, remember to bump the version number in **pyproject.toml**. The number should follow *semantic-versioning* rules
## TODO
Planned features:
* A rate limit may reset on a fixed schedule, eg: every first-day of a month
* Sometimes, we may need to apply specific rate-limiting strategies based on schedules/region or some other metrics. It
requires the capability to switch the strategies instantly without re-deploying the whole service.
* Reference: https://www.keycdn.com/support/rate-limiting#types-of-rate-limits
vutran1710-PyrateLimiter-634d166/LICENSE 0000664 0000000 0000000 00000002053 15134637643 0017432 0 ustar 00root root 0000000 0000000 MIT License
Copyright (c) 2021 vutran1710
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.
vutran1710-PyrateLimiter-634d166/README.md 0000664 0000000 0000000 00000066124 15134637643 0017715 0 ustar 00root root 0000000 0000000
# PyrateLimiter
The request rate limiter using Leaky-bucket Algorithm.
Full project documentation can be found at [pyratelimiter.readthedocs.io](https://pyratelimiter.readthedocs.io).
[](https://badge.fury.io/py/pyrate-limiter)
[](https://pypi.org/project/pyrate-limiter)
[](https://codecov.io/gh/vutran1710/PyrateLimiter)
[](https://github.com/vutran1710/PyrateLimiter/graphs/commit-activity)
[](https://pypi.python.org/pypi/pyrate-limiter/)
> **Upgrading from v3.x?** See the [Migration Guide](docs/migrating.md) for breaking changes.
## Contents
- [Features](#features)
- [Installation](#installation)
- [Web Requests](#web-request-rate-limiting)
- [AIOHTTP](#aiohttp)
- [HTTPX](#httpx)
- [Requests](#requests)
- [Quickstart](#quickstart)
- [limiter_factory](#limiter_factory)
- [Examples](#examples)
- [Basic usage](#basic-usage)
- [Key concepts](#key-concepts)
- [Defining rate limits & buckets](#defining-rate-limits-and-buckets)
- [Defining clock & routing logic](#defining-clock--routing-logic-with-bucketfactory)
- [Wrapping all up with Limiter](#wrapping-all-up-with-limiter)
- [asyncio and event loops](#asyncio-and-event-loops)
- [`as_decorator()`: use limiter as decorator](#as_decorator-use-limiter-as-decorator)
- [Limiter API](#limiter-api)
- [Context Manager](#context-manager)
- [Weight](#weight)
- [Handling exceeded limits](#handling-exceeded-limits)
- [Bucket analogy](#bucket-analogy)
- [Blocking vs Non-blocking](#blocking-vs-non-blocking)
- [Backends](#backends)
- [InMemoryBucket](#inmemorybucket)
- [MultiprocessBucket](#multiprocessbucket)
- [SQLiteBucket](#sqlitebucket)
- [RedisBucket](#redisbucket)
- [PostgresBucket](#postgresbucket)
- [BucketAsyncWrapper](#bucketasyncwrapper)
- [Async or Sync or Multiprocessing](#async-or-sync-or-multiprocessing)
- [Advanced Usage](#advanced-usage)
- [Component-level Diagram](#component-level-diagram)
- [Time sources](#time-sources)
- [Leaking](#leaking)
- [Concurrency](#concurrency)
- [Custom backend](#custom-backend)
## Features
- Supports unlimited rate limits and custom intervals.
- Separately tracks limits for different services or resources.
- Manages limit breaches with configurable blocking or non-blocking behavior.
- Offers multiple usage modes: direct calls or decorators.
- Fully compatible with both synchronous and asynchronous workflows.
- Provides SQLite and Redis backends for persistent limit tracking across threads or restarts.
- Includes MultiprocessBucket and SQLite File Lock backends for multiprocessing environments.
## Installation
**PyrateLimiter** supports **python ^3.8**
Install using pip:
```
pip install pyrate-limiter
```
Or using conda:
```
conda install --channel conda-forge pyrate-limiter
```
## Quickstart
To limit 5 requests within 2 seconds:
```python
from pyrate_limiter import Duration, Rate, Limiter
limiter = Limiter(Rate(5, Duration.SECOND * 2))
# Blocking mode (default) - waits until permit available
for i in range(6):
limiter.try_acquire(str(i))
print(f"Acquired permit {i}")
# Non-blocking mode - returns False if bucket full
for i in range(6):
success = limiter.try_acquire(str(i), blocking=False)
if not success:
print(f"Rate limited at {i}")
```
## limiter_factory
[limiter_factory.py](https://github.com/vutran1710/PyrateLimiter/blob/master/pyrate_limiter/limiter_factory.py) provides several functions to simplify common cases:
- create_sqlite_limiter(rate_per_duration: int, duration: Duration, ...)
- create_inmemory_limiter(rate_per_duration: int, duration: Duration, ...)
- + more to be added...
## Examples
- Rate limiting asyncio tasks: [asyncio_ratelimit.py](https://github.com/vutran1710/PyrateLimiter/blob/master/examples/asyncio_ratelimit.py)
- Rate limiting asyncio tasks w/ a decorator: [asyncio_decorator.py](https://github.com/vutran1710/PyrateLimiter/blob/master/examples/asyncio_decorator.py)
- HTTPX rate limiting - asyncio, single process and multiprocess examples [httpx_ratelimiter.py](https://github.com/vutran1710/PyrateLimiter/blob/master/examples/httpx_ratelimiter.py)
- Multiprocessing using an in-memory rate limiter - [in_memory_multiprocess.py](https://github.com/vutran1710/PyrateLimiter/blob/master/examples/in_memory_multiprocess.py)
- Multiprocessing using SQLite and a file lock - this can be used for distributed processes not created within a multiprocessing [sql_filelock_multiprocess.py](https://github.com/vutran1710/PyrateLimiter/blob/master/examples/sql_filelock_multiprocess.py)
## Web Request Rate Limiting
pyrate_limiter provides three extras for popular web request libraries:
- [AIOHTTP](https://pypi.org/project/aiohttp/)
- [HTTPX](https://pypi.org/project/httpx/)
- [Requests](https://pypi.org/project/requests/)
### AIOHTTP
```py
from pyrate_limiter import limiter_factory
from pyrate_limiter.extras.aiohttp_limiter import RateLimitedSession
limiter = limiter_factory.create_inmemory_limiter(rate_per_duration=2, duration=Duration.SECOND)
session = RateLimitedSession(limiter)
```
Example: [aiohttp_ratelimiter.py](https://github.com/vutran1710/PyrateLimiter/blob/master/examples/aiohttp_ratelimiter.py)
### HTTPX
```py
from pyrate_limiter import limiter_factory
from pyrate_limiter.extras.httpx_limiter import AsyncRateLimiterTransport, RateLimiterTransport
import httpx
limiter = limiter_factory.create_inmemory_limiter(rate_per_duration=1, duration=Duration.SECOND, max_delay=Duration.HOUR)
url = "https://example.com"
with httpx.Client(transport=RateLimiterTransport(limiter=limiter)) as client:
client.get(url)
# or async
async with httpx.AsyncClient(transport=AsyncRateLimiterTransport(limiter=limiter)) as client:
client.get(url)
...
```
Example: [httpx_ratelimiter.py](https://github.com/vutran1710/PyrateLimiter/blob/master/examples/httpx_ratelimiter.py)
### Requests
```py
from pyrate_limiter import limiter_factory
from pyrate_limiter.extras.requests_limiter import RateLimitedRequestsSession
limiter = limiter_factory.create_inmemory_limiter(rate_per_duration=2, duration=Duration.SECOND)
session = RateLimitedRequestsSession(limiter)
....
```
Example: [requests_ratelimiter.py](https://github.com/vutran1710/PyrateLimiter/blob/master/examples/requests_ratelimiter.py)
## Basic Usage
### Key concepts
#### Clock
- Timestamps incoming items
#### Bucket
- Stores items with timestamps.
- Functions as a FIFO queue.
- Can `leak` to remove outdated items.
#### BucketFactory
- Manages buckets and clocks, routing items to their appropriate buckets.
- Schedules periodic `leak` operations to prevent overflow.
- Allows custom logic for routing, conditions, and timing.
#### Limiter
- Provides a simple, intuitive API by abstracting underlying logic.
- Seamlessly supports both sync and async contexts.
- Offers multiple interaction modes: direct calls, decorators, and (future) context managers.
- Ensures thread-safety via RLock, and if needed, asyncio concurrency via asyncio.Lock
### Defining rate limits and buckets
For example, an API (like LinkedIn or GitHub) might have these rate limits:
```
- 500 requests per hour
- 1000 requests per day
- 10000 requests per month
```
You can define these rates using the `Rate` class. `Rate` class has 2 properties only: **limit** and **interval**
```python
from pyrate_limiter import Duration, Rate
hourly_rate = Rate(500, Duration.HOUR) # 500 requests per hour
daily_rate = Rate(1000, Duration.DAY) # 1000 requests per day
monthly_rate = Rate(10000, Duration.WEEK * 4) # 10000 requests per month
rates = [hourly_rate, daily_rate, monthly_rate]
```
Rates must be properly ordered:
- Rates' intervals & limits must be ordered from least to greatest
- Rates' ratio of **limit/interval** must be ordered from greatest to least
Buckets validate rates during initialization. If using a custom implementation, use the built-in validator:
```python
from pyrate_limiter import validate_rate_list
assert validate_rate_list(my_rates)
```
Then, add the rates to the bucket of your choices
```python
from pyrate_limiter import InMemoryBucket, RedisBucket
basic_bucket = InMemoryBucket(rates)
# Or, using redis
from redis import Redis
redis_connection = Redis(host='localhost')
redis_bucket = RedisBucket.init(rates, redis_connection, "my-bucket-name")
# Async Redis would work too!
from redis.asyncio import Redis
redis_connection = Redis(host='localhost')
redis_bucket = await RedisBucket.init(rates, redis_connection, "my-bucket-name")
```
If you only need a single Bucket for everything, and python's built-in `time()` is enough for you, then pass the bucket to Limiter then ready to roll!
```python
from pyrate_limiter import Limiter
# Limiter constructor accepts single bucket as the only parameter,
# the rest are 3 optional parameters with default values as following
# Limiter(bucket, clock=MonotonicClock(), raise_when_fail=True, max_delay=None)
limiter = Limiter(bucket)
# Limiter is now ready to work!
limiter.try_acquire("hello world")
```
If you want to have finer grain control with routing & clocks etc, then you should use `BucketFactory`.
### Defining Clock & routing logic with BucketFactory
When multiple bucket types are needed and items must be routed based on certain conditions, use `BucketFactory`.
First, define your clock (time source). Most use cases work with the built-in clocks:
```python
from pyrate_limiter.clock import MonotonicClock, SQLiteClock
base_clock = MonotonicClock()
```
PyrateLimiter does not assume routing logic, so you implement a custom BucketFactory. At a minimum, these two methods must be defined:
```python
from pyrate_limiter import BucketFactory
from pyrate_limiter import AbstractBucket
class MyBucketFactory(BucketFactory):
# You can use constructor here,
# nor it requires to make bucket-factory work!
def wrap_item(self, name: str, weight: int = 1) -> RateItem:
"""Time-stamping item, return a RateItem"""
now = clock.now()
return RateItem(name, now, weight=weight)
def get(self, _item: RateItem) -> AbstractBucket:
"""For simplicity's sake, all items route to the same, single bucket"""
return bucket
```
### Creating buckets dynamically
If more than one bucket is needed, the bucket-routing logic should go to BucketFactory `get(..)` method.
When creating buckets dynamically, it is needed to schedule leak for each newly created buckets.
To support this, BucketFactory comes with a predefined method call `self.create(..)`. It is meant to create the bucket and schedule that bucket for leaking using the Factory's clock
```python
def create(
self,
clock: AbstractClock,
bucket_class: Type[AbstractBucket],
*args,
**kwargs,
) -> AbstractBucket:
"""Creating a bucket dynamically"""
bucket = bucket_class(*args, **kwargs)
self.schedule_leak(bucket, clock)
return bucket
```
By utilizing this, we can modify the code as following:
```python
class MultiBucketFactory(BucketFactory):
def __init__(self, clock):
self.clock = clock
self.buckets = {}
def wrap_item(self, name: str, weight: int = 1) -> RateItem:
"""Time-stamping item, return a RateItem"""
now = clock.now()
return RateItem(name, now, weight=weight)
def get(self, item: RateItem) -> AbstractBucket:
if item.name not in self.buckets:
# Use `self.create(..)` method to both initialize new bucket and calling `schedule_leak` on that bucket
# We can create different buckets with different types/classes here as well
new_bucket = self.create(YourBucketClass, *your-arguments, **your-keyword-arguments)
self.buckets.update({item.name: new_bucket})
return self.buckets[item.name]
```
### Wrapping all up with Limiter
Pass your bucket-factory to Limiter, and ready to roll!
```python
from pyrate_limiter import Limiter
limiter = Limiter(
bucket_factory,
raise_when_fail=False, # Default = True
max_delay=1000, # Default = None
)
item = "the-earth"
limiter.try_acquire(item)
heavy_item = "the-sun"
limiter.try_acquire(heavy_item, weight=10000)
```
### asyncio and event loops
To ensure the event loop isn't blocked, use `try_acquire_async` with an **async bucket**, which leverages `asyncio.Lock` for concurrency control.
If your bucket isn't async, wrap it with `BucketAsyncWrapper`. This ensures `asyncio.sleep` is used instead of `time.sleep`, preventing event loop blocking:
```python
await limiter.try_acquire_async(item)
```
Example: [asyncio_ratelimit.py](https://github.com/vutran1710/PyrateLimiter/blob/master/examples/asyncio_ratelimit.py)
#### `as_decorator()`: use limiter as decorator
`Limiter` can be used as a decorator with `name` and `weight` parameters.
The decorator works with both synchronous and asynchronous functions:
```python
from pyrate_limiter import Rate, Duration, Limiter
limiter = Limiter(Rate(5, Duration.SECOND))
@limiter.as_decorator(name="api_call", weight=1)
def handle_something(*args, **kwargs):
"""function logic"""
@limiter.as_decorator(name="background_job", weight=2)
async def handle_something_async(*args, **kwargs):
"""async function logic"""
```
For full example see [asyncio_decorator.py](https://github.com/vutran1710/PyrateLimiter/blob/master/examples/asyncio_decorator.py)
### Limiter API
#### `bucket()`: get list of all active buckets
Return list of all active buckets with `limiter.buckets()`
#### `dispose(bucket: int | BucketObject)`: dispose/remove/delete the given bucket
Method signature:
```python
def dispose(self, bucket: Union[int, AbstractBucket]) -> bool:
"""Dispose/Remove a specific bucket,
using bucket-id or bucket object as param
"""
```
Example of usage:
```python
active_buckets = limiter.buckets()
assert len(active_buckets) > 0
bucket_to_remove = active_buckets[0]
assert limiter.dispose(bucket_to_remove)
```
If a bucket is found and get deleted, calling this method will return **True**, otherwise **False**.
If there is no more buckets in the limiter's bucket-factory, all the leaking tasks will be stopped.
#### Context Manager
Limiter supports the context manager protocol for automatic cleanup:
```python
from pyrate_limiter import Limiter, RequestRate, Duration, InMemoryBucket
# Define a simple rate and create a bucket
rate = RequestRate(5, Duration.SECOND)
bucket = InMemoryBucket(rate)
# Use Limiter as a context manager
with Limiter(bucket) as limiter:
limiter.try_acquire("item")
# Resources automatically released
# Or manually close
limiter = Limiter(bucket)
try:
limiter.try_acquire("item")
finally:
limiter.close()
```
### Weight
Item can have weight. By default item's weight = 1, but you can modify the weight before passing to `limiter.try_acquire`.
Item with weight W > 1 when consumed will be multiplied to (W) items with the same timestamp and weight = 1. Example with a big item with weight W=5, when put to bucket, it will be divided to 5 items with weight=1 + following names
```
BigItem(weight=5, name="item", timestamp=100) => [
item(weight=1, name="item", timestamp=100),
item(weight=1, name="item", timestamp=100),
item(weight=1, name="item", timestamp=100),
item(weight=1, name="item", timestamp=100),
item(weight=1, name="item", timestamp=100),
]
```
Yet, putting this big, heavy item into bucket is expected to be transactional & atomic - meaning either all 5 items will be consumed or none of them will. This is made possible as bucket `put(item)` always check for available space before ingesting. All of the Bucket's implementations provided by **PyrateLimiter** follows this rule.
Any additional, custom implementation of Bucket are expected to behave alike - as we have unit tests to cover the case.
See [Advanced Usage](#advanced-usage) below for more details.
### Handling exceeded limits
When a rate limit is exceeded, you can choose between blocking and non-blocking behavior.
#### Bucket analogy
At this point it's useful to introduce the analogy of "buckets" used for rate-limiting. Here is a
quick summary:
- This library implements the [Leaky Bucket algorithm](https://en.wikipedia.org/wiki/Leaky_bucket).
- It is named after the idea of representing some kind of fixed capacity -- like a network or service -- as a bucket.
- The bucket "leaks" at a constant rate. For web services, this represents the **ideal or permitted request rate**.
- The bucket is "filled" at an intermittent, unpredicatble rate, representing the **actual rate of requests**.
- When the bucket is "full", it will overflow, representing **canceled or delayed requests**.
- Item can have weight. Consuming a single item with weight W > 1 is the same as consuming W smaller, unit items - each with weight=1, with the same timestamp and maybe same name (depending on however user choose to implement it)
#### Blocking vs Non-blocking
By default, `try_acquire` blocks until a permit becomes available:
```python
from pyrate_limiter import Rate, Limiter, Duration
rate = Rate(3, Duration.SECOND)
limiter = Limiter(rate)
# Blocking (default) - waits until permit is available
for i in range(5):
limiter.try_acquire("item") # blocks if bucket is full
print(f"Acquired {i}")
```
For non-blocking behavior, set `blocking=False` to return immediately:
```python
# Non-blocking - returns False immediately if bucket is full
for i in range(5):
success = limiter.try_acquire("item", blocking=False)
if not success:
print(f"Rate limited at request {i}")
break
```
For async code, use `try_acquire_async` with optional timeout:
```python
# Async with timeout (in seconds)
success = await limiter.try_acquire_async("item", timeout=5)
if not success:
print("Timed out waiting for permit")
```
The `buffer_ms` parameter (default 50ms) adds a small delay buffer to account for timing variations:
```python
from pyrate_limiter import Duration, InMemoryBucket, Limiter, RequestRate
rate = RequestRate(5, Duration.SECOND)
bucket = InMemoryBucket(rate)
limiter = Limiter(bucket, buffer_ms=100) # 100ms buffer
```
### Backends
A few different bucket backends are available:
- **InMemoryBucket**: using python built-in list as bucket
- **MultiprocessBucket**: uses a multiprocessing lock for distributed concurrency with a ListProxy as the bucket
- **RedisBucket**, using err... redis, with both async/sync support
- **PostgresBucket**, using `psycopg2`
- **SQLiteBucket**, using sqlite3
- **BucketAsyncWrapper**: wraps an existing bucket with async interfaces, to avoid blocking the event loop
#### InMemoryBucket
The default bucket is stored in memory, using python `list`
```python
from pyrate_limiter import InMemoryBucket, Rate, Duration
rates = [Rate(5, Duration.MINUTE * 2)]
bucket = InMemoryBucket(rates)
```
This bucket only availabe in `sync` mode. The only constructor argument is `List[Rate]`.
#### MultiprocessBucket
MultiprocessBucket uses a ListProxy to store items within a python multiprocessing pool or ProcessPoolExecutor. Concurrency is enforced via a multiprocessing Lock.
The bucket is shared across instances.
An example is provided in [in_memory_multiprocess](https://github.com/vutran1710/PyrateLimiter/blob/master/examples/in_memory_multiprocess.py)
Whenever multiprocessing, bucket.waiting calculations will be often wrong because of the concurrency involved. Set Limiter.retry_until_max_delay=True so that the
item keeps retrying rather than returning False when contention causes an extra delay.
#### RedisBucket
RedisBucket uses `Sorted-Set` to store items with key being item's name and score item's timestamp
Because it is intended to work with both async & sync, we provide a classmethod `init` for it
```python
from pyrate_limiter import RedisBucket, Rate, Duration
# Using synchronous redis
from redis import ConnectionPool
from redis import Redis
rates = [Rate(5, Duration.MINUTE * 2)]
pool = ConnectionPool.from_url("redis://localhost:6379")
redis_db = Redis(connection_pool=pool)
bucket_key = "bucket-key"
bucket = RedisBucket.init(rates, redis_db, bucket_key)
# Using asynchronous redis
from redis.asyncio import ConnectionPool as AsyncConnectionPool
from redis.asyncio import Redis as AsyncRedis
pool = AsyncConnectionPool.from_url("redis://localhost:6379")
redis_db = AsyncRedis(connection_pool=pool)
bucket_key = "bucket-key"
bucket = await RedisBucket.init(rates, redis_db, bucket_key)
```
The API are the same, regardless of sync/async. If AsyncRedis is being used, calling `await bucket.method_name(args)` would just work!
#### SQLiteBucket
If you need to persist the bucket state, a SQLite backend is available. The SQLite bucket works in sync manner.
Manully create a connection to Sqlite and pass it along with the table name to the bucket class:
```python
from pyrate_limiter import SQLiteBucket, Rate, Duration
import sqlite3
rates = [Rate(5, Duration.MINUTE * 2)]
bucket = SQLiteBucket.init_from_file(rates)
```
```py
from pyrate_limiter import Rate, Limiter, Duration, SQLiteBucket
requests_per_minute = 5
rate = Rate(requests_per_minute, Duration.MINUTE)
bucket = SQLiteBucket.init_from_file([rate], use_file_lock=False) # set use_file_lock to True if using across multiple processes
limiter = Limiter(bucket, raise_when_fail=False, max_delay=max_delay)
```
You can also pass custom arguments to the `init_from_file` following its signature:
```python
class SQLiteBucket(AbstractBucket):
@classmethod
def init_from_file(
cls,
rates: List[Rate],
table: str = "rate_bucket",
db_path: Optional[str] = None,
create_new_table = True,
use_file_lock: bool = False
) -> "SQLiteBucket":
...
```
Options:
- `db_path`: If not provided, uses `tempdir / "pyrate-limiter.sqlite"`
- `use_file_lock`: Should be False for single process workloads. For multi process, uses a [filelock](https://pypi.org/project/filelock/) to ensure single access to the SQLite bucket across multiple processes, allowing multi process rate limiting on a single host.
Example: [limiter_factory.py::create_sqlite_limiter()](https://github.com/vutran1710/PyrateLimiter/blob/master/pyrate_limiter/limiter_factory.py)
#### PostgresBucket
Postgres is supported, but you have to install `psycopg[pool]` either as an extra or as a separate package. The PostgresBucket currently does not support async.
You can use Postgres's built-in **CURRENT_TIMESTAMP** as the time source with `PostgresClock`, or use an external custom time source.
```python
from pyrate_limiter import PostgresBucket, Rate, PostgresClock
from psycopg_pool import ConnectionPool
connection_pool = ConnectionPool('postgresql://postgres:postgres@localhost:5432')
clock = PostgresClock(connection_pool)
rates = [Rate(3, 1000), Rate(4, 1500)]
bucket = PostgresBucket(connection_pool, "my-bucket-table", rates)
```
#### BucketAsyncWrapper
The BucketAsyncWrapper wraps a sync bucket to ensure all its methods return awaitables. This allows the Limiter to detect
asynchronous behavior and use asyncio.sleep() instead of time.sleep() during delay handling,
preventing blocking of the asyncio event loop.
Example: [limiter_factory.py::create_inmemory_limiter()](https://github.com/vutran1710/PyrateLimiter/blob/master/pyrate_limiter/limiter_factory.py)
### Async or Sync or Multiprocessing
The Limiter is basically made of a Clock backend and a Bucket backend. The backends may be async or sync, which determines the Limiters internal behavior, regardless of whether the caller enters via a sync or async function.
try_acquire_async: When calling from an async context, use try_acquire_async. This uses a thread-local asyncio lock to ensure only one asyncio task is acquiring, followed by a global RLock so that only one thread is acquiring.
try_acquire: When called directly, the global RLock enforces only one thread at a time.
Multiprocessing: If using MultiprocessBucket, two locks are used in Limiter: a top level multiprocessing lock, then a thread level RLock
## Advanced Usage
### Component level diagram

### Time sources
Time source can be anything from anywhere: be it python's built-in time, or monotonic clock, sqliteclock, or crawling from world time server(well we don't have that, but you can!).
```python
from pyrate_limiter import MonotonicClock # use python time.monotonic()
```
Clock's abstract interface only requires implementing a method `now() -> int`. And it can be both sync or async.
### Leaking
Typically bucket should not hold items forever. Bucket's abstract interface requires its implementation must be provided with `leak(current_timestamp: Optional[int] = None)`.
The `leak` method when called is expected to remove any items considered outdated at that moment. During Limiter lifetime, all the buckets' `leak` should be called periodically.
**BucketFactory** provide a method called `schedule_leak` to help deal with this matter. Basically, it will run as a background task for all the buckets currently in use, with interval between `leak` call by **default is 10 seconds**.
```python
# Runnning a background task (whether it is sync/async - doesnt matter)
# calling the bucket's leak
factory.schedule_leak(bucket, clock)
```
You can change this calling interval by overriding BucketFactory's `leak_interval` property. This interval is in **miliseconds**.
```python
class MyBucketFactory(BucketFactory):
def __init__(self, *args):
self.leak_interval = 300
```
When dealing with leak using BucketFactory, the author's suggestion is, we can be pythonic about this by implementing a constructor
```python
class MyBucketFactory(BucketFactory):
def constructor(self, clock, buckets):
self.clock = clock
self.buckets = buckets
for bucket in buckets:
self.schedule_leak(bucket, clock)
```
### Concurrency
Generally, Lock is provided at Limiter's level, except SQLiteBucket case.
### Custom backends
If these don't suit your needs, you can also create your own bucket backend by implementing `pyrate_limiter.AbstractBucket` class.
One of **PyrateLimiter** design goals is powerful extensibility and maximum ease of development.
It must be not only be a ready-to-use tool, but also a guide-line, or a framework that help implementing new features/bucket free of the most hassles.
Due to the composition nature of the library, it is possbile to write minimum code and validate the result:
- Fork the repo
- Implement your bucket with `pyrate_limiter.AbstractBucket`
- Add your own `create_bucket` method in `tests/conftest.py` and pass it to the `create_bucket` fixture
- Run the test suite to validate the result
If the tests pass through, then you are just good to go with your new, fancy bucket!
vutran1710-PyrateLimiter-634d166/benchmarks/ 0000775 0000000 0000000 00000000000 15134637643 0020542 5 ustar 00root root 0000000 0000000 vutran1710-PyrateLimiter-634d166/benchmarks/stress_limiters.py 0000664 0000000 0000000 00000015113 15134637643 0024350 0 ustar 00root root 0000000 0000000 # ruff: noqa: G004
import logging
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, wait
from dataclasses import dataclass
from functools import partial
from time import perf_counter
from typing import Callable, Literal, cast
from pyrate_limiter import Duration, Limiter, MultiprocessBucket, Rate, limiter_factory
logger = logging.getLogger(__name__)
BUFFER_MS: int = 1 # reduce the buffer to improve measurement
TEST_DURATION_SEC: int = 1 # time per test
PREFILL: bool = True
@dataclass
class TestResult:
label: str
requests_per_second: int
test_duration_seconds: int
duration: float
num_requests: int
percent_from_expected_duration: float
def create_mp_limiter(bucket: MultiprocessBucket):
limiter = Limiter(bucket, buffer_ms=BUFFER_MS)
return limiter
def create_rate_limiter_factory(
requests_per_second: int,
backend: Literal["default", "sqlite", "sqlite_filelock", "mp_limiter"],
) -> Callable[[], Limiter]:
"""Returns a callable, so it can be used with multiprocessing"""
rate = Rate(requests_per_second, Duration.SECOND)
if backend == "default":
limiter = limiter_factory.create_inmemory_limiter(rate_per_duration=requests_per_second, duration=Duration.SECOND, buffer_ms=BUFFER_MS)
return lambda: limiter
elif backend == "sqlite":
limiter = limiter_factory.create_sqlite_limiter(
rate_per_duration=requests_per_second, use_file_lock=False, buffer_ms=BUFFER_MS, db_path="pyrate_limiter.sqlite"
)
return lambda: limiter
elif backend == "sqlite_filelock":
return partial(
limiter_factory.create_sqlite_limiter,
rate_per_duration=requests_per_second,
duration=Duration.SECOND,
use_file_lock=True,
buffer_ms=BUFFER_MS,
db_path="pyrate_limiter.sqlite",
)
elif backend == "mp_limiter":
bucket = MultiprocessBucket.init([rate])
return partial(create_mp_limiter, bucket=bucket)
else:
raise ValueError(f"Unexpected backend option: {backend}")
def task():
assert limiter_factory.LIMITER is not None, "Limiter not initialized"
try:
limiter_factory.LIMITER.try_acquire("task")
except Exception as e:
logger.exception(e)
def limiter_init(limiter_creator: Callable[[], Limiter]):
limiter_factory.LIMITER = limiter_creator()
def test_rate_limiter(
limiter_creator: Callable[[], Limiter],
num_requests: int,
use_process_pool: bool,
):
start = perf_counter()
if use_process_pool:
logger.info("Using ProcessPoolExecutor")
with ProcessPoolExecutor(initializer=partial(limiter_init, limiter_creator) if limiter_creator is not None else None) as executor:
if PREFILL:
# Pre-load the buckets, after processes created
limiter = limiter_creator()
[limiter.try_acquire("task") for i in range(requests_per_second)]
futures = [executor.submit(task) for _ in range(num_requests)]
wait(futures)
else:
with ThreadPoolExecutor() as executor:
if PREFILL:
# Pre-load the buckets, after threads created
limiter = limiter_creator()
[limiter.try_acquire("task") for i in range(requests_per_second)]
limiter = limiter_creator()
limiter_factory.LIMITER = limiter
futures = [executor.submit(task) for _ in range(num_requests)]
wait(futures)
for f in futures:
try:
f.result()
except Exception as e:
logger.exception(f"Task raised: {e}")
end = perf_counter()
return end - start
def run_test_limiter(
limiter_creator: Callable,
label: str,
requests_per_second: int,
test_duration_seconds: int,
use_process_pool: bool = False,
):
num_requests = test_duration_seconds * requests_per_second # should finish in around 20 seconds
duration = test_rate_limiter(limiter_creator=limiter_creator, num_requests=num_requests, use_process_pool=use_process_pool)
percent_from_expected_duration = (abs(duration) - test_duration_seconds) / test_duration_seconds
return TestResult(
label=label,
requests_per_second=requests_per_second,
test_duration_seconds=test_duration_seconds,
duration=duration,
num_requests=num_requests,
percent_from_expected_duration=percent_from_expected_duration,
)
if __name__ == "__main__":
import pandas as pd
import plotly.express as px
requests_per_second_list = [10, 100, 1000, 2500, 5000]
test_duration_seconds = TEST_DURATION_SEC
test_results = []
logging.basicConfig(
format="%(asctime)s %(name)s %(levelname)-8s %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
)
for backend in ["default", "sqlite", "mp_limiter"]:
backend = cast(Literal["default", "sqlite", "sqlite_filelock", "mp_limiter"], backend)
for requests_per_second in requests_per_second_list:
logger.info(f"Testing with {backend=}, {requests_per_second=}")
limiter_creator = create_rate_limiter_factory(requests_per_second, backend=backend)
result = run_test_limiter(
limiter_creator=limiter_creator,
label="Threads: " + backend,
requests_per_second=requests_per_second,
test_duration_seconds=test_duration_seconds,
)
test_results.append(result)
logger.info("Testing Multiprocessing")
for backend in ["sqlite_filelock", "mp_limiter"]:
backend = cast(Literal["default", "sqlite", "sqlite_filelock", "mp_limiter"], backend)
for requests_per_second in requests_per_second_list:
logger.info(f"Testing with {backend=}, {requests_per_second=}")
limiter_creator = create_rate_limiter_factory(requests_per_second, backend=backend)
result = run_test_limiter(
limiter_creator=limiter_creator,
label="Processes: " + backend,
requests_per_second=requests_per_second,
test_duration_seconds=test_duration_seconds,
use_process_pool=True,
)
test_results.append(result)
results_df = pd.DataFrame(test_results).sort_values(by="requests_per_second")
results_df["requests_per_second"] = results_df["requests_per_second"].astype(str)
fig = px.line(results_df, x="requests_per_second", y="duration", color="label", markers=True)
fig.write_html("chart.html")
logger.info("Output written to chart.html")
vutran1710-PyrateLimiter-634d166/docker-compose.yaml 0000664 0000000 0000000 00000001166 15134637643 0022227 0 ustar 00root root 0000000 0000000 services:
redis-master:
image: redis:7
command: ["redis-server","--appendonly","yes","--requirepass","changeme"]
ports: ["6379:6379"]
networks: [pyrate-bay]
redis-slave:
image: redis:7
command: ["redis-server","--replicaof","redis-master","6379","--masterauth","changeme","--requirepass","changeme"]
ports: ["6380:6379"]
networks: [pyrate-bay]
postgres:
image: postgres:18
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=postgres
command: ["postgres","-c","max_connections=1000"]
networks:
- pyrate-bay
networks:
pyrate-bay:
driver: bridge vutran1710-PyrateLimiter-634d166/docs/ 0000775 0000000 0000000 00000000000 15134637643 0017355 5 ustar 00root root 0000000 0000000 vutran1710-PyrateLimiter-634d166/docs/_static/ 0000775 0000000 0000000 00000000000 15134637643 0021003 5 ustar 00root root 0000000 0000000 vutran1710-PyrateLimiter-634d166/docs/_static/components.jpg 0000664 0000000 0000000 00000775723 15134637643 0023717 0 ustar 00root root 0000000 0000000 ÿØÿà JFIF ÿâØICC_PROFILE È 0 mntrRGB XYZ à acsp öÖ Ó- desc ð $rXYZ gXYZ ( bXYZ <