Skip to main content

Asynchronous distributed rate limiting for FastAPI/Starlette applications.

Project description

Traffik

Asynchronous distributed rate limiting for FastAPI/Starlette applications.

Test Code Quality codecov PyPI version Python versions

Features

  • Fully Asynchronous: Built for async/await with non-blocking operations and minimal overhead
  • Distributed-First: Atomic operations with distributed locks (Redis, Memcached) achieving high accuracy under concurrency
  • 10+ Strategies: Fixed Window, Sliding Window (Log & Counter), Token Bucket, Leaky Bucket, GCRA, and more
  • HTTP & WebSocket: Full-featured rate limiting for both protocols with per-message throttling support
  • Production-Ready: Circuit breakers, automatic retries, backend failover, and custom error handling
  • Flexible Integration: Dependencies, decorators, middleware, or direct calls — use what fits your architecture
  • Highly Extensible: Simple, well-documented APIs for custom backends, strategies, error handlers, and identifiers
  • Observable: Rich error context and strategy statistics for monitoring
  • Performance-Optimized: Lock striping, connection pooling, script caching, and minimal memory footprint

Built for production workloads.

Documentation

Full documentation is available at https://ti-oluwa.github.io/traffik/, covering:

Installation

# Basic (InMemory backend only)
pip install traffik

# With Redis support
pip install "traffik[redis]"

# With Memcached support
pip install "traffik[memcached]"

# All backends
pip install "traffik[all]"

Quick Start

from fastapi import FastAPI, Depends
from traffik import HTTPThrottle
from traffik.backends.inmemory import InMemoryBackend

backend = InMemoryBackend(namespace="myapp")
app = FastAPI(lifespan=backend.lifespan)

throttle = HTTPThrottle("api:v1", rate="100/min", backend=backend)

@app.get("/items", dependencies=[Depends(throttle)])
async def list_items():
    return {"items": []}

See the Quick Start guide for more examples including Redis, WebSocket throttling, middleware setup, and advanced patterns.

License

MIT — see LICENSE for details.

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

traffik-1.1.0.tar.gz (437.1 kB view details)

Uploaded Source

Built Distribution

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

traffik-1.1.0-py3-none-any.whl (124.4 kB view details)

Uploaded Python 3

File details

Details for the file traffik-1.1.0.tar.gz.

File metadata

  • Download URL: traffik-1.1.0.tar.gz
  • Upload date:
  • Size: 437.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for traffik-1.1.0.tar.gz
Algorithm Hash digest
SHA256 a48e04a936237fbc9d467cc6dffe12685c97a969f51245c64ddb802870aaa908
MD5 05f0a77ab095d92d913b535dd7293ba4
BLAKE2b-256 0bba62db9c8c38a83274af73aeacc456944a146c91f4f55dcf84707c64fa8753

See more details on using hashes here.

File details

Details for the file traffik-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: traffik-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 124.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for traffik-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e84b610b0dc075a059b45fee1dbc9259e892f8b774c40e54872237bacc594aa2
MD5 85259654b7a36c964102a7ba5d1ef065
BLAKE2b-256 27a09d24de54317f54be515d6fa0001efc28a2b6a07d6f14286c481dc663fa17

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