Skip to main content

Context-aware decorator to prevent repeated API retries using Redis.

Project description

ContextGuard 🛡️

A plug-and-play decorator to guard your API calls and background tasks using Redis-based context awareness.

🚀 Features

  • ✅ Prevent retrying recent failures (e.g., rate limits, flaky APIs)
  • 🔄 Auto-reset after TTL
  • ⚡ Works for both sync and async functions
  • 🎯 Supports dynamic TTL logic
  • 🧠 Skips re-execution on recent success (optional)
  • 🧰 Namespace-safe Redis keys

📦 Installation

pip install redis

🧑‍💻 Usage

For Sync Functions

@context_guard(lambda x: f"zoom:{x}", ttl=60)
def call_api(x):
    raise Exception("Rate limit error")

For Async Functions

@context_guard(lambda x: f"zoom:{x}", ttl=60)
async def async_call_api(x):
    raise Exception("Async rate limit error")

🔁 Options

  • ttl: static fallback TTL
  • ttl_fn: dynamic TTL per invocation
  • skip_on_success=True: also skip recent successes
  • store_metadata=True: return cached_at and elapsed info
  • redis_client: custom Redis instance

✅ Ideal Use Cases

  • Avoiding rate-limit loops (Zoom, Gmail, Slack, etc.)
  • Skipping repeat ETL calls in Airflow or workers
  • Backoff logic without external libraries

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

contextguard-0.1.2.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

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

contextguard-0.1.2-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

Details for the file contextguard-0.1.2.tar.gz.

File metadata

  • Download URL: contextguard-0.1.2.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for contextguard-0.1.2.tar.gz
Algorithm Hash digest
SHA256 accb35296e57a0e22ad0f74f691d26db0b91f20498fef252e2670169e0542777
MD5 312a1b0e24ec9959f2dda5bc77e3defa
BLAKE2b-256 0c2cc32e4b3f9400b81ab25f4e7f91cc9ca108262839391c9d88f077cf703210

See more details on using hashes here.

File details

Details for the file contextguard-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: contextguard-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for contextguard-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a56b9d5a20b2dfe52b8843e37f914010d7bd651cb7726595ad6966c04b528e15
MD5 d8f4f70370a92e696bffe9a555818835
BLAKE2b-256 a748fb730461cc71637f0e1e375253bdfe36ebd2d26fc5989df7715b241d6d3b

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