Skip to main content

Sliding-window active request counter (last 60s) using Redis for FastAPI or any Python app.

Project description

PyPI version License: MIT Downloads LinkedIn

active_request

active_request is a tiny Redis-backed sliding-window counter for tracking live requests in the last N seconds (default 60s). It is safe across multiple processes and works with both sync and async Redis clients (e.g. FastAPI).

Installation

pip install active_request

Requires Redis server and redis Python client ≥ 5.0.

Usage

Sync

from __future__ import annotations

import os

import redis
from active_request import record_active_request

r = redis.Redis.from_url(os.getenv("REDIS_URL", "redis://localhost:6379/0"))

count = record_active_request(
    r,
    key="metrics:active_requests:api",
    window_seconds=60,
    gauge_key="metrics:active_requests:api:count",
)
print(count)  # live number of requests in the last 60s

Async (FastAPI example)

from __future__ import annotations

import os
from fastapi import FastAPI, Depends
from redis.asyncio import Redis

from active_request import arecord_active_request

app = FastAPI()
redis_url = os.getenv("REDIS_URL", "redis://localhost:6379/0")
r = Redis.from_url(redis_url)


@app.get("/ping")
async def ping():
    # Increment and get live count of requests in the last 60 seconds
    live = await arecord_active_request(
        r,
        key="metrics:active_requests:api",
        window_seconds=60,
        gauge_key="metrics:active_requests:api:count",
    )
    return {"ok": True, "live_60s": live}

Features

  • Sliding window counter using a Redis sorted set + Lua (accurate to milliseconds).
  • Concurrency-safe across multiple workers/processes.
  • Sync and async APIs: record_active_request / arecord_active_request.
  • Optional “gauge” key written with short TTL for easy scraping/dashboards.
  • Small dependency footprint (redis only).

Contributing

Contributions, issues, and feature requests are welcome! Repo: https://github.com/chigwell/active_request • Issues: https://github.com/chigwell/active_request/issues

License

active_request is licensed under the MIT License.

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

active_request-2025.9.152128.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

active_request-2025.9.152128-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file active_request-2025.9.152128.tar.gz.

File metadata

  • Download URL: active_request-2025.9.152128.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.11

File hashes

Hashes for active_request-2025.9.152128.tar.gz
Algorithm Hash digest
SHA256 8c36439663fe28fba35971f1aabeba3dce082c728b0b5a7546bbe893144188a5
MD5 a1ec384a974a5da251e125a7d0afb444
BLAKE2b-256 9892f1a4307c021a65d4824b026751b9f17b91215db8c1af9032a4c1c355f0e8

See more details on using hashes here.

File details

Details for the file active_request-2025.9.152128-py3-none-any.whl.

File metadata

File hashes

Hashes for active_request-2025.9.152128-py3-none-any.whl
Algorithm Hash digest
SHA256 11820ec792b08f110d667e640fded2f85a1b5fec2fb6e0590746edae2ae4ba13
MD5 9c3d09a8b605ca10a40866e5506582f6
BLAKE2b-256 9c5dfdc339032db904f47ea34f307d04017b959dd8d75853f3507e28f04571ed

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