Resiliency

Keep your app running smoothly with circuit breakers and retries.

1. Handling API Blocks

One great thing about MedKit is that it deals with random API blocks for you.

For example, if ClinicalTrials.gov blocks your request with a 403 error, MedKit will catch it and try again using a local curl command behind the scenes. Your app won't even notice the hiccup.

# Inside MedKit's ClinicalTrialsProvider.py
try:
    response = await client.get("https://clinicaltrials.gov/api/v2/studies")
    response.raise_for_status()
    # ...
except httpx.HTTPStatusError as e:
    if e.response.status_code == 403:
        # Transparent fallback to cURL spawned as a subprocess
        logger.warning("403 Forbidden. Using curl fallback mechanism.")
        output = await asyncio.create_subprocess_shell("curl -s ...")
        return json.loads(stdout)

2. Circuit Breakers

To prevent your app from crashing if PubMed goes down, MedKit uses a Circuit Breaker.

from medkit.resiliency import CircuitBreaker

# Example Architecture Context
cb = CircuitBreaker(failure_threshold=5, recovery_timeout=60.0)

# If PubMed fails 5 times consecutively, the SDK circuit trips OPEN.
# Subsequent query attempts will immediately raise `CircuitOpenError` natively via Python 
# rather than waiting 15 seconds repeatedly for an HTTP network timeout payload.

This ensures your application's threads aren't locked up waiting on unresponsive servers.

3. Exponential Jitter Retries

You can configure exactly how MedKit retries failed requests. You can determine exactly how many times the client attempts to recover a dropped payload, how long to wait between attempts, and whether to algorithmically use random Jitter delays.

Using Jitter ensures that if 1,000 servers simultaneously experience a PubMed timeout, they randomly stagger their retry attempts, eliminating denial-of-service scenarios against FDA infrastructure.

from medkit import MedKitConfig

# Override the defaults
config = MedKitConfig(
    MEDKIT_RETRY_MAX=3,
    MEDKIT_RETRY_DELAY=2.0,
    MEDKIT_RETRY_JITTER=True # Injects random milliseconds into backoff
)

# MedKit handles the 429 Too Many Requests seamlessly
async with AsyncMedKit(config=config) as med:
    ...
← Previous