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.151746.tar.gz (3.8 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.151746-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: active_request-2025.9.151746.tar.gz
  • Upload date:
  • Size: 3.8 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.151746.tar.gz
Algorithm Hash digest
SHA256 f1a3d8f6be22ef83e2341cac0bee289a6ff63cd123ccf35b0698d9a8216c883e
MD5 2e612b7eb53d6e35491fab096a516dca
BLAKE2b-256 387666605309abd1a9e34b3d31b15379e8aee72306b44d39a43458cbe6412d9f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for active_request-2025.9.151746-py3-none-any.whl
Algorithm Hash digest
SHA256 3fb7f750c99b1bd74b3c83bd91a4edc736f56fcc5ab4705385517b700ad20525
MD5 8fdcaadf419bc332789178a736aa3eb4
BLAKE2b-256 4a8f2184906655ced1b684970c49d85e2e52bb15d7552b3d09ff475379c90b29

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